diff --git a/src/BootstrapBlazor/Components/Filters/FilterLogicItem.razor.cs b/src/BootstrapBlazor/Components/Filters/FilterLogicItem.razor.cs index 99a09b57e..7b6c9fb51 100644 --- a/src/BootstrapBlazor/Components/Filters/FilterLogicItem.razor.cs +++ b/src/BootstrapBlazor/Components/Filters/FilterLogicItem.razor.cs @@ -53,9 +53,9 @@ public partial class FilterLogicItem base.OnInitialized(); Items = new List() - { - 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) + }; } } diff --git a/src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs b/src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs index 952a22ba3..387dfc25f 100644 --- a/src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs +++ b/src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs @@ -16,15 +16,12 @@ public partial class LookupFilter private List Items { get; } = new List(); - /// - /// 内部使用 - /// - [NotNull] - private Type? EnumType { get; set; } - /// /// 获得/设置 相关枚举类型 /// +#if NET6_0_OR_GREATER + [EditorRequired] +#endif [Parameter] [NotNull] @@ -33,6 +30,9 @@ public partial class LookupFilter /// /// 获得/设置 相关枚举类型 /// +#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); } diff --git a/src/BootstrapBlazor/Components/Filters/SearchFilterAction.cs b/src/BootstrapBlazor/Components/Filters/SearchFilterAction.cs index de7769672..86e767f65 100644 --- a/src/BootstrapBlazor/Components/Filters/SearchFilterAction.cs +++ b/src/BootstrapBlazor/Components/Filters/SearchFilterAction.cs @@ -42,12 +42,12 @@ public class SearchFilterAction : IFilterAction /// /// public virtual IEnumerable GetFilterConditions() => new List() + { + new() { - new() - { - FieldKey = Name, - FieldValue = Value, - FilterAction = Action, - } - }; + FieldKey = Name, + FieldValue = Value, + FilterAction = Action, + } + }; } diff --git a/test/UnitTest/Components/TableLookupFilterTest.cs b/test/UnitTest/Components/TableLookupFilterTest.cs new file mode 100644 index 000000000..5f6cc41af --- /dev/null +++ b/test/UnitTest/Components/TableLookupFilterTest.cs @@ -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(pb => + { + pb.Add(a => a.Type, typeof(string)); + pb.Add(a => a.Lookup, new List() + { + 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(pb => + { + pb.Add(a => a.Type, typeof(string)); + pb.Add(a => a.Lookup, new List() + { + new SelectedItem("true", "True"), + new SelectedItem("false", "False") + }); + }); + + var filter = cut.Instance; + IEnumerable? 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(() => Context.RenderComponent()); + Assert.ThrowsAny(() => Context.RenderComponent(pb => + { + pb.Add(a => a.Lookup, new List()); + })); + } + + [Fact] + public void IsHeaderRow_OnFilterValueChanged() + { + var cut = Context.RenderComponent(pb => + { + pb.AddChildContent>(pb => + { + pb.Add(a => a.Items, new List() { new Foo() }); + pb.Add(a => a.RenderMode, TableRenderMode.Table); + pb.Add(a => a.ShowFilterHeader, true); + pb.Add(a => a.TableColumns, new RenderFragment(foo => builder => + { + var index = 0; + builder.OpenComponent>(index++); + builder.AddAttribute(index++, nameof(TableColumn.Field), foo.Complete); + builder.AddAttribute(index++, nameof(TableColumn.FieldExpression), foo.GenerateValueExpression(nameof(foo.Complete), typeof(bool))); + builder.AddAttribute(index++, nameof(TableColumn.Filterable), true); + builder.AddAttribute(index++, nameof(TableColumn.Lookup), new List() + { + new SelectedItem("true", "True"), + new SelectedItem("false", "False") + }); + builder.CloseComponent(); + })); + }); + }); + + var items = cut.FindAll(".dropdown-item"); + IEnumerable? condtions = null; + cut.InvokeAsync(() => items[1].Click()); + cut.InvokeAsync(() => condtions = cut.FindComponent().Instance.GetFilterConditions()); + Assert.Single(condtions); + } +} diff --git a/test/UnitTest/Components/TableStringFilterTest.cs b/test/UnitTest/Components/TableStringFilterTest.cs index 5b1a04175..edd7c5009 100644 --- a/test/UnitTest/Components/TableStringFilterTest.cs +++ b/test/UnitTest/Components/TableStringFilterTest.cs @@ -34,6 +34,12 @@ public class TableStringFilterTest : BootstrapBlazorTestBase conditions = cut.Instance.GetFilterConditions(); Assert.Equal(2, conditions.Count()); + + // 测试 FilterLogicItem LogicChanged 代码覆盖率 + var logicItem = cut.FindComponent(); + 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().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); + } }