!2438 test(#I4U8Z3): add unit test for LookupFilter

* test: 增加 FilterLogicItem 单元测试
* test: 增加 SearchFilterAction 单元测试
* refactor: 格式化代码
* test: 增加 FilterKeyValueAction 单元测试
* refactor: 格式化代码
* test: 补全单元测试
* test: 增加 LookupFilter 单元测试
* refactor: 重构代码
This commit is contained in:
Argo 2022-02-18 03:24:13 +00:00
parent 972af30f02
commit 09dbf5d765
5 changed files with 138 additions and 18 deletions

View File

@ -53,9 +53,9 @@ public partial class FilterLogicItem
base.OnInitialized();
Items = new List<SelectedItem>()
{
new SelectedItem("And",Localizer["And"]?.Value ?? "And"),
new SelectedItem("Or",Localizer["Or"]?.Value ?? "Or")
};
{
new SelectedItem("And",Localizer["And"].Value),
new SelectedItem("Or",Localizer["Or"].Value)
};
}
}

View File

@ -16,15 +16,12 @@ public partial class LookupFilter
private List<SelectedItem> Items { get; } = new List<SelectedItem>();
/// <summary>
/// 内部使用
/// </summary>
[NotNull]
private Type? EnumType { get; set; }
/// <summary>
/// 获得/设置 相关枚举类型
/// </summary>
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
[Parameter]
[NotNull]
@ -33,6 +30,9 @@ public partial class LookupFilter
/// <summary>
/// 获得/设置 相关枚举类型
/// </summary>
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
[Parameter]
[NotNull]
public Type? Type { get; set; }
@ -48,11 +48,15 @@ public partial class LookupFilter
{
base.OnInitialized();
if (Lookup == null) throw new InvalidOperationException("the Parameter Lookup must be set.");
if (Type == null) throw new InvalidOperationException("the Parameter Type must be set.");
if (TableFilter != null)
{
TableFilter.ShowMoreButton = false;
}
Items.Add(new SelectedItem("", Localizer["EnumFilter.AllText"]?.Value ?? "All"));
Items.Add(new SelectedItem("", Localizer["EnumFilter.AllText"].Value));
Items.AddRange(Lookup);
}

View File

@ -42,12 +42,12 @@ public class SearchFilterAction : IFilterAction
/// </summary>
/// <returns></returns>
public virtual IEnumerable<FilterKeyValueAction> GetFilterConditions() => new List<FilterKeyValueAction>()
{
new()
{
new()
{
FieldKey = Name,
FieldValue = Value,
FilterAction = Action,
}
};
FieldKey = Name,
FieldValue = Value,
FilterAction = Action,
}
};
}

View File

@ -0,0 +1,97 @@
// 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 TableLookupFilterTest : BootstrapBlazorTestBase
{
[Fact]
public void Reset_Ok()
{
var cut = Context.RenderComponent<LookupFilter>(pb =>
{
pb.Add(a => a.Type, typeof(string));
pb.Add(a => a.Lookup, new List<SelectedItem>()
{
new SelectedItem("true", "True"),
new SelectedItem("false", "False")
});
});
var filter = cut.Instance;
cut.InvokeAsync(() => filter.Reset());
}
[Fact]
public void GetFilterConditions_Ok()
{
var cut = Context.RenderComponent<LookupFilter>(pb =>
{
pb.Add(a => a.Type, typeof(string));
pb.Add(a => a.Lookup, new List<SelectedItem>()
{
new SelectedItem("true", "True"),
new SelectedItem("false", "False")
});
});
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 InvalidOperationException_Exception()
{
Assert.ThrowsAny<InvalidOperationException>(() => Context.RenderComponent<LookupFilter>());
Assert.ThrowsAny<InvalidOperationException>(() => Context.RenderComponent<LookupFilter>(pb =>
{
pb.Add(a => a.Lookup, new List<SelectedItem>());
}));
}
[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, bool>>(index++);
builder.AddAttribute(index++, nameof(TableColumn<Foo, bool>.Field), foo.Complete);
builder.AddAttribute(index++, nameof(TableColumn<Foo, bool>.FieldExpression), foo.GenerateValueExpression(nameof(foo.Complete), typeof(bool)));
builder.AddAttribute(index++, nameof(TableColumn<Foo, bool>.Filterable), true);
builder.AddAttribute(index++, nameof(TableColumn<Foo, bool>.Lookup), new List<SelectedItem>()
{
new SelectedItem("true", "True"),
new SelectedItem("false", "False")
});
builder.CloseComponent();
}));
});
});
var items = cut.FindAll(".dropdown-item");
IEnumerable<FilterKeyValueAction>? condtions = null;
cut.InvokeAsync(() => items[1].Click());
cut.InvokeAsync(() => condtions = cut.FindComponent<LookupFilter>().Instance.GetFilterConditions());
Assert.Single(condtions);
}
}

View File

@ -34,6 +34,12 @@ public class TableStringFilterTest : BootstrapBlazorTestBase
conditions = cut.Instance.GetFilterConditions();
Assert.Equal(2, conditions.Count());
// 测试 FilterLogicItem LogicChanged 代码覆盖率
var logicItem = cut.FindComponent<FilterLogicItem>();
var item = logicItem.FindAll(".dropdown-item")[0];
cut.InvokeAsync(() => item.Click());
Assert.Equal(FilterLogic.And, logicItem.Instance.Logic);
}
[Fact]
@ -66,4 +72,17 @@ public class TableStringFilterTest : BootstrapBlazorTestBase
cut.InvokeAsync(() => condtions = cut.FindComponent<StringFilter>().Instance.GetFilterConditions());
Assert.Single(condtions);
}
[Fact]
public void SearchFilterAction_Ok()
{
var searchFilterAction = new SearchFilterAction("Test-Search", "1", FilterAction.NotEqual);
var condtion = searchFilterAction.GetFilterConditions();
Assert.Single(condtion);
Assert.Equal("Test-Search", condtion.First().FieldKey);
Assert.Equal("1", condtion.First().FieldValue);
Assert.Equal(FilterAction.NotEqual, condtion.First().FilterAction);
Assert.Equal(FilterLogic.And, condtion.First().FilterLogic);
}
}