|
| 1 | +namespace Bunit.TestIds; |
| 2 | + |
| 3 | +public class TestIdQueryExtensionsTests : BunitContext |
| 4 | +{ |
| 5 | + [Fact(DisplayName = "Should find span element with matching testid value")] |
| 6 | + public void Test001() |
| 7 | + { |
| 8 | + var cut = Render<Wrapper>(ps => ps.AddChildContent($"""<span data-testid="myTestId"><span>""")); |
| 9 | + |
| 10 | + var elem = cut.FindByTestId("myTestId"); |
| 11 | + |
| 12 | + elem.ShouldNotBeNull(); |
| 13 | + elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); |
| 14 | + elem.GetAttribute("data-testid").ShouldBe("myTestId"); |
| 15 | + } |
| 16 | + |
| 17 | + [Fact(DisplayName = "Should throw exception when testid does not exist in the DOM")] |
| 18 | + public void Test002() |
| 19 | + { |
| 20 | + var cut = Render<Wrapper>(ps => ps.AddChildContent("""<span data-testid="testId"><span>""")); |
| 21 | + |
| 22 | + Should.Throw<TestIdNotFoundException>(() => cut.FindByTestId("myTestId")).TestId.ShouldBe("myTestId"); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact(DisplayName = "Should throw exception when testid casing is different from DOM")] |
| 26 | + public void Test003() |
| 27 | + { |
| 28 | + var cut = Render<Wrapper>(ps => ps.AddChildContent("""<span data-testid="testId"><span>""")); |
| 29 | + |
| 30 | + Should.Throw<TestIdNotFoundException>(() => cut.FindByTestId("MYTESTID")).TestId.ShouldBe("MYTESTID"); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact(DisplayName = "Should find first div element with matching testid value")] |
| 34 | + public void Test004() |
| 35 | + { |
| 36 | + var cut = Render<Wrapper>(ps => ps.AddChildContent($""" |
| 37 | + <div data-testid="myTestId"></div> |
| 38 | + <span data-testid="myTestId"><span> |
| 39 | + """)); |
| 40 | + |
| 41 | + var elem = cut.FindByTestId("myTestId"); |
| 42 | + |
| 43 | + elem.ShouldNotBeNull(); |
| 44 | + elem.NodeName.ShouldBe("DIV", StringCompareShould.IgnoreCase); |
| 45 | + elem.GetAttribute("data-testid").ShouldBe("myTestId"); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact(DisplayName = "Should find first non-child div element with matching testid value")] |
| 49 | + public void Test005() |
| 50 | + { |
| 51 | + var cut = Render<Wrapper>(ps => ps.AddChildContent($""" |
| 52 | + <div data-testid="myTestId"> |
| 53 | + <span data-testid="myTestId"><span> |
| 54 | + </div> |
| 55 | + """)); |
| 56 | + |
| 57 | + var elem = cut.FindByTestId("myTestId"); |
| 58 | + |
| 59 | + elem.ShouldNotBeNull(); |
| 60 | + elem.NodeName.ShouldBe("DIV", StringCompareShould.IgnoreCase); |
| 61 | + elem.GetAttribute("data-testid").ShouldBe("myTestId"); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact(DisplayName = "Should find span element with matching testid attribute name and value")] |
| 65 | + public void Test006() |
| 66 | + { |
| 67 | + var cut = Render<Wrapper>(ps => ps.AddChildContent($"""<span data-testidattr="myTestId"><span>""")); |
| 68 | + |
| 69 | + var elem = cut.FindByTestId("myTestId", opts => opts.TestIdAttribute = "data-testidattr"); |
| 70 | + |
| 71 | + elem.ShouldNotBeNull(); |
| 72 | + elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); |
| 73 | + elem.GetAttribute("data-testidattr").ShouldBe("myTestId"); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact(DisplayName = "Should find span element with equivalent case-insensitive testid value")] |
| 77 | + public void Test007() |
| 78 | + { |
| 79 | + var cut = Render<Wrapper>(ps => ps.AddChildContent("""<span data-testid="myTestId"><span>""")); |
| 80 | + |
| 81 | + var elem = cut.FindByTestId("MYTESTID", opts => opts.ComparisonType = StringComparison.OrdinalIgnoreCase); |
| 82 | + |
| 83 | + elem.ShouldNotBeNull(); |
| 84 | + elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); |
| 85 | + elem.GetAttribute("data-testid").ShouldBe("myTestId"); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact(DisplayName = "Should find span element with equivalent case-sensitive testid value")] |
| 89 | + public void Test008() |
| 90 | + { |
| 91 | + var cut = Render<Wrapper>(ps => ps.AddChildContent(""" |
| 92 | + <span data-testid="myTestId"><span> |
| 93 | + <span data-testid="MYTESTID"><span> |
| 94 | + """)); |
| 95 | + |
| 96 | + var elem = cut.FindByTestId("MYTESTID"); |
| 97 | + |
| 98 | + elem.ShouldNotBeNull(); |
| 99 | + elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); |
| 100 | + elem.GetAttribute("data-testid").ShouldBe("MYTESTID"); |
| 101 | + } |
| 102 | +} |
0 commit comments