From 120227d94958e997435173e2003d74d6aab12c50 Mon Sep 17 00:00:00 2001 From: scottsauber Date: Tue, 31 Oct 2023 23:25:05 -0500 Subject: [PATCH] feat: support for all element types that can have a wrapped label --- src/bunit.web.query/Labels/LabelQueryConstants.cs | 15 +++++++++++++++ .../LabelTextUsingWrappedElementStrategy.cs | 2 +- .../BlazorE2E/LabelQueryComponent.razor | 9 ++++++--- .../Labels/LabelQueryExtensionsTests.cs | 10 ++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/bunit.web.query/Labels/LabelQueryConstants.cs diff --git a/src/bunit.web.query/Labels/LabelQueryConstants.cs b/src/bunit.web.query/Labels/LabelQueryConstants.cs new file mode 100644 index 000000000..f9b144697 --- /dev/null +++ b/src/bunit.web.query/Labels/LabelQueryConstants.cs @@ -0,0 +1,15 @@ +namespace Bunit; + +internal class LabelQueryConstants +{ + internal static readonly List HtmlElementsThatCanHaveALabel = new() + { + "input", + "select", + "textarea", + "button", + "meter", + "output", + "progress", + }; +} diff --git a/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs b/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs index 3b10f0a91..c27a319be 100644 --- a/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs @@ -11,6 +11,6 @@ internal class LabelTextUsingWrappedElementStrategy : ILabelTextQueryStrategy return matchingLabel? .Children - .SingleOrDefault(n => n.NodeName == "INPUT"); + .SingleOrDefault(n => LabelQueryConstants.HtmlElementsThatCanHaveALabel.Contains(n.NodeName, StringComparer.OrdinalIgnoreCase)); } } diff --git a/tests/bunit.testassets/BlazorE2E/LabelQueryComponent.razor b/tests/bunit.testassets/BlazorE2E/LabelQueryComponent.razor index a57bd52b3..b9a2fbdc8 100644 --- a/tests/bunit.testassets/BlazorE2E/LabelQueryComponent.razor +++ b/tests/bunit.testassets/BlazorE2E/LabelQueryComponent.razor @@ -10,9 +10,12 @@ @* Testing wrapped label *@ - +@foreach (var htmlElement in htmlElementsThatCanHaveALabel) +{ + +} @* Testing no input *@ diff --git a/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs b/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs index 0f220bca3..d0666de77 100644 --- a/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs +++ b/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs @@ -38,15 +38,17 @@ public void Test002() .CssSelector.ShouldBe(expectedLabelText); } - [Fact(DisplayName = "Should return back input associated with label when label when is wrapped around input")] - public void Test003() + [Theory(DisplayName = "Should return back element associated with label when label when is wrapped around element")] + [MemberData(nameof(HtmlElementsThatCanHaveALabel))] + public void Test003(string htmlElementWithLabel) { var cut = RenderComponent(); - var input = cut.FindByLabelText("Wrapped Label"); + var input = cut.FindByLabelText($"{htmlElementWithLabel} Wrapped Label"); input.ShouldNotBeNull(); - input.Id.ShouldBe("wrapped-label"); + input.NodeName.ShouldBe(htmlElementWithLabel, StringCompareShould.IgnoreCase); + input.Id.ShouldBe($"{htmlElementWithLabel}-wrapped-label"); } [Fact(DisplayName = "Should throw exception when label text exists but is not tied to any input")]