Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3cacae2

Browse files
committedMar 17, 2024·
fix: cover scenario where wrapped label has nested HTML
1 parent 1f16c19 commit 3cacae2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
 

‎src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal sealed class LabelTextUsingWrappedElementStrategy : ILabelTextQueryStra
88
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
99
{
1010
var matchingLabel = renderedFragment.Nodes.TryQuerySelectorAll("label")
11-
.SingleOrDefault(l => l.InnerHtml.Trim().StartsWith(labelText, options.ComparisonType));
11+
.SingleOrDefault(l => l.GetInnerText().Trim().StartsWith(labelText, options.ComparisonType));
1212

1313
var matchingElement = matchingLabel?
1414
.Children

‎tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,23 @@ public void Test019(string htmlElementWithLabel)
320320
input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase);
321321
input.GetAttribute("aria-labelledby").ShouldBe($"{htmlElementWithLabel}-with-aria-labelledby");
322322
}
323+
324+
[Theory(DisplayName = "Should return back element associated with label when is wrapped around element with label containing nested html")]
325+
[MemberData(nameof(HtmlElementsThatCanHaveALabel))]
326+
public void Test020(string htmlElementWithLabel)
327+
{
328+
var cut = RenderComponent<Wrapper>(ps =>
329+
ps.AddChildContent($"""
330+
<label>
331+
<p><span>Test Label</p></span>
332+
<{htmlElementWithLabel} id="{htmlElementWithLabel}-wrapped-label" />
333+
</label>
334+
"""));
335+
336+
var input = cut.FindByLabelText("Test Label");
337+
338+
input.ShouldNotBeNull();
339+
input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase);
340+
input.Id.ShouldBe($"{htmlElementWithLabel}-wrapped-label");
341+
}
323342
}

0 commit comments

Comments
 (0)
Please sign in to comment.