Skip to content

Commit

Permalink
fix: cover scenario where wrapped label has nested HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
scottsauber committed Mar 17, 2024
1 parent 1f16c19 commit 00d7fb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed class LabelTextUsingWrappedElementStrategy : ILabelTextQueryStra
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
{
var matchingLabel = renderedFragment.Nodes.TryQuerySelectorAll("label")
.SingleOrDefault(l => l.InnerHtml.Trim().StartsWith(labelText, options.ComparisonType));
.SingleOrDefault(l => l.GetInnerText().Trim().StartsWith(labelText, options.ComparisonType));

var matchingElement = matchingLabel?
.Children
Expand Down
19 changes: 19 additions & 0 deletions tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,23 @@ public void Test019(string htmlElementWithLabel)
input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase);
input.GetAttribute("aria-labelledby").ShouldBe($"{htmlElementWithLabel}-with-aria-labelledby");
}

[Theory(DisplayName = "Should return back element associated with label when is wrapped around element with the correct casing")]
[MemberData(nameof(HtmlElementsThatCanHaveALabel))]
public void Test020(string htmlElementWithLabel)
{
var cut = RenderComponent<Wrapper>(ps =>
ps.AddChildContent($"""
<label>
<p><span>Test Label</p></span>
<{htmlElementWithLabel} id="{htmlElementWithLabel}-wrapped-label" />
</label>
"""));

var input = cut.FindByLabelText("Test Label");

input.ShouldNotBeNull();
input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase);
input.Id.ShouldBe($"{htmlElementWithLabel}-wrapped-label");
}
}

0 comments on commit 00d7fb1

Please sign in to comment.