@@ -8,182 +8,80 @@ namespace UnitTest.Components;
88public class TableEnumFilterTest : BootstrapBlazorTestBase
99{
1010 [ Fact ]
11- public void Reset_Ok ( )
11+ public void Type_Ok ( )
1212 {
13- var cut = Context . RenderComponent < EnumFilter > ( pb =>
14- {
15- pb . Add ( a => a . Type , typeof ( EnumEducation ) ) ;
16- } ) ;
17-
18- var filter = cut . Instance ;
19- cut . InvokeAsync ( ( ) => filter . Reset ( ) ) ;
20- }
21-
22- [ Fact ]
23- public void GetFilterConditions_Ok ( )
24- {
25- var cut = Context . RenderComponent < EnumFilter > ( pb =>
26- {
27- pb . Add ( a => a . Type , typeof ( EnumEducation ) ) ;
28- } ) ;
29-
30- var filter = cut . Instance ;
31- var conditions = filter . GetFilterConditions ( ) ;
32- Assert . NotNull ( conditions . Filters ) ;
33- Assert . Empty ( conditions . Filters ) ;
34-
35- // Set Value
36- var items = cut . FindAll ( ".dropdown-item" ) ;
37- cut . InvokeAsync ( ( ) => items [ 1 ] . Click ( ) ) ;
38- conditions = filter . GetFilterConditions ( ) ;
39- Assert . NotNull ( conditions . Filters ) ;
40- Assert . Single ( conditions . Filters ) ;
41- }
13+ Assert . ThrowsAny < InvalidOperationException > ( ( ) => Context . RenderComponent < EnumFilter > ( ) ) ;
4214
43- [ Fact ]
44- public async Task Count_Ok ( )
45- {
46- var cut = Context . RenderComponent < EnumFilter > ( pb =>
15+ var cut = Context . RenderComponent < TableColumnFilter > ( pb =>
4716 {
48- pb . Add ( a => a . Type , typeof ( EnumEducation ) ) ;
17+ pb . Add ( a => a . Column , new MockColumn ( ) ) ;
4918 } ) ;
50-
51- var logic = cut . FindComponent < FilterLogicItem > ( ) ;
52- Assert . NotNull ( logic ) ;
53-
54- var filter = cut . Instance . GetFilterConditions ( ) ;
55- Assert . NotNull ( filter . Filters ) ;
56- Assert . Empty ( filter . Filters ) ;
57-
58- var com = cut . FindComponent < Select < string ? > > ( ) . Instance ;
59- await cut . InvokeAsync ( ( ) => com . SetValue ( "Middle" ) ) ;
60-
61- filter = cut . Instance . GetFilterConditions ( ) ;
62- Assert . NotNull ( filter . Filters ) ;
63- Assert . Single ( filter . Filters ) ;
64-
65- com = cut . FindComponents < Select < string ? > > ( ) [ 1 ] . Instance ;
66- await cut . InvokeAsync ( ( ) => com . SetValue ( "Primary" ) ) ;
67-
68- filter = cut . Instance . GetFilterConditions ( ) ;
69- Assert . NotNull ( filter . Filters ) ;
70- Assert . Equal ( 2 , filter . Filters . Count ) ;
71- }
72-
73- [ Fact ]
74- public void InvalidOperationException_Exception ( )
75- {
76- Assert . ThrowsAny < InvalidOperationException > ( ( ) => Context . RenderComponent < EnumFilter > ( ) ) ;
19+ var filter = cut . FindComponent < EnumFilter > ( ) ;
20+ Assert . Equal ( typeof ( EnumEducation ) , filter . Instance . Type ) ;
7721 }
7822
7923 [ Fact ]
80- public void SetFilterConditions_Ok ( )
24+ public async Task FilterAction_Ok ( )
8125 {
8226 var cut = Context . RenderComponent < EnumFilter > ( pb =>
8327 {
8428 pb . Add ( a => a . Type , typeof ( EnumEducation ) ) ;
8529 } ) ;
8630 var filter = cut . Instance ;
87- var conditions = filter . GetFilterConditions ( ) ;
88- Assert . NotNull ( conditions . Filters ) ;
89- Assert . Empty ( conditions . Filters ) ;
9031
9132 var newConditions = new FilterKeyValueAction ( )
9233 {
93- Filters = [ new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Middle } ]
34+ Filters =
35+ [
36+ new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Primary } ,
37+ new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Middle }
38+ ]
9439 } ;
95- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
40+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
41+ var conditions = filter . GetFilterConditions ( ) ;
42+ Assert . Equal ( 2 , conditions . Filters . Count ) ;
9643
44+ await cut . InvokeAsync ( ( ) => filter . Reset ( ) ) ;
9745 conditions = filter . GetFilterConditions ( ) ;
98- Assert . NotNull ( conditions . Filters ) ;
99- Assert . Equal ( EnumEducation . Middle , conditions . Filters . First ( ) . FieldValue ) ;
46+ Assert . Empty ( conditions . Filters ) ;
10047
48+ // Improve test coverage
10149 newConditions = new FilterKeyValueAction ( )
10250 {
103- Filters = [ new FilterKeyValueAction ( ) { FieldValue = null } ]
51+ Filters =
52+ [
53+ new FilterKeyValueAction ( ) { FieldValue = null } ,
54+ new FilterKeyValueAction ( ) { FieldValue = null }
55+ ]
10456 } ;
105- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
57+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
10658 conditions = filter . GetFilterConditions ( ) ;
107- Assert . NotNull ( conditions . Filters ) ;
10859 Assert . Empty ( conditions . Filters ) ;
10960
110- newConditions = new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Middle } ;
111- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
112- conditions = filter . GetFilterConditions ( ) ;
113- Assert . NotNull ( conditions . Filters ) ;
114- Assert . Single ( conditions . Filters ) ;
115-
116- newConditions = new FilterKeyValueAction ( ) { Filters = [ ] , FilterLogic = FilterLogic . Or } ;
117- newConditions . Filters . Add ( new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Primary } ) ;
118- newConditions . Filters . Add ( new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Middle } ) ;
119- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
120- cut . Render ( ) ;
121-
122- // 检查 UI
123- var comps = cut . FindComponents < Select < string ? > > ( ) ;
124- Assert . Equal ( "Primary" , comps [ 0 ] . Instance . Value ) ;
125- Assert . Equal ( "Middle" , comps [ 1 ] . Instance . Value ) ;
126-
127- newConditions = new FilterKeyValueAction ( ) { Filters = [ ] , FilterLogic = FilterLogic . Or } ;
128- newConditions . Filters . Add ( new FilterKeyValueAction ( ) { FieldValue = EnumEducation . Primary } ) ;
129- newConditions . Filters . Add ( new FilterKeyValueAction ( ) { FieldValue = null } ) ;
130- cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
131- cut . Render ( ) ;
132- comps = cut . FindComponents < Select < string ? > > ( ) ;
133- Assert . Equal ( "Primary" , comps [ 0 ] . Instance . Value ) ;
134- Assert . Equal ( "" , comps [ 1 ] . Instance . Value ) ;
135- }
136-
137- [ Fact ]
138- public void TableFilter_On ( )
139- {
140- var cut = Context . RenderComponent < BootstrapBlazorRoot > ( pb =>
141- {
142- pb . AddChildContent < Table < Foo > > ( pb =>
143- {
144- pb . Add ( a => a . Items , new List < Foo > ( ) { new ( ) } ) ;
145- pb . Add ( a => a . RenderMode , TableRenderMode . Table ) ;
146- pb . Add ( a => a . TableColumns , new RenderFragment < Foo > ( foo => builder =>
147- {
148- var index = 0 ;
149- builder . OpenComponent < TableColumn < Foo , int > > ( index ++ ) ;
150- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , int > . Field ) , foo . Count ) ;
151- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , int > . FieldExpression ) , foo . GenerateValueExpression ( nameof ( Foo . Count ) , typeof ( int ) ) ) ;
152- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , int > . Filterable ) , true ) ;
153- builder . AddAttribute ( index ++ , nameof ( TableColumn < Foo , int > . FilterTemplate ) , new RenderFragment ( builder =>
154- {
155- builder . OpenComponent < MockFilter > ( 0 ) ;
156- builder . CloseComponent ( ) ;
157- } ) ) ;
158- builder . CloseComponent ( ) ;
159- } ) ) ;
160- } ) ;
161- } ) ;
162-
163- // 测试 filter.Filters?.Count > 1 TableFilter 表达式
164- var filter = cut . FindComponent < MockFilter > ( ) ;
165- cut . InvokeAsync ( ( ) => filter . Instance . SetFilterConditionsAsync ( new FilterKeyValueAction ( )
61+ newConditions = new FilterKeyValueAction ( )
16662 {
167- FieldValue = 1
168- } ) ) ;
169- filter . Instance . Reset ( ) ;
63+ Filters =
64+ [
65+ new FilterKeyValueAction ( ) { FieldValue = true } ,
66+ new FilterKeyValueAction ( ) { FieldValue = false }
67+ ]
68+ } ;
69+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
70+ conditions = filter . GetFilterConditions ( ) ;
71+ Assert . Empty ( conditions . Filters ) ;
17072
171- var tableFilter = cut . FindComponent < TableFilter > ( ) ;
172- tableFilter . Render ( ) ;
73+ newConditions = new FilterKeyValueAction ( ) { FieldValue = "1" } ;
74+ await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
75+ conditions = filter . GetFilterConditions ( ) ;
76+ Assert . Empty ( conditions . Filters ) ;
17377 }
17478
175- class MockFilter : FilterBase
79+ class MockColumn : TableColumn < Foo , EnumEducation >
17680 {
177- private List < FilterKeyValueAction > ? _filters = [ new FilterKeyValueAction ( ) { FieldValue = 1 } , new FilterKeyValueAction ( ) { FieldValue = 2 } ] ;
178-
179- public override FilterKeyValueAction GetFilterConditions ( )
180- {
181- return new FilterKeyValueAction ( ) { Filters = _filters } ;
182- }
183-
184- public override void Reset ( )
81+ public MockColumn ( )
18582 {
186- _filters = null ;
83+ PropertyType = typeof ( EnumEducation ) ;
84+ FieldName = "Education" ;
18785 }
18886 }
18987}
0 commit comments