Skip to content

Commit

Permalink
feat: support for all element types that can have an aria-labelledby
Browse files Browse the repository at this point in the history
  • Loading branch information
scottsauber committed Nov 1, 2023
1 parent acfa3f3 commit a84bdd6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/bunit.web.query/Labels/LabelQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class LabelQueryExtensions
new LabelTextUsingForAttributeStrategy(),
new LabelTextUsingAriaLabelStrategy(),
new LabelTextUsingWrappedElementStrategy(),
new LabelTextUsingAriaLabelledByStrategy(),
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AngleSharp.Dom;

namespace Bunit;

internal class LabelTextUsingAriaLabelledByStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText)
{
var elementsWithAriaLabelledBy = renderedFragment.FindAll("[aria-labelledby]");

foreach (var element in elementsWithAriaLabelledBy)
{
var labelElements = renderedFragment.FindAll($"#{element.GetAttribute("aria-labelledby")}");
if (labelElements.Count > 0 && labelElements[0].GetInnerText() == labelText)
return element;
}

return null;
}
}
8 changes: 7 additions & 1 deletion tests/bunit.testassets/BlazorE2E/LabelQueryComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@
@((MarkupString) $"""<{htmlElement} id="{htmlElement}-with-aria-label" aria-label="{htmlElement} Aria Label"></{htmlElement}>""")
}

@* Testing aria-labelledby *@
@foreach (var htmlElement in htmlElementsThatCanHaveALabel)
{
<h2 id="@htmlElement-with-aria-labelledby">@htmlElement Aria Labelled By</h2>
@((MarkupString) $"""<{htmlElement} aria-labelledby="{htmlElement}-with-aria-labelledby"></{htmlElement}>""")
}

@code {
List<string> htmlElementsThatCanHaveALabel = new()
readonly List<string> htmlElementsThatCanHaveALabel = new()
{
"input",
"select",
Expand Down
14 changes: 13 additions & 1 deletion tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public void Test005(string htmlElementWithLabel)
input.Id.ShouldBe($"{htmlElementWithLabel}-with-aria-label");
}

[Theory(DisplayName = "Should return back element associated with another element when that other element uses aria-labelledby")]
[MemberData(nameof(HtmlElementsThatCanHaveALabel))]
public void Test006(string htmlElementWithLabel)
{
var cut = RenderComponent<LabelQueryComponent>();

var input = cut.FindByLabelText($"{htmlElementWithLabel} Aria Labelled By");

input.ShouldNotBeNull();
input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase);
input.GetAttribute("aria-labelledby").ShouldBe($"{htmlElementWithLabel}-with-aria-labelledby");
}

// Throw error that says why
// TODO: get aria-labelledby
}

0 comments on commit a84bdd6

Please sign in to comment.