ant-design-blazor/tests/AntDesign.Tests/Radio/RadioGroupTests.razor
James Yeung a790691187
feat(module: radio): add button style support for RadioGroup with options (#3589)
* feat(module: radio): add button style support for RadioGroup with options

* fix test
2023-12-25 22:40:28 +08:00

257 lines
8.0 KiB
C#

@inherits AntDesignTestBase
@code {
[Fact]
public void Renders_basic_radioGroup()
{
var cut = Context.Render(@<RadioGroup Value="1">
<Radio Value="1">A</Radio>
<Radio Value="2">B</Radio>
<Radio Value="3">C</Radio>
<Radio Value="4">D</Radio>
</RadioGroup>);
cut.MarkupMatches(
@<div class="ant-radio-group" id:ignore>
<label class="ant-radio-wrapper ant-radio-wrapper-checked" id:ignore>
<span class="ant-radio ant-radio-checked">
<input type="radio" class="ant-radio-input" value="1" name:ignore checked="">
<span class="ant-radio-inner"></span>
</span>
<span>A</span>
</label>
<label class="ant-radio-wrapper" id:ignore>
<span class="ant-radio">
<input type="radio" class="ant-radio-input" name:ignore value="2">
<span class="ant-radio-inner"></span>
</span>
<span>B</span>
</label>
<label class="ant-radio-wrapper" id:ignore>
<span class="ant-radio">
<input type="radio" class="ant-radio-input" name:ignore value="3">
<span class="ant-radio-inner"></span>
</span>
<span>C</span>
</label>
<label class="ant-radio-wrapper" id:ignore>
<span class="ant-radio">
<input type="radio" class="ant-radio-input" name:ignore value="4">
<span class="ant-radio-inner"></span>
</span>
<span>D</span>
</label>
</div>);
}
[Fact]
public void Toggle_basic_radioGroup()
{
//Arrange
IRenderedComponent<RadioGroup<string>> cut = Render<RadioGroup<string>>(@<RadioGroup Value=@("1")>
<Radio Value=@("1")>A</Radio>
<Radio Value=@("2")>B</Radio>
<Radio Value=@("3")>C</Radio>
<Radio Value=@("4")>D</Radio>
</RadioGroup>);
var radioComponent = cut.Instance;
var inputElement = cut.Find("[type=radio][value=2]");
//Act
inputElement.Click();
//Assert
radioComponent.Value.Should().Be("2");
}
[Fact]
public void Bind_Value_basic_radioGroup()
{
//Arrange
int value = 1;
var cut = Context.Render(@<RadioGroup @bind-Value="@value">
<Radio Value="1">A</Radio>
<Radio Value="2">B</Radio>
<Radio Value="3">C</Radio>
<Radio Value="4">D</Radio>
</RadioGroup>);
var inputElement = cut.Find("[type=radio][value=2]");
//Act
inputElement.Click();
//Assert
value.Should().Be(2);
}
[Fact]
public void Bind_default_null_value_radioGroup()
{
//Arrange
int? value = null;
var cut = Context.Render(@<RadioGroup @bind-Value="@value">
<Radio TValue="int?" Value="1">A</Radio>
<Radio TValue="int?" Value="2">B</Radio>
<Radio TValue="int?" Value="3">C</Radio>
<Radio TValue="int?" Value="4">D</Radio>
</RadioGroup>
);
var defaultCheckedElement = cut.FindAll(".ant-radio-checked");
var inputElement = cut.Find("[type=radio][value=2]");
//Act
inputElement.Click();
//Assert
defaultCheckedElement.Should().BeEmpty();
value.Should().Be(2);
}
[Fact]
public void set_value_to_null_radioGroup()
{
//Arrange
int? value = 1;
var cut = Context.Render<RadioGroup<int?>>(
@<RadioGroup @bind-Value="@value">
<Radio TValue="int?" Value="1">A</Radio>
<Radio TValue="int?" Value="2">B</Radio>
<Radio TValue="int?" Value="3">C</Radio>
<Radio TValue="int?" Value="4">D</Radio>
</RadioGroup>
);
var defaultValueChecked = cut.Find("[type=radio][value=1]").HasAttribute("checked");
cut.Instance.Value = null;
cut.Render();
cut.FindComponents<Radio<string>>().ForEach(x => x.Render());
// Assert
defaultValueChecked.Should().BeTrue();
cut.FindAll("[type=radio][checked]").Should().BeEmpty();
}
[Fact]
public void Bind_Default_radioGroup()
{
//Arrange
var cut = Render<RadioGroup<int>>(
@<RadioGroup DefaultValue="2">
<Radio Value="1">A</Radio>
<Radio Value="2">B</Radio>
<Radio Value="3">C</Radio>
<Radio Value="4">D</Radio>
</RadioGroup>
);
//Assert
cut.Instance.Value.Should().Be(2);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Sync_Disabled_basic_radioGroup(bool initGroupDisabled)
{
//Arrange
int value = 1;
IRenderedComponent<RadioGroup<int>> radioGroup = Render<RadioGroup<int>>(
@<RadioGroup @bind-Value=@value Disabled=@initGroupDisabled>
<Radio Value="1">A</Radio>
<Radio Value="2">B</Radio>
</RadioGroup>
);
var members = radioGroup.FindComponents<Radio<int>>();
//Act
#pragma warning disable BL0005
radioGroup.Instance.Disabled = !initGroupDisabled;
#pragma warning restore BL0005
radioGroup.Render();
//Assert
Assert.All(members, e => Assert.Equal(!initGroupDisabled, e.Instance.Disabled));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Sync_Disabled_basic_radioGroup_with_options(bool initGroupDisabled)
{
var options = new RadioOption<int>[] { new() { Value = 1, Label = "A" }, new() { Value = 1, Label = "A" } };
//Arrange
int value = 1;
IRenderedComponent<RadioGroup<int>> radioGroup = Render<RadioGroup<int>>(
@<RadioGroup Options="options" @bind-Value=@value Disabled=@initGroupDisabled />
);
var members = radioGroup.FindComponents<Radio<int>>();
//Act
#pragma warning disable BL0005
radioGroup.Instance.Disabled = !initGroupDisabled;
#pragma warning restore BL0005
radioGroup.Render();
//Assert
Assert.All(members, e => Assert.Equal(!initGroupDisabled, e.Instance.Disabled));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Sync_Disabled_exist_disabled_member_radioGroup(bool initGroupDisabled)
{
//Arrange
int value = 1;
IRenderedComponent<RadioGroup<int>> radioGroup = Render<RadioGroup<int>>(
@<RadioGroup @bind-Value=@value Disabled=@initGroupDisabled>
<Radio Value="1" Disabled=true>A</Radio>
<Radio Value="2">B</Radio>
</RadioGroup>
);
var members = radioGroup.FindComponents<Radio<int>>();
//Act
#pragma warning disable BL0005
radioGroup.Instance.Disabled = !initGroupDisabled;
#pragma warning restore BL0005
radioGroup.Render();
//Assert
members[0].Instance.Disabled.Should().BeTrue();
members[1].Instance.Disabled.Should().Be(!initGroupDisabled);
}
[Fact]
public void Change_radio_list_and_binding_value_at_same_time()
{
// Arrange
var radioList = new[] { "1", "2", "3" };
var seletedValue = "1";
var cut = Render<RadioGroup<string>>(
@<RadioGroup @bind-Value="seletedValue">
@foreach (var item in radioList)
{
<Radio Value="item" @key="item" />
}
</RadioGroup>
);
var defaultValueChecked = cut.Find("[type=radio][value=1]").HasAttribute("checked");
// Act
radioList = new[] { "2", "3", "4" };
seletedValue = "4";
cut.Instance.Value = seletedValue;
cut.Render();
cut.WaitForElement("[type=radio][value=4]");
cut.FindComponents<Radio<string>>().ForEach(x => x.Render());
// Assert
defaultValueChecked.Should().BeTrue();
cut.Find("[type=radio][value=4]").HasAttribute("checked").Should().BeTrue();
}
}