!2430 test(#I4TWCR): add unit test for StringFilter

* test: 增加单元测试
* refactor: 代码重构
This commit is contained in:
Argo 2022-02-16 05:20:18 +00:00
parent a16759326e
commit 94a395fda0
2 changed files with 91 additions and 15 deletions

View File

@ -41,10 +41,10 @@ public partial class StringFilter
Items = new SelectedItem[]
{
new SelectedItem("Contains", Localizer["Contains"]?.Value ?? "Contains"),
new SelectedItem("Equal", Localizer["Equal"]?.Value ?? "Equal"),
new SelectedItem("NotEqual", Localizer["NotEqual"]?.Value ?? "NotEqual"),
new SelectedItem("NotContains", Localizer["NotContains"]?.Value ?? "NotContains")
new SelectedItem("Contains", Localizer["Contains"].Value),
new SelectedItem("Equal", Localizer["Equal"].Value),
new SelectedItem("NotEqual", Localizer["NotEqual"].Value),
new SelectedItem("NotContains", Localizer["NotContains"].Value)
};
}
@ -69,19 +69,26 @@ public partial class StringFilter
public override IEnumerable<FilterKeyValueAction> GetFilterConditions()
{
var filters = new List<FilterKeyValueAction>();
if (!string.IsNullOrEmpty(Value1)) filters.Add(new FilterKeyValueAction()
if (!string.IsNullOrEmpty(Value1))
{
FieldKey = FieldKey,
FieldValue = Value1,
FilterAction = Action1
});
if (Count > 0 && !string.IsNullOrEmpty(Value2)) filters.Add(new FilterKeyValueAction()
filters.Add(new FilterKeyValueAction()
{
FieldKey = FieldKey,
FieldValue = Value1,
FilterAction = Action1
});
}
if (Count > 0 && !string.IsNullOrEmpty(Value2))
{
FieldKey = FieldKey,
FieldValue = Value2,
FilterAction = Action2,
FilterLogic = Logic
});
filters.Add(new FilterKeyValueAction()
{
FieldKey = FieldKey,
FieldValue = Value2,
FilterAction = Action2,
FilterLogic = Logic
});
}
return filters;
}
}

View File

@ -0,0 +1,69 @@
// 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 UnitTest.Extensions;
namespace UnitTest.Components;
public class TableStringFilterTest : BootstrapBlazorTestBase
{
[Fact]
public void Count_Ok()
{
var cut = Context.RenderComponent<StringFilter>(pb =>
{
pb.Add(a => a.Count, 2);
});
var logic = cut.FindComponent<FilterLogicItem>();
Assert.NotNull(logic);
var conditions = cut.Instance.GetFilterConditions();
Assert.Empty(conditions);
var dt = cut.FindComponent<BootstrapInput<string>>().Instance;
cut.InvokeAsync(() => dt.SetValue("Test"));
conditions = cut.Instance.GetFilterConditions();
Assert.Single(conditions);
dt = cut.FindComponents<BootstrapInput<string>>()[1].Instance;
cut.InvokeAsync(() => dt.SetValue("Test"));
conditions = cut.Instance.GetFilterConditions();
Assert.Equal(2, conditions.Count());
}
[Fact]
public void IsHeaderRow_OnFilterValueChanged()
{
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.Items, new List<Foo>() { new Foo() });
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.ShowFilterHeader, true);
pb.Add(a => a.TableColumns, new RenderFragment<Foo>(foo => builder =>
{
var index = 0;
builder.OpenComponent<TableColumn<Foo, string>>(index++);
builder.AddAttribute(index++, nameof(TableColumn<Foo, string>.Field), foo.Name);
builder.AddAttribute(index++, nameof(TableColumn<Foo, string>.FieldExpression), foo.GenerateValueExpression());
builder.AddAttribute(index++, nameof(TableColumn<Foo, string>.Filterable), true);
builder.CloseComponent();
}));
});
});
var filter = cut.FindComponent<BootstrapInput<string>>().Instance;
cut.InvokeAsync(() => filter.SetValue("test"));
var items = cut.FindAll(".dropdown-item");
IEnumerable<FilterKeyValueAction>? condtions = null;
cut.InvokeAsync(() => items[1].Click());
cut.InvokeAsync(() => condtions = cut.FindComponent<StringFilter>().Instance.GetFilterConditions());
Assert.Single(condtions);
}
}