-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathLabelQueryExtensions.cs
More file actions
94 lines (80 loc) · 3.55 KB
/
LabelQueryExtensions.cs
File metadata and controls
94 lines (80 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using AngleSharp.Dom;
using Bunit.Labels.Strategies;
using Bunit.Web.AngleSharp;
namespace Bunit;
/// <summary>
/// Extension methods for querying <see cref="IRenderedComponent{TComponent}" /> by Label
/// </summary>
public static class LabelQueryExtensions
{
private static readonly IReadOnlyList<ILabelTextQueryStrategy> LabelTextQueryStrategies =
[
// This is intentionally in the order of most likely to minimize strategies tried to find the label
new LabelTextUsingForAttributeStrategy(),
new LabelTextUsingAriaLabelStrategy(),
new LabelTextUsingWrappedElementStrategy(),
new LabelTextUsingAriaLabelledByStrategy(),
];
/// <summary>
/// Returns the first element (i.e. an input, select, textarea, etc. element) associated with the given label text.
/// </summary>
/// <param name="renderedComponent">The rendered fragment to search.</param>
/// <param name="labelText">The text of the label to search (i.e. the InnerText of the Label, such as "First Name" for a `<label>First Name</label>`)</param>
/// <param name="configureOptions">Method used to override the default behavior of FindByLabelText.</param>
public static IElement FindByLabelText(this IRenderedComponent<IComponent> renderedComponent, string labelText, Action<ByLabelTextOptions>? configureOptions = null)
{
var options = ByLabelTextOptions.Default;
if (configureOptions is not null)
{
options = options with { };
configureOptions.Invoke(options);
}
return FindByLabelTextInternal(renderedComponent, labelText, options) ?? throw new LabelNotFoundException(labelText);
}
/// <summary>
/// Returns all elements (i.e. input, select, textarea, etc. elements) associated with the given label text.
/// </summary>
/// <param name="renderedComponent">The rendered fragment to search.</param>
/// <param name="labelText">The text of the label to search (i.e. the InnerText of the Label, such as "First Name" for a `<label>First Name</label>`)</param>
/// <param name="configureOptions">Method used to override the default behavior of FindAllByLabelText.</param>
/// <returns>A read-only collection of elements matching the label text. Returns an empty collection if no matches are found.</returns>
public static IReadOnlyList<IElement> FindAllByLabelText(this IRenderedComponent<IComponent> renderedComponent, string labelText, Action<ByLabelTextOptions>? configureOptions = null)
{
var options = ByLabelTextOptions.Default;
if (configureOptions is not null)
{
options = options with { };
configureOptions.Invoke(options);
}
return FindAllByLabelTextInternal(renderedComponent, labelText, options);
}
internal static IElement? FindByLabelTextInternal(this IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
foreach (var strategy in LabelTextQueryStrategies)
{
var element = strategy.FindElement(renderedComponent, labelText, options);
if (element is not null)
return element;
}
return null;
}
internal static IReadOnlyList<IElement> FindAllByLabelTextInternal(this IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
var results = new List<IElement>();
foreach (var strategy in LabelTextQueryStrategies)
{
results.AddRange(strategy.FindElements(renderedComponent, labelText, options));
}
var seen = new HashSet<IElement>();
var distinctResults = new List<IElement>();
foreach (var element in results)
{
var underlyingElement = element.Unwrap();
if (seen.Add(underlyingElement))
{
distinctResults.Add(element);
}
}
return distinctResults;
}
}