|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +namespace UnitTest.Components; |
| 7 | + |
| 8 | +public class TableBoolFilterTest : BootstrapBlazorTestBase |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public async Task OnFilterAsync_Ok() |
| 12 | + { |
| 13 | + var cut = Context.RenderComponent<TableColumnFilter>(pb => |
| 14 | + { |
| 15 | + pb.Add(a => a.Table, new MockTable()); |
| 16 | + pb.Add(a => a.Column, new MockColumn()); |
| 17 | + pb.Add(a => a.IsHeaderRow, true); |
| 18 | + }); |
| 19 | + |
| 20 | + var items = cut.FindAll(".dropdown-item"); |
| 21 | + await cut.InvokeAsync(() => { items[1].Click(); }); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task FilterAction_Ok() |
| 26 | + { |
| 27 | + var cut = Context.RenderComponent<BoolFilter>(); |
| 28 | + var filter = cut.Instance; |
| 29 | + |
| 30 | + var newConditions = new FilterKeyValueAction() |
| 31 | + { |
| 32 | + Filters = |
| 33 | + [ |
| 34 | + new FilterKeyValueAction() { FieldValue = true }, |
| 35 | + ] |
| 36 | + }; |
| 37 | + await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions)); |
| 38 | + |
| 39 | + var conditions = filter.GetFilterConditions(); |
| 40 | + newConditions = new FilterKeyValueAction() |
| 41 | + { |
| 42 | + Filters = |
| 43 | + [ |
| 44 | + new FilterKeyValueAction() { FieldValue = false }, |
| 45 | + ] |
| 46 | + }; |
| 47 | + await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions)); |
| 48 | + Assert.Single(conditions.Filters); |
| 49 | + |
| 50 | + await cut.InvokeAsync(() => filter.Reset()); |
| 51 | + conditions = filter.GetFilterConditions(); |
| 52 | + Assert.Empty(conditions.Filters); |
| 53 | + |
| 54 | + // Improve test coverage |
| 55 | + newConditions = new FilterKeyValueAction() |
| 56 | + { |
| 57 | + Filters = |
| 58 | + [ |
| 59 | + new FilterKeyValueAction() { FieldValue = null }, |
| 60 | + ] |
| 61 | + }; |
| 62 | + await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions)); |
| 63 | + conditions = filter.GetFilterConditions(); |
| 64 | + Assert.Empty(conditions.Filters); |
| 65 | + |
| 66 | + newConditions = new FilterKeyValueAction() { FieldValue = "1" }; |
| 67 | + await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions)); |
| 68 | + conditions = filter.GetFilterConditions(); |
| 69 | + Assert.Empty(conditions.Filters); |
| 70 | + } |
| 71 | + |
| 72 | + class MockTable : ITable |
| 73 | + { |
| 74 | + public Dictionary<string, IFilterAction> Filters { get; set; } = []; |
| 75 | + |
| 76 | + public Func<Task>? OnFilterAsync { get; set; } |
| 77 | + |
| 78 | + public List<ITableColumn> Columns => []; |
| 79 | + |
| 80 | + public IEnumerable<ITableColumn> GetVisibleColumns() => Columns; |
| 81 | + } |
| 82 | + |
| 83 | + class MockColumn : TableColumn<Foo, EnumEducation> |
| 84 | + { |
| 85 | + public MockColumn() |
| 86 | + { |
| 87 | + PropertyType = typeof(bool); |
| 88 | + FieldName = "Complete"; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments