Skip to content

Commit ce9640b

Browse files
committed
add unit tests
1 parent 4e4623e commit ce9640b

1 file changed

Lines changed: 130 additions & 9 deletions

File tree

tests/StaticInput.UnitTests/Components/RadioTests.cs

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using FluentAssertions;
1+
using AngleSharp.Dom;
2+
using Bunit;
3+
using FluentAssertions;
4+
using Microsoft.Playwright;
25
using MudBlazor.StaticInput;
36
using StaticInput.UnitTests.Fixtures;
4-
using Bunit;
5-
using AngleSharp.Dom;
67
using StaticInput.UnitTests.Viewer.Components.Tests.Radio;
8+
using static Microsoft.Playwright.Assertions;
79

810
namespace StaticInput.UnitTests.Components
911
{
@@ -56,14 +58,133 @@ public void MudStaticRadio_Icon_Should_Reflect_Checked_State()
5658
IElement radioA = radioInputs.First(r => r.GetAttribute("value") == "A");
5759
IElement radioB = radioInputs.First(r => r.GetAttribute("value") == "B");
5860

59-
IElement? iconA = radioA.ParentElement!.QuerySelector(".mud-icon-root");
60-
IElement? iconB = radioB.ParentElement!.QuerySelector(".mud-icon-root");
61+
var iconA = radioA.ParentElement!.QuerySelectorAll(".mud-icon-root");
62+
var iconB = radioB.ParentElement!.QuerySelectorAll(".mud-icon-root");
63+
64+
iconA.Should().NotBeNullOrEmpty("The A radio input should be rendered.");
65+
iconB.Should().NotBeNullOrEmpty("The B radio input should be rendered.");
66+
67+
iconA.First(x => x.Id!.StartsWith("radio-checked-icon-")).GetAttribute("style")!.Should().Contain("display: block", "The A radio input is checked");
68+
iconA.First(x => x.Id!.StartsWith("radio-unchecked-icon-")).GetAttribute("style")!.Should().Contain("display: none", "The A radio input is checked");
69+
70+
iconB.First(x => x.Id!.StartsWith("radio-checked-icon-")).GetAttribute("style")!.Should().Contain("display: none", "The B radio input is checked");
71+
iconB.First(x => x.Id!.StartsWith("radio-unchecked-icon-")).GetAttribute("style")!.Should().Contain("display: block", "The B radio input is checked");
72+
}
73+
74+
[Fact]
75+
public void MudStaticRadio_Checked_Unchecked_Colors_Should_Differ()
76+
{
77+
var comp = Context.RenderComponent<RadioColorsTest>();
78+
79+
IRefreshableElementCollection<IElement> radioInputs = comp.FindAll("input[type='radio']");
80+
IElement radioA = radioInputs.First(r => r.GetAttribute("value") == "A");
81+
IElement radioB = radioInputs.First(r => r.GetAttribute("value") == "B");
82+
83+
var iconA = radioA.ParentElement!.QuerySelectorAll(".mud-icon-root");
84+
var iconB = radioB.ParentElement!.QuerySelectorAll(".mud-icon-root");
85+
86+
87+
iconA.First(x => x.Id!.StartsWith("radio-checked-icon-")).ClassList.Should().Contain("mud-primary-text", "Checked should be primary color");
88+
iconA.First(x => x.Id!.StartsWith("radio-unchecked-icon-")).ClassList.Should().Contain("mud-error-text", "Unchecked should be error color");
89+
90+
iconB.First(x => x.Id!.StartsWith("radio-checked-icon-")).ClassList.Should().Contain("mud-primary-text", "Checked should be primary color");
91+
iconB.First(x => x.Id!.StartsWith("radio-unchecked-icon-")).ClassList.Should().Contain("mud-info-text", "Unchecked should be info color");
92+
}
93+
94+
[Fact]
95+
public async Task CheckBox_State_Changes_When_Clicked()
96+
{
97+
var url = typeof(RadioIconTest).ToQueryString();
98+
99+
await Page.GotoAsync(url);
100+
101+
var radioA = Page.Locator("input.static-radio-input[value='A']");
102+
var radioB = Page.Locator("input.static-radio-input[value='B']");
103+
104+
await Expect(radioA).ToBeCheckedAsync();
105+
await Expect(radioB).ToBeCheckedAsync(new() { Checked = false });
106+
107+
await radioB.CheckAsync();
108+
109+
await Expect(radioA).ToBeCheckedAsync(new() { Checked = false });
110+
await Expect(radioB).ToBeCheckedAsync();
111+
112+
}
113+
114+
[Fact]
115+
public async Task Radio_Disabled_Cannot_Be_Selected()
116+
{
117+
var url = typeof(RadioDisabledTest).ToQueryString();
118+
119+
await Page.GotoAsync(url);
120+
121+
var radioA = Page.Locator("input.static-radio-input[value='A']");
122+
var radioB = Page.Locator("input.static-radio-input[value='B']");
61123

62-
iconA.Should().NotBeNull("The A radio input should be rendered.");
63-
iconB.Should().NotBeNull("The B radio input should be rendered.");
124+
await Expect(radioA).ToBeEnabledAsync();
125+
await Expect(radioB).ToBeDisabledAsync();
64126

65-
iconA.InnerHtml.Should().Contain("M12 7c-2.76", "The A radio input is checked");
66-
iconB.InnerHtml.Should().Contain("M12 2C6.48", "The B radio input is checked");
127+
await radioA.CheckAsync();
128+
129+
await Expect(radioA).ToBeCheckedAsync();
130+
await Expect(radioB).ToBeCheckedAsync(new() { Checked = false });
131+
132+
try
133+
{
134+
await radioB.CheckAsync(new() { Timeout = 2500 });
135+
}
136+
catch (TimeoutException) { }
137+
138+
await Expect(radioB).ToBeCheckedAsync(new() { Checked = false });
139+
await Expect(radioA).ToBeCheckedAsync();
67140
}
141+
142+
[Fact]
143+
public async Task Radio_Should_Checked_To_Submit()
144+
{
145+
var url = typeof(RadioRequiredTest).ToQueryString();
146+
147+
await Page.GotoAsync(url);
148+
149+
var button = Page.GetByRole(AriaRole.Button, new() { Name = "Submit" });
150+
151+
await button.ClickAsync();
152+
153+
var content = await Page.ContentAsync();
154+
155+
content.Should().Contain("Selection required!");
156+
157+
var radioC = Page.Locator("input.static-radio-input[value='C']");
158+
159+
await radioC.CheckAsync();
160+
await Expect(radioC).ToBeCheckedAsync();
161+
162+
await button.ClickAsync();
163+
164+
await Expect(Page).ToHaveTitleAsync("Home");
165+
}
166+
167+
[Fact]
168+
public async Task Radio_Should_Initialize_True()
169+
{
170+
var url = typeof(RadioInitTest).ToQueryString();
171+
172+
await Page.GotoAsync(url);
173+
174+
var radioA = Page.Locator("input.static-radio-input[value='A']");
175+
var radioB = Page.Locator("input.static-radio-input[value='B']");
176+
var radioC = Page.Locator("input.static-radio-input[value='C']");
177+
178+
await Expect(radioA).ToBeCheckedAsync(new() { Checked = false });
179+
await Expect(radioB).ToBeCheckedAsync(new() { Checked = false });
180+
await Expect(radioC).ToBeCheckedAsync();
181+
182+
await radioA.CheckAsync();
183+
184+
await Expect(radioA).ToBeCheckedAsync();
185+
await Expect(radioB).ToBeCheckedAsync(new() { Checked = false });
186+
await Expect(radioC).ToBeCheckedAsync(new() { Checked = false });
187+
}
188+
68189
}
69190
}

0 commit comments

Comments
 (0)