!2427 test(#I4TV55): add unit test for BoolFilter

* test: 增加 BoolFilter 单元测试代码覆盖率
* test: 增加 BoolFilter 单元测试
* refactor: 重构代码
This commit is contained in:
Argo 2022-02-16 02:38:35 +00:00
parent 6d5ace1963
commit 869bcf505d
2 changed files with 73 additions and 8 deletions

View File

@ -30,9 +30,9 @@ public partial class BoolFilter
Items = new SelectedItem[]
{
new SelectedItem("", Localizer["BoolFilter.AllText"]?.Value ?? "All"),
new SelectedItem("true", Localizer["BoolFilter.TrueText"]?.Value ?? "True"),
new SelectedItem("false", Localizer["BoolFilter.FalseText"]?.Value ?? "False")
new SelectedItem("", Localizer["BoolFilter.AllText"].Value),
new SelectedItem("true", Localizer["BoolFilter.TrueText"].Value),
new SelectedItem("false", Localizer["BoolFilter.FalseText"].Value)
};
if (TableFilter != null)
@ -57,12 +57,15 @@ public partial class BoolFilter
public override IEnumerable<FilterKeyValueAction> GetFilterConditions()
{
var filters = new List<FilterKeyValueAction>();
if (!string.IsNullOrEmpty(Value)) filters.Add(new FilterKeyValueAction()
if (!string.IsNullOrEmpty(Value))
{
FieldKey = FieldKey,
FieldValue = Value == "" ? (object?)null : (Value == "true"),
FilterAction = FilterAction.Equal
});
filters.Add(new FilterKeyValueAction()
{
FieldKey = FieldKey,
FieldValue = Value == "true",
FilterAction = FilterAction.Equal
});
}
return filters;
}
}

View File

@ -0,0 +1,62 @@
// 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/
namespace UnitTest.Components;
public class BoolFilterTest : BootstrapBlazorTestBase
{
[Fact]
public void Reset_Ok()
{
var cut = Context.RenderComponent<BoolFilter>();
var filter = cut.Instance;
cut.InvokeAsync(() => filter.Reset());
}
[Fact]
public void GetFilterConditions_Ok()
{
var cut = Context.RenderComponent<BoolFilter>();
var filter = cut.Instance;
IEnumerable<FilterKeyValueAction>? condtions = null;
cut.InvokeAsync(() => condtions = filter.GetFilterConditions());
Assert.Empty(condtions);
// Set Value
var items = cut.FindAll(".dropdown-item");
cut.InvokeAsync(() => items[1].Click());
cut.InvokeAsync(() => condtions = filter.GetFilterConditions());
Assert.Single(condtions);
}
[Fact]
public void IsHeaderRow_OnSelectedItemChanged()
{
var cut = Context.RenderComponent<MockBoolFilter>();
var filter = cut.Instance;
var items = cut.FindAll(".dropdown-item");
IEnumerable<FilterKeyValueAction>? condtions = null;
cut.InvokeAsync(() => items[1].Click());
cut.InvokeAsync(() => condtions = filter.GetFilterConditions());
Assert.Single(condtions);
}
public class MockBoolFilter : BoolFilter
{
protected override void OnInitialized()
{
base.OnInitialized();
if (TableFilter == null)
{
TableFilter = new TableFilter();
TableFilter.SetParametersAsync(ParameterView.FromDictionary(new Dictionary<string, object?>
{
[nameof(TableFilter.IsHeaderRow)] = true
}));
}
}
}
}