@@ -8,115 +8,85 @@ namespace UnitTest.Components;
88public class TableLookupFilterTest : BootstrapBlazorTestBase
99{
1010 [ Fact ]
11- public void Lookup_Ok ( )
11+ public async Task OnFilterAsync_Ok ( )
1212 {
13- var cut = Context . RenderComponent < LookupFilter > ( ) ;
14- var items = cut . FindAll ( ".dropdown-item" ) ;
15- Assert . Equal ( 3 , items . Count ) ;
16- Assert . Contains ( "LookupService-Test-2" , items [ items . Count - 1 ] . InnerHtml ) ;
17-
18- cut . WaitForAssertion ( ( ) =>
13+ var cut = Context . RenderComponent < TableColumnFilter > ( pb =>
1914 {
20- items = cut . FindAll ( ".dropdown-item" ) ;
21- Assert . Equal ( 3 , items . Count ) ;
22- Assert . Contains ( "LookupService-Test-2-async" , items [ items . Count - 1 ] . InnerHtml ) ;
15+ pb . Add ( a => a . Table , new MockTable ( ) ) ;
16+ pb . Add ( a => a . Column , new MockColumn ( ) ) ;
17+ pb . Add ( a => a . IsHeaderRow , true ) ;
2318 } ) ;
24- }
25-
26- [ Fact ]
27- public void Reset_Ok ( )
28- {
29- var cut = Context . RenderComponent < LookupFilter > ( ) ;
30-
31- var filter = cut . Instance ;
32- cut . InvokeAsync ( ( ) => filter . Reset ( ) ) ;
33- }
34-
35- [ Fact ]
36- public void GetFilterConditions_Ok ( )
37- {
38- var cut = Context . RenderComponent < LookupFilter > ( ) ;
3919
40- var filter = cut . Instance ;
41- var conditions = filter . GetFilterConditions ( ) ;
42- Assert . NotNull ( conditions . Filters ) ;
43- Assert . Empty ( conditions . Filters ) ;
44-
45- // Set Value
4620 var items = cut . FindAll ( ".dropdown-item" ) ;
47- cut . InvokeAsync ( ( ) => items [ 1 ] . Click ( ) ) ;
48- conditions = filter . GetFilterConditions ( ) ;
49- Assert . NotNull ( conditions . Filters ) ;
50- Assert . Single ( conditions . Filters ) ;
21+ await cut . InvokeAsync ( ( ) => { items [ 1 ] . Click ( ) ; } ) ;
5122 }
5223
5324 [ Fact ]
54- public void IsHeaderRow_OnFilterValueChanged ( )
25+ public async Task FilterAction_Ok ( )
5526 {
56- var cut = Context . RenderComponent < BootstrapBlazorRoot > ( pb =>
27+ var cut = Context . RenderComponent < TableColumnFilter > ( pb =>
5728 {
58- pb . AddChildContent < Table < Foo > > ( pb =>
59- {
60- pb . Add ( a => a . Items , new List < Foo > ( ) { new ( ) } ) ;
61- pb . Add ( a => a . RenderMode , TableRenderMode . Table ) ;
62- pb . Add ( a => a . ShowFilterHeader , true ) ;
63- pb . Add ( a => a . TableColumns , new RenderFragment < Foo > ( foo => builder =>
64- {
65- var index = 0 ;
66- builder . OpenComponent < TableColumn < Foo , bool > > ( index ++ ) ;
67- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , bool > . Field ) , foo . Complete ) ;
68- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , bool > . FieldExpression ) , foo . GenerateValueExpression ( nameof ( foo . Complete ) , typeof ( bool ) ) ) ;
69- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , bool > . Filterable ) , true ) ;
70- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , bool > . LookupStringComparison ) , StringComparison . OrdinalIgnoreCase ) ;
71- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , bool > . Lookup ) , new List < SelectedItem > ( )
72- {
73- new ( "true" , "True" ) ,
74- new ( "false" , "False" )
75- } ) ;
76- builder . CloseComponent ( ) ;
77- } ) ) ;
78- } ) ;
29+ pb . Add ( a => a . Table , new MockTable ( ) ) ;
30+ pb . Add ( a => a . Column , new MockColumn ( ) ) ;
7931 } ) ;
80-
81- var items = cut . FindAll ( ".dropdown-item" ) ;
82- cut . InvokeAsync ( ( ) => items [ 1 ] . Click ( ) ) ;
83- var conditions = cut . FindComponent < LookupFilter > ( ) . Instance . GetFilterConditions ( ) ;
84- Assert . NotNull ( conditions . Filters ) ;
85- Assert . Single ( conditions . Filters ) ;
86- }
87-
88- [ Fact ]
89- public void SetFilterConditions_Ok ( )
90- {
91- var cut = Context . RenderComponent < LookupFilter > ( ) ;
92-
93- var filter = cut . Instance ;
94- var conditions = filter . GetFilterConditions ( ) ;
95- Assert . NotNull ( conditions . Filters ) ;
96- Assert . Empty ( conditions . Filters ) ;
32+ var lookup = cut . FindComponent < LookupFilter > ( ) ;
33+ var filter = lookup . Instance ;
9734
9835 var newConditions = new FilterKeyValueAction ( )
9936 {
100- Filters = [ new FilterKeyValueAction ( ) { FieldValue = true } ]
37+ Filters =
38+ [
39+ new FilterKeyValueAction ( ) { FieldValue = "1" } ,
40+ ]
10141 } ;
102- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
103- conditions = filter . GetFilterConditions ( ) ;
104- Assert . NotNull ( conditions . Filters ) ;
42+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
43+ var conditions = filter . GetFilterConditions ( ) ;
10544 Assert . Single ( conditions . Filters ) ;
45+ await cut . InvokeAsync ( ( ) => filter . Reset ( ) ) ;
46+ conditions = filter . GetFilterConditions ( ) ;
47+ Assert . Empty ( conditions . Filters ) ;
10648
49+ // Improve test coverage
10750 newConditions = new FilterKeyValueAction ( )
10851 {
109- Filters = [ new FilterKeyValueAction ( ) { FieldValue = null } ]
52+ Filters =
53+ [
54+ new FilterKeyValueAction ( ) { FieldValue = false } ,
55+ ]
11056 } ;
111- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
57+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
11258 conditions = filter . GetFilterConditions ( ) ;
113- Assert . NotNull ( conditions . Filters ) ;
11459 Assert . Empty ( conditions . Filters ) ;
11560
116- newConditions = new FilterKeyValueAction ( ) { FieldValue = true } ;
117- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
61+ newConditions = new FilterKeyValueAction ( ) { FieldValue = null } ;
62+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
11863 conditions = filter . GetFilterConditions ( ) ;
119- Assert . NotNull ( conditions . Filters ) ;
120- Assert . Single ( conditions . Filters ) ;
64+ Assert . Empty ( conditions . Filters ) ;
65+ }
66+
67+ class MockTable : ITable
68+ {
69+ public Dictionary < string , IFilterAction > Filters { get ; set ; } = [ ] ;
70+
71+ public Func < Task > ? OnFilterAsync { get ; set ; }
72+
73+ public List < ITableColumn > Columns => [ ] ;
74+
75+ public IEnumerable < ITableColumn > GetVisibleColumns ( ) => Columns ;
76+ }
77+
78+ class MockColumn : TableColumn < Foo , string >
79+ {
80+ public MockColumn ( )
81+ {
82+ PropertyType = typeof ( string ) ;
83+ FieldName = "Lookup" ;
84+ Lookup = new List < SelectedItem > ( )
85+ {
86+ new ( "1" , "Test-1" ) ,
87+ new ( "2" , "Test-2" ) ,
88+ new ( "3" , "Test-3" )
89+ } ;
90+ }
12191 }
12292}
0 commit comments