diff --git a/src/BootstrapBlazor/Components/Filters/DateTimeFilter.razor.cs b/src/BootstrapBlazor/Components/Filters/DateTimeFilter.razor.cs index daa3d8f6215d9d34779126d0737f777bbcab687e..6ee55b64a165856bbc5283298b44e8e8f582abfc 100644 --- a/src/BootstrapBlazor/Components/Filters/DateTimeFilter.razor.cs +++ b/src/BootstrapBlazor/Components/Filters/DateTimeFilter.razor.cs @@ -36,12 +36,12 @@ public partial class DateTimeFilter Items = new SelectedItem[] { - new SelectedItem("GreaterThanOrEqual", Localizer["GreaterThanOrEqual"]?.Value ?? "GreaterThanOrEqual"), - new SelectedItem("LessThanOrEqual", Localizer["LessThanOrEqual"]?.Value ?? "LessThanOrEqual"), - new SelectedItem("GreaterThan", Localizer["GreaterThan"]?.Value ?? "GreaterThan"), - new SelectedItem("LessThan", Localizer["LessThan"]?.Value ?? "LessThan"), - new SelectedItem("Equal", Localizer["Equal"]?.Value ?? "Equal"), - new SelectedItem("NotEqual", Localizer["NotEqual"]?.Value ?? "NotEqual") + new SelectedItem("GreaterThanOrEqual", Localizer["GreaterThanOrEqual"].Value), + new SelectedItem("LessThanOrEqual", Localizer["LessThanOrEqual"].Value), + new SelectedItem("GreaterThan", Localizer["GreaterThan"].Value), + new SelectedItem("LessThan", Localizer["LessThan"].Value), + new SelectedItem("Equal", Localizer["Equal"].Value), + new SelectedItem("NotEqual", Localizer["NotEqual"].Value ) }; } @@ -55,6 +55,7 @@ public partial class DateTimeFilter Action1 = FilterAction.GreaterThanOrEqual; Action2 = FilterAction.LessThanOrEqual; Count = 0; + Logic = FilterLogic.And; StateHasChanged(); } @@ -65,19 +66,26 @@ public partial class DateTimeFilter public override IEnumerable GetFilterConditions() { var filters = new List(); - if (Value1 != null) filters.Add(new FilterKeyValueAction() + if (Value1 != null) { - FieldKey = FieldKey, - FieldValue = Value1, - FilterAction = Action1 - }); - if (Count > 0 && Value2 != null) filters.Add(new FilterKeyValueAction() + filters.Add(new FilterKeyValueAction() + { + FieldKey = FieldKey, + FieldValue = Value1, + FilterAction = Action1 + }); + } + + if (Count > 0 && Value2 != null) { - FieldKey = FieldKey, - FieldValue = Value2, - FilterAction = Action2, - FilterLogic = Logic - }); + filters.Add(new FilterKeyValueAction() + { + FieldKey = FieldKey, + FieldValue = Value2, + FilterAction = Action2, + FilterLogic = Logic + }); + } return filters; } } diff --git a/test/UnitTest/Components/BoolFilterTest.cs b/test/UnitTest/Components/TableBoolFilterTest.cs similarity index 56% rename from test/UnitTest/Components/BoolFilterTest.cs rename to test/UnitTest/Components/TableBoolFilterTest.cs index 0d5273ab6f0156d6eb1ea1a94aaffb66c473e35c..e6a960f16857feb17a5a4bd2264c14cd9cf629b0 100644 --- a/test/UnitTest/Components/BoolFilterTest.cs +++ b/test/UnitTest/Components/TableBoolFilterTest.cs @@ -2,9 +2,12 @@ // 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 BoolFilterTest : BootstrapBlazorTestBase +public class TableBoolFilterTest : BootstrapBlazorTestBase { [Fact] public void Reset_Ok() @@ -34,29 +37,29 @@ public class BoolFilterTest : BootstrapBlazorTestBase [Fact] public void IsHeaderRow_OnSelectedItemChanged() { - var cut = Context.RenderComponent(); - var filter = cut.Instance; + 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.CloseComponent(); + })); + }); + }); + var filter = cut.FindComponent().Instance; var items = cut.FindAll(".dropdown-item"); IEnumerable? 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 - { - [nameof(TableFilter.IsHeaderRow)] = true - })); - } - } - } } diff --git a/test/UnitTest/Components/TableDateTimeFilterTest.cs b/test/UnitTest/Components/TableDateTimeFilterTest.cs new file mode 100644 index 0000000000000000000000000000000000000000..cdeb1e21f7ed658ee494483acd86d83862951f74 --- /dev/null +++ b/test/UnitTest/Components/TableDateTimeFilterTest.cs @@ -0,0 +1,117 @@ +// 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 Bunit; +using UnitTest.Extensions; + +namespace UnitTest.Components; + +public class TableDateTimeFilterTest : BootstrapBlazorTestBase +{ + [Fact] + public void Reset_Ok() + { + var cut = Context.RenderComponent(); + + var filter = cut.Instance; + cut.InvokeAsync(() => filter.Reset()); + } + [Fact] + public void GetFilterConditions_Ok() + { + var cut = Context.RenderComponent(); + + var filter = cut.Instance; + IEnumerable? condtions = null; + cut.InvokeAsync(() => condtions = filter.GetFilterConditions()); + Assert.Empty(condtions); + + // Set Value + var dt = cut.FindComponent>(); + cut.InvokeAsync(() => dt.Instance.SetValue(DateTime.Now)); + cut.InvokeAsync(() => condtions = filter.GetFilterConditions()); + Assert.Single(condtions); + } + + [Fact] + public void Count_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.Count, 2); + }); + + var logic = cut.FindComponent(); + Assert.NotNull(logic); + + var conditions = cut.Instance.GetFilterConditions(); + Assert.Empty(conditions); + + var dt = cut.FindComponent>().Instance; + cut.InvokeAsync(() => dt.SetValue(DateTime.Now)); + + conditions = cut.Instance.GetFilterConditions(); + Assert.Single(conditions); + + dt = cut.FindComponents>()[1].Instance; + cut.InvokeAsync(() => dt.SetValue(DateTime.Now)); + + conditions = cut.Instance.GetFilterConditions(); + Assert.Equal(2, conditions.Count()); + } + + [Fact] + public void Misc_Ok() + { + 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.DateTime); + builder.AddAttribute(index++, nameof(TableColumn.FieldExpression), foo.GenerateValueExpression(nameof(Foo.DateTime), typeof(DateTime?))); + builder.AddAttribute(index++, nameof(TableColumn.Filterable), true); + builder.CloseComponent(); + })); + }); + }); + var filter = cut.FindComponent(); + var dt = filter.FindComponent>(); + IEnumerable? condtions = null; + + // Click ToDay Cell + cut.InvokeAsync(() => + { + dt.Find(".current.today .cell").Click(); + dt.FindAll(".is-confirm")[1].Click(); + }); + + // OnFilterValueChanged + var filterButton = cut.FindComponent>(); + var logics = filterButton.FindAll(".dropdown-item"); + Assert.Equal(6, logics.Count); + cut.InvokeAsync(() => + { + logics[1].Click(); + condtions = filter.Instance.GetFilterConditions(); + }); + Assert.Single(condtions); + Assert.Equal(FilterAction.LessThanOrEqual, condtions?.First().FilterAction); + + // OnClearFilter + cut.InvokeAsync(() => + { + dt.Find(".is-confirm").Click(); + condtions = filter.Instance.GetFilterConditions(); + }); + Assert.Empty(condtions); + } +}