|
| 1 | +using Bunit; |
| 2 | +using Microsoft.AspNetCore.Components; |
| 3 | +using Microsoft.AspNetCore.Components.Web; |
| 4 | +using SoPro.FancyTable.Components; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +public sealed class FancyTreeTableFilterTests : BunitContext |
| 8 | +{ |
| 9 | + private sealed class Node(string name, params Node[] children) |
| 10 | + { |
| 11 | + public string Name { get; } = name; |
| 12 | + public IReadOnlyList<Node> Children { get; } = children; |
| 13 | + } |
| 14 | + |
| 15 | + private static readonly List<Node> Roots = |
| 16 | + [ |
| 17 | + new Node("Root A", new Node("Child A1", new Node("Target"))), |
| 18 | + new Node("Root B", new Node("Child B1")) |
| 19 | + ]; |
| 20 | + |
| 21 | + public FancyTreeTableFilterTests() |
| 22 | + { |
| 23 | + JSInterop.Mode = JSRuntimeMode.Loose; |
| 24 | + } |
| 25 | + |
| 26 | + private static List<FancyColumn<Node>> Columns(bool hideableName = false) |
| 27 | + { |
| 28 | + return |
| 29 | + [ |
| 30 | + new FancyColumn<Node> { Key = "name", Title = "Name", ValueSelector = n => n.Name, Filterable = true, Hideable = hideableName } |
| 31 | + ]; |
| 32 | + } |
| 33 | + |
| 34 | + private IRenderedComponent<FancyTreeTable<Node>> RenderTreeTable( |
| 35 | + IReadOnlyList<FancyColumnFilter>? initial = null, |
| 36 | + Action<IReadOnlyList<FancyColumnFilter>>? onChanged = null, |
| 37 | + bool paginate = false, |
| 38 | + int currentPage = 1, |
| 39 | + Action<int>? onPageChanged = null, |
| 40 | + Func<Node, string, bool>? searchPredicate = null, |
| 41 | + bool enableReordering = false) |
| 42 | + { |
| 43 | + return Render<FancyTreeTable<Node>>(parameters => |
| 44 | + { |
| 45 | + parameters |
| 46 | + .Add(p => p.Items, Roots) |
| 47 | + .Add(p => p.Columns, Columns()) |
| 48 | + .Add(p => p.ChildItemsSelector, n => n.Children) |
| 49 | + .Add(p => p.EnableColumnReordering, enableReordering) |
| 50 | + .Add(p => p.InitialColumnFilters, initial) |
| 51 | + .Add(p => p.ColumnFiltersChanged, EventCallback.Factory.Create<IReadOnlyList<FancyColumnFilter>>(this, f => onChanged?.Invoke(f))) |
| 52 | + .Add(p => p.SearchPredicate, searchPredicate); |
| 53 | + if (paginate) |
| 54 | + { |
| 55 | + parameters |
| 56 | + .Add(p => p.PaginationEnabled, true) |
| 57 | + .Add(p => p.PageSize, 1) |
| 58 | + .Add(p => p.CurrentPage, currentPage) |
| 59 | + .Add(p => p.CurrentPageChanged, EventCallback.Factory.Create<int>(this, page => onPageChanged?.Invoke(page))); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private static void ApplyFilter(IRenderedComponent<FancyTreeTable<Node>> cut, string key, string operatorName, string? value) |
| 65 | + { |
| 66 | + string th = $"th[data-column-key='{key}']"; |
| 67 | + cut.Find($"{th} button.fancy-filter-button").Click(); |
| 68 | + cut.Find($"{th} select").Change(operatorName); |
| 69 | + if (value is not null) |
| 70 | + { |
| 71 | + cut.Find($"{th} input").Input(value); |
| 72 | + } |
| 73 | + |
| 74 | + cut.Find($"{th} button.btn-primary").Click(); |
| 75 | + } |
| 76 | + |
| 77 | + private static string[] RowTexts(IRenderedComponent<FancyTreeTable<Node>> cut) => |
| 78 | + cut.FindAll("tbody tr").Select(row => row.TextContent.Trim()).ToArray(); |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void FilterOnly_NoSearch_ShowsMatchWithAncestors() |
| 82 | + { |
| 83 | + IRenderedComponent<FancyTreeTable<Node>> cut = RenderTreeTable( |
| 84 | + initial: [new FancyColumnFilter("name", FancyColumnFilterOperator.Equals, "Target")]); |
| 85 | + |
| 86 | + string[] rows = RowTexts(cut); |
| 87 | + |
| 88 | + Assert.Equal(3, rows.Length); |
| 89 | + Assert.Contains("Root A", rows[0]); |
| 90 | + Assert.Contains("Child A1", rows[1]); |
| 91 | + Assert.Contains("Target", rows[2]); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void FilterAndSearch_NodeMustSatisfyBoth() |
| 96 | + { |
| 97 | + IRenderedComponent<FancyTreeTable<Node>> cut = RenderTreeTable( |
| 98 | + initial: [new FancyColumnFilter("name", FancyColumnFilterOperator.Contains, "Child")]); |
| 99 | + Assert.Equal(4, RowTexts(cut).Length); |
| 100 | + |
| 101 | + cut.Find("input[placeholder]").Input("B1"); |
| 102 | + cut.Find("input[placeholder]").KeyUp(new KeyboardEventArgs { Key = "Enter" }); |
| 103 | + |
| 104 | + string[] rows = RowTexts(cut); |
| 105 | + Assert.Equal(2, rows.Length); |
| 106 | + Assert.Contains("Root B", rows[0]); |
| 107 | + Assert.Contains("Child B1", rows[1]); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void CustomSearchPredicate_NotCalledWhenOnlyFiltersActive() |
| 112 | + { |
| 113 | + int calls = 0; |
| 114 | + IRenderedComponent<FancyTreeTable<Node>> cut = RenderTreeTable(searchPredicate: (_, _) => |
| 115 | + { |
| 116 | + calls++; |
| 117 | + return true; |
| 118 | + }); |
| 119 | + |
| 120 | + ApplyFilter(cut, "name", "Equals", "Target"); |
| 121 | + |
| 122 | + Assert.Equal(3, RowTexts(cut).Length); |
| 123 | + Assert.Equal(0, calls); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void Apply_RaisesChangedAndResetsPage() |
| 128 | + { |
| 129 | + IReadOnlyList<FancyColumnFilter>? received = null; |
| 130 | + List<int> pages = []; |
| 131 | + IRenderedComponent<FancyTreeTable<Node>> cut = RenderTreeTable( |
| 132 | + onChanged: f => received = f, |
| 133 | + paginate: true, |
| 134 | + currentPage: 2, |
| 135 | + onPageChanged: pages.Add); |
| 136 | + Assert.Contains("Root B", cut.Find("tbody tr").TextContent); |
| 137 | + |
| 138 | + ApplyFilter(cut, "name", "Equals", "Target"); |
| 139 | + |
| 140 | + Assert.Equal([new FancyColumnFilter("name", FancyColumnFilterOperator.Equals, "Target")], received); |
| 141 | + Assert.Equal([1], pages); |
| 142 | + Assert.Contains("Root A", cut.Find("tbody tr").TextContent); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public void Clear_RestoresExpandedView() |
| 147 | + { |
| 148 | + IReadOnlyList<FancyColumnFilter>? received = null; |
| 149 | + IRenderedComponent<FancyTreeTable<Node>> cut = RenderTreeTable( |
| 150 | + initial: [new FancyColumnFilter("name", FancyColumnFilterOperator.Equals, "Target")], |
| 151 | + onChanged: f => received = f); |
| 152 | + Assert.Equal(3, RowTexts(cut).Length); |
| 153 | + |
| 154 | + cut.Find("th[data-column-key='name'] button.fancy-filter-button").Click(); |
| 155 | + cut.Find("th[data-column-key='name'] button.btn-outline-secondary").Click(); |
| 156 | + |
| 157 | + Assert.NotNull(received); |
| 158 | + Assert.Empty(received); |
| 159 | + string[] rows = RowTexts(cut); |
| 160 | + Assert.Equal(2, rows.Length); |
| 161 | + Assert.Contains("Root A", rows[0]); |
| 162 | + Assert.Contains("Root B", rows[1]); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void PinnedTreeColumn_CanBeFiltered() |
| 167 | + { |
| 168 | + IRenderedComponent<FancyTreeTable<Node>> cut = RenderTreeTable(enableReordering: true); |
| 169 | + |
| 170 | + Assert.Single(cut.FindAll("th[data-column-key='name'] button.fancy-filter-button")); |
| 171 | + } |
| 172 | + |
| 173 | + [Fact] |
| 174 | + public void HiddenFilteredColumn_RestoreButtonShowsFunnel() |
| 175 | + { |
| 176 | + IRenderedComponent<FancyTreeTable<Node>> cut = Render<FancyTreeTable<Node>>(parameters => parameters |
| 177 | + .Add(p => p.Items, Roots) |
| 178 | + .Add(p => p.Columns, |
| 179 | + [ |
| 180 | + new FancyColumn<Node> { Key = "name", Title = "Name", ValueSelector = n => n.Name }, |
| 181 | + new FancyColumn<Node> { Key = "other", Title = "Other", ValueSelector = n => n.Name, Filterable = true, Hideable = true } |
| 182 | + ]) |
| 183 | + .Add(p => p.ChildItemsSelector, n => n.Children) |
| 184 | + .Add(p => p.InitialColumnFilters, [new FancyColumnFilter("other", FancyColumnFilterOperator.Equals, "Target")])); |
| 185 | + |
| 186 | + cut.Find("th[data-column-key='other'] button:has(i.bi-eye-slash)").Click(); |
| 187 | + |
| 188 | + Assert.Single(cut.FindAll("button:has(i.bi-eye) i.bi-funnel-fill")); |
| 189 | + } |
| 190 | +} |
0 commit comments