!2371 test(#I4SKQA): add unit test for RadioList

* test: 增加 RadioList 单元测试
* refactor: 移除冗余代码
* refactor: 更新注释
* test: 增加 OnSelectedChanged 单元测试
* test: 增加值不在 Items 中的单元测试
* refactor: 移除不需要的代码
* test: 增加可为空单元测试
* test: 增加 Enum 类型单元测试
* fix: 重载 Value 检查方法
* Merge branch 'main' into dev-test-radiolist
* Merge branch 'main' into dev-test-radiolist
* chore: 移除配置文件
* refactor: 格式化代码
* Merge branch 'main' into dev-test-radiolist
* test: 添加 radiolist 单元测试
This commit is contained in:
Argo 2022-01-31 07:43:41 +00:00
parent 901c2032c5
commit 707c1904dc
2 changed files with 123 additions and 55 deletions

View File

@ -5,7 +5,6 @@
using Microsoft.AspNetCore.Components;
using System;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
@ -29,7 +28,7 @@ public partial class RadioList<TValue>
public bool IsAutoAddNullItem { get; set; }
/// <summary>
/// 获得/设置 空值项显示文字 默认为 null 是否自动添加空值请参考 <see cref="IsAutoAddNullItem"/>
/// 获得/设置 空值项显示文字 默认为 "" 是否自动添加空值请参考 <see cref="IsAutoAddNullItem"/>
/// </summary>
[Parameter]
public string NullItemText { get; set; } = "";
@ -62,9 +61,7 @@ public partial class RadioList<TValue>
if (!Items.Any(i => i.Value == CurrentValueAsString))
{
CurrentValueAsString = Items.FirstOrDefault(i => i.Active)?.Value
?? Items.FirstOrDefault()?.Value
?? "";
CurrentValueAsString = Items.FirstOrDefault()?.Value ?? "";
}
}
@ -73,10 +70,12 @@ public partial class RadioList<TValue>
/// </summary>
/// <param name="typeValue"></param>
/// <param name="list"></param>
protected override void ProcessGenericItems(Type typeValue, IEnumerable? list)
{
protected override void ProcessGenericItems(Type typeValue, IEnumerable? list) { }
}
/// <summary>
///
/// </summary>
protected override void EnsureParameterValid() { }
/// <summary>
/// 点击选择框方法
@ -102,51 +101,5 @@ public partial class RadioList<TValue>
}
}
private CheckboxState CheckState(SelectedItem item)
{
return item.Value == CurrentValueAsString ? CheckboxState.Checked : CheckboxState.UnChecked;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="result"></param>
/// <param name="validationErrorMessage"></param>
/// <returns></returns>
protected override bool TryParseValueFromString(string value, [MaybeNullWhen(false)] out TValue result, out string? validationErrorMessage)
{
var ret = false;
if (typeof(TValue) == typeof(SelectedItem))
{
var val = Items.FirstOrDefault(i => i.Value == value)
?? Items.FirstOrDefault();
if (val != null)
{
result = (TValue)(object)val;
}
else
{
result = default;
}
validationErrorMessage = null;
ret = true;
}
else
{
ret = base.TryParseValueFromString(value, out result, out validationErrorMessage);
}
return ret;
}
/// <summary>
/// 将 Value 格式化为 String 方法
/// </summary>
/// <param name="value">The value to format.</param>
/// <returns>A string representation of the value.</returns>
protected override string? FormatValueAsString(TValue value) => typeof(TValue).Name switch
{
nameof(SelectedItem) => (value as SelectedItem)?.Value,
_ => value?.ToString()
};
private CheckboxState CheckState(SelectedItem item) => item.Value == CurrentValueAsString ? CheckboxState.Checked : CheckboxState.UnChecked;
}

View File

@ -0,0 +1,115 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
namespace UnitTest.Components;
public class RadioTest : BootstrapBlazorTestBase
{
[Fact]
public void EnumValue_Ok()
{
var v = EnumEducation.Middel;
var cut = Context.RenderComponent<RadioList<EnumEducation>>(pb =>
{
pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList());
pb.Add(a => a.Value, v);
});
Assert.Contains("form-check-input", cut.Markup);
}
[Fact]
public void EnumNullValue_Ok()
{
EnumEducation? v = null;
var cut = Context.RenderComponent<RadioList<EnumEducation?>>(pb =>
{
pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList());
pb.Add(a => a.Value, v);
pb.Add(a => a.NullItemText, "");
pb.Add(a => a.IsAutoAddNullItem, true);
});
Assert.Contains("form-check-input", cut.Markup);
}
[Fact]
public void NotInItems_Ok()
{
// 组件值为 test
// 组件给的候选 Items 中无 test 选项
string v = "test";
var cut = Context.RenderComponent<RadioList<string>>(pb =>
{
pb.Add(a => a.Items, new List<SelectedItem>() { new("1", "test1") });
pb.Add(a => a.Value, v);
});
Assert.Equal("1", cut.Instance.Value);
cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.Items, new List<SelectedItem>() { });
});
Assert.Equal("", cut.Instance.Value);
}
[Fact]
public void OnSelectedChanged_Ok()
{
var selected = false;
var v = EnumEducation.Middel;
var cut = Context.RenderComponent<RadioList<EnumEducation>>(pb =>
{
pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList());
pb.Add(a => a.Value, v);
pb.Add(a => a.OnSelectedChanged, (items, v) =>
{
selected = true;
return Task.CompletedTask;
});
});
var item = cut.Find(".form-check-input");
item.Click();
Assert.True(selected);
}
[Fact]
public void OnSelectedChanged_SelectedItem_Ok()
{
var selected = false;
var v = new SelectedItem(nameof(EnumEducation.Primary), "Test 1");
var cut = Context.RenderComponent<RadioList<SelectedItem>>(pb =>
{
pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList());
pb.Add(a => a.Value, v);
pb.Add(a => a.OnSelectedChanged, (items, v) =>
{
selected = true;
return Task.CompletedTask;
});
});
var item = cut.Find(".form-check-input");
item.Click();
Assert.True(selected);
}
[Fact]
public void GenericValue_Ok()
{
var cut = Context.RenderComponent<RadioList<RadioListGenericMock<string>>>(pb =>
{
pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList());
pb.Add(a => a.Color, Color.Primary);
pb.Add(a => a.Value, new RadioListGenericMock<string>());
});
Assert.Contains("primary", cut.Markup);
}
private class RadioListGenericMock<T>
{
}
}