|
| 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 FancyTableFilterTests : BunitContext |
| 8 | +{ |
| 9 | + private sealed record Person(int Id, string Name, string City); |
| 10 | + |
| 11 | + private static readonly List<Person> People = Enumerable.Range(1, 30) |
| 12 | + .Select(i => new Person(i, i % 2 == 0 ? $"Anna {i}" : $"Bob {i}", i % 3 == 0 ? "Berlin" : "Hamburg")) |
| 13 | + .ToList(); |
| 14 | + |
| 15 | + public FancyTableFilterTests() |
| 16 | + { |
| 17 | + JSInterop.Mode = JSRuntimeMode.Loose; |
| 18 | + } |
| 19 | + |
| 20 | + private static List<FancyColumn<Person>> Columns(bool filterable = true, bool hideableCity = false) |
| 21 | + { |
| 22 | + return |
| 23 | + [ |
| 24 | + new FancyColumn<Person> { Key = "name", Title = "Name", ValueSelector = p => p.Name, Filterable = filterable }, |
| 25 | + new FancyColumn<Person> { Key = "city", Title = "City", ValueSelector = p => p.City, Filterable = filterable, Hideable = hideableCity }, |
| 26 | + new FancyColumn<Person> { Key = "id", Title = "Id", ValueSelector = p => p.Id } |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + private IRenderedComponent<FancyTable<Person>> RenderTable( |
| 31 | + List<FancyColumn<Person>>? columns = null, |
| 32 | + IReadOnlyList<FancyColumnFilter>? initial = null, |
| 33 | + Action<IReadOnlyList<FancyColumnFilter>>? onChanged = null, |
| 34 | + bool paginate = false, |
| 35 | + int currentPage = 1, |
| 36 | + Action<int>? onPageChanged = null, |
| 37 | + Func<Person, string, bool>? searchPredicate = null) |
| 38 | + { |
| 39 | + return Render<FancyTable<Person>>(parameters => |
| 40 | + { |
| 41 | + parameters |
| 42 | + .Add(p => p.Items, People) |
| 43 | + .Add(p => p.Columns, columns ?? Columns()) |
| 44 | + .Add(p => p.InitialColumnFilters, initial) |
| 45 | + .Add(p => p.ColumnFiltersChanged, EventCallback.Factory.Create<IReadOnlyList<FancyColumnFilter>>(this, f => onChanged?.Invoke(f))) |
| 46 | + .Add(p => p.SearchPredicate, searchPredicate); |
| 47 | + if (paginate) |
| 48 | + { |
| 49 | + parameters |
| 50 | + .Add(p => p.PaginationEnabled, true) |
| 51 | + .Add(p => p.PageSize, 10) |
| 52 | + .Add(p => p.CurrentPage, currentPage) |
| 53 | + .Add(p => p.CurrentPageChanged, EventCallback.Factory.Create<int>(this, page => onPageChanged?.Invoke(page))); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + private static void ApplyFilter(IRenderedComponent<FancyTable<Person>> cut, string key, string operatorName, string? value) |
| 59 | + { |
| 60 | + string th = $"th[data-column-key='{key}']"; |
| 61 | + cut.Find($"{th} button.fancy-filter-button").Click(); |
| 62 | + cut.Find($"{th} select").Change(operatorName); |
| 63 | + if (value is not null) |
| 64 | + { |
| 65 | + cut.Find($"{th} input").Input(value); |
| 66 | + } |
| 67 | + |
| 68 | + cut.Find($"{th} button.btn-primary").Click(); |
| 69 | + } |
| 70 | + |
| 71 | + private static int RowCount(IRenderedComponent<FancyTable<Person>> cut) => cut.FindAll("tbody tr").Count; |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void NonFilterableColumn_RendersNoFunnel() |
| 75 | + { |
| 76 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable(); |
| 77 | + |
| 78 | + Assert.Empty(cut.FindAll("th[data-column-key='id'] button.fancy-filter-button")); |
| 79 | + Assert.Single(cut.FindAll("th[data-column-key='name'] button.fancy-filter-button")); |
| 80 | + Assert.Single(cut.FindAll("th[data-column-key='city'] button.fancy-filter-button")); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void NoFilterableColumns_MarkupHasNoFilterPopup() |
| 85 | + { |
| 86 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable(Columns(filterable: false)); |
| 87 | + |
| 88 | + Assert.Empty(cut.FindAll("button.fancy-filter-button")); |
| 89 | + Assert.DoesNotContain("bi-funnel", cut.Markup); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void InitialColumnFilters_AreApplied() |
| 94 | + { |
| 95 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable( |
| 96 | + initial: [new FancyColumnFilter("city", FancyColumnFilterOperator.Equals, "Berlin")]); |
| 97 | + |
| 98 | + Assert.Equal(10, RowCount(cut)); |
| 99 | + Assert.All(cut.FindAll("tbody tr"), row => Assert.Contains("Berlin", row.TextContent)); |
| 100 | + Assert.Single(cut.FindAll("th[data-column-key='city'] i.bi-funnel-fill")); |
| 101 | + Assert.Empty(cut.FindAll("th[data-column-key='name'] i.bi-funnel-fill")); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void Apply_FiltersRowsAndRaisesChangedWithFullOrderedList() |
| 106 | + { |
| 107 | + IReadOnlyList<FancyColumnFilter>? received = null; |
| 108 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable( |
| 109 | + initial: [new FancyColumnFilter("city", FancyColumnFilterOperator.Equals, "Berlin")], |
| 110 | + onChanged: f => received = f); |
| 111 | + |
| 112 | + ApplyFilter(cut, "name", "Contains", "a"); |
| 113 | + |
| 114 | + Assert.Equal( |
| 115 | + [ |
| 116 | + new FancyColumnFilter("name", FancyColumnFilterOperator.Contains, "a"), |
| 117 | + new FancyColumnFilter("city", FancyColumnFilterOperator.Equals, "Berlin") |
| 118 | + ], |
| 119 | + received); |
| 120 | + Assert.Equal(5, RowCount(cut)); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void Clear_RemovesFilterAndRaisesChanged() |
| 125 | + { |
| 126 | + IReadOnlyList<FancyColumnFilter>? received = null; |
| 127 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable( |
| 128 | + initial: [new FancyColumnFilter("city", FancyColumnFilterOperator.Equals, "Berlin")], |
| 129 | + onChanged: f => received = f); |
| 130 | + |
| 131 | + cut.Find("th[data-column-key='city'] button.fancy-filter-button").Click(); |
| 132 | + cut.Find("th[data-column-key='city'] button.btn-outline-secondary").Click(); |
| 133 | + |
| 134 | + Assert.NotNull(received); |
| 135 | + Assert.Empty(received); |
| 136 | + Assert.Equal(30, RowCount(cut)); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void FilterChange_ResetsToFirstPage() |
| 141 | + { |
| 142 | + List<int> pages = []; |
| 143 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable(paginate: true, currentPage: 3, onPageChanged: pages.Add); |
| 144 | + Assert.Contains("Bob 21", cut.Find("tbody tr").TextContent); |
| 145 | + |
| 146 | + ApplyFilter(cut, "name", "Contains", "anna"); |
| 147 | + |
| 148 | + Assert.Equal([1], pages); |
| 149 | + Assert.Contains("Anna 2", cut.Find("tbody tr").TextContent); |
| 150 | + Assert.Equal(10, RowCount(cut)); |
| 151 | + Assert.Contains("15", cut.Find(".fancy-pagination-summary").TextContent); |
| 152 | + } |
| 153 | + |
| 154 | + [Fact] |
| 155 | + public void FiltersCombineWithSearch() |
| 156 | + { |
| 157 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable( |
| 158 | + initial: [new FancyColumnFilter("city", FancyColumnFilterOperator.Equals, "Berlin")]); |
| 159 | + |
| 160 | + cut.Find("input[placeholder]").Input("Anna"); |
| 161 | + cut.Find("input[placeholder]").KeyUp(new KeyboardEventArgs { Key = "Enter" }); |
| 162 | + |
| 163 | + Assert.Equal(5, RowCount(cut)); |
| 164 | + } |
| 165 | + |
| 166 | + [Fact] |
| 167 | + public void CustomSearchPredicate_NotCalledWhenOnlyFiltersActive() |
| 168 | + { |
| 169 | + int calls = 0; |
| 170 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable(searchPredicate: (_, _) => |
| 171 | + { |
| 172 | + calls++; |
| 173 | + return true; |
| 174 | + }); |
| 175 | + |
| 176 | + ApplyFilter(cut, "city", "Equals", "Berlin"); |
| 177 | + |
| 178 | + Assert.Equal(10, RowCount(cut)); |
| 179 | + Assert.Equal(0, calls); |
| 180 | + } |
| 181 | + |
| 182 | + [Fact] |
| 183 | + public void HiddenFilteredColumn_RestoreButtonShowsFunnel() |
| 184 | + { |
| 185 | + IRenderedComponent<FancyTable<Person>> cut = RenderTable( |
| 186 | + Columns(hideableCity: true), |
| 187 | + initial: [new FancyColumnFilter("city", FancyColumnFilterOperator.Equals, "Berlin")]); |
| 188 | + |
| 189 | + cut.Find("th[data-column-key='city'] button:has(i.bi-eye-slash)").Click(); |
| 190 | + |
| 191 | + Assert.Single(cut.FindAll("button:has(i.bi-eye) i.bi-funnel-fill")); |
| 192 | + Assert.Equal(10, RowCount(cut)); |
| 193 | + } |
| 194 | +} |
0 commit comments