From 00d7fb1510090d1222680bfb2f658086aae3266f Mon Sep 17 00:00:00 2001 From: scottsauber Date: Sat, 16 Mar 2024 21:17:13 -0500 Subject: [PATCH] fix: cover scenario where wrapped label has nested HTML --- .../LabelTextUsingWrappedElementStrategy.cs | 2 +- .../Labels/LabelQueryExtensionsTests.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs b/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs index e0335c054..43f47503c 100644 --- a/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs @@ -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 diff --git a/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs b/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs index 50ecc3d1d..78a1f5b11 100644 --- a/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs +++ b/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs @@ -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(ps => + ps.AddChildContent($""" + + """)); + + var input = cut.FindByLabelText("Test Label"); + + input.ShouldNotBeNull(); + input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase); + input.Id.ShouldBe($"{htmlElementWithLabel}-wrapped-label"); + } }