33// See the LICENSE file in the project root for more information.
44// Maintainer: Argo Zhang([email protected] ) Website: https://www.blazor.zone 55
6+ using Microsoft . EntityFrameworkCore . Metadata . Internal ;
67using Microsoft . Extensions . Localization ;
8+ using System . ComponentModel . DataAnnotations ;
79
810namespace UnitTest . Components ;
911
1012public class CheckboxListGenericTest : BootstrapBlazorTestBase
1113{
12- private IStringLocalizer < Foo > Localizer { get ; }
13-
14- public CheckboxListGenericTest ( )
15- {
16- Localizer = Context . Services . GetRequiredService < IStringLocalizer < Foo > > ( ) ;
17- }
18-
1914 [ Fact ]
2015 public void EditorForm_Ok ( )
2116 {
22- var dummy = new Dummy ( ) ;
17+ var dummy = new Dummy ( ) { Data = [ new ( ) { Id = 2 , Name = "Test2" } ] } ;
18+ var items = new List < SelectedItem < Foo > > ( )
19+ {
20+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
21+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" )
22+ } ;
2323 var cut = Context . RenderComponent < ValidateForm > ( builder =>
2424 {
2525 builder . Add ( a => a . Model , dummy ) ;
26- builder . AddChildContent < CheckboxList < IEnumerable < Foo > > > ( pb =>
26+ builder . AddChildContent < CheckboxListGeneric < Foo > > ( pb =>
2727 {
28- pb . Add ( a => a . Items , Foo . GenerateHobbies ( Localizer ) ) ;
28+ pb . Add ( a => a . Items , items ) ;
2929 pb . Add ( a => a . Value , dummy . Data ) ;
3030 pb . Add ( a => a . ValueExpression , Utility . GenerateValueExpression ( dummy , nameof ( dummy . Data ) , typeof ( List < Foo > ) ) ) ;
3131 } ) ;
3232 } ) ;
3333 // 断言生成 CheckboxList
3434 Assert . Contains ( "form-check is-label" , cut . Markup ) ;
35- cut . Contains ( "是/否" ) ;
3635
3736 // 提交表单触发客户端验证
3837 var form = cut . Find ( "form" ) ;
3938 form . Submit ( ) ;
40- Assert . Contains ( "is-invalid " , cut . Markup ) ;
39+ Assert . Contains ( "is-valid " , cut . Markup ) ;
4140 }
4241
4342 [ Fact ]
4443 public void ShowBorder_Ok ( )
4544 {
46- var foo = Foo . Generate ( Localizer ) ;
47- var cut = Context . RenderComponent < CheckboxList < IEnumerable < string > > > ( pb =>
45+ var items = new List < SelectedItem < Foo > > ( )
46+ {
47+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
48+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" )
49+ } ;
50+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( pb =>
4851 {
49- pb . Add ( a => a . Items , Foo . GenerateHobbies ( Localizer ) ) ;
50- pb . Add ( a => a . Value , foo . Hobby ) ;
52+ pb . Add ( a => a . Items , items ) ;
5153 } ) ;
5254 Assert . DoesNotContain ( "no-border" , cut . Markup ) ;
5355
@@ -61,36 +63,60 @@ public void ShowBorder_Ok()
6163 [ Fact ]
6264 public void IsVertical_Ok ( )
6365 {
64- var cut = Context . RenderComponent < CheckboxList < IEnumerable < int > > > ( ) ;
66+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( ) ;
6567 Assert . DoesNotContain ( "is-vertical" , cut . Markup ) ;
6668
6769 cut . SetParametersAndRender ( pb =>
6870 {
6971 pb . Add ( a => a . IsVertical , true ) ;
72+ pb . Add ( a => a . CustomKeyAttribute , typeof ( KeyAttribute ) ) ;
73+ pb . Add ( a => a . ModelEqualityComparer , new Func < Foo , Foo , bool > ( ( x , y ) => x . Id == y . Id ) ) ;
7074 } ) ;
7175 Assert . Contains ( "is-vertical" , cut . Markup ) ;
7276 }
7377
78+ [ Fact ]
79+ public async Task NullItem_Ok ( )
80+ {
81+ var items = new List < SelectedItem < Foo > > ( )
82+ {
83+ new ( null , "Select ..." ) ,
84+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" )
85+ } ;
86+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( pb =>
87+ {
88+ pb . Add ( a => a . Items , items ) ;
89+ } ) ;
90+ cut . Contains ( "Select ..." ) ;
91+
92+ var checkboxes = cut . FindComponents < Checkbox < bool > > ( ) ;
93+ await cut . InvokeAsync ( async ( ) =>
94+ {
95+ await checkboxes [ 0 ] . Instance . OnToggleClick ( ) ;
96+ } ) ;
97+ Assert . Null ( cut . Instance . Value [ 0 ] ) ;
98+ }
99+
74100 [ Fact ]
75101 public void IsDisabled_Ok ( )
76102 {
77- var cut = Context . RenderComponent < CheckboxList < IEnumerable < SelectedItem > > > ( pb =>
103+ var items = new List < SelectedItem < Foo > > ( )
78104 {
79- pb . Add ( a => a . Items , new List < SelectedItem > ( )
80- {
81- new ( ) { Text = "Item 1" , Value = "1" } ,
82- new ( ) { Text = "Item 2" , Value = "2" , IsDisabled = true } ,
83- new ( ) { Text = "Item 3" , Value = "3" } ,
84- } ) ;
105+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
106+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" ) { IsDisabled = true }
107+ } ;
108+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( pb =>
109+ {
110+ pb . Add ( a => a . Items , items ) ;
85111 } ) ;
86112 cut . Contains ( "form-check is-label disabled" ) ;
87113
88114 cut . SetParametersAndRender ( pb =>
89115 {
90- pb . Add ( a => a . Items , new List < SelectedItem > ( )
116+ pb . Add ( a => a . Items , new List < SelectedItem < Foo > > ( )
91117 {
92- new ( ) { Text = "Item 1" , Value = "1 " } ,
93- new ( ) { Text = "Item 2" , Value = "2 " }
118+ new ( new Foo ( ) { Id = 1 , Name = "Test1 " } , "Test 1" ) ,
119+ new ( new Foo ( ) { Id = 2 , Name = "Test2 " } , "Test 2" )
94120 } ) ;
95121 pb . Add ( a => a . IsDisabled , true ) ;
96122 } ) ;
@@ -100,35 +126,35 @@ public void IsDisabled_Ok()
100126 [ Fact ]
101127 public void CheckboxItemClass_Ok ( )
102128 {
103- var cut = Context . RenderComponent < CheckboxList < string > > ( builder =>
129+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( builder =>
104130 {
105131 builder . Add ( a => a . CheckboxItemClass , "test-item" ) ;
106132 } ) ;
107133 Assert . DoesNotContain ( "test-item" , cut . Markup ) ;
108134
135+ var items = new List < SelectedItem < Foo > > ( )
136+ {
137+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
138+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" )
139+ } ;
109140 cut . SetParametersAndRender ( pb =>
110141 {
111- pb . Add ( a => a . Items , Foo . GenerateHobbies ( Localizer ) ) ;
142+ pb . Add ( a => a . Items , items ) ;
112143 } ) ;
113144 Assert . Contains ( "test-item" , cut . Markup ) ;
114145 }
115146
116147 [ Fact ]
117148 public async Task StringValue_Ok ( )
118149 {
119- var cut = Context . RenderComponent < CheckboxList < string > > ( builder =>
150+ var items = new List < SelectedItem < Foo > > ( )
120151 {
121- builder . Add ( a => a . Value , "1,2" ) ;
122- } ) ;
123- Assert . Contains ( "checkbox-list" , cut . Markup ) ;
124-
125- cut . SetParametersAndRender ( pb =>
152+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
153+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" )
154+ } ;
155+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( pb =>
126156 {
127- pb . Add ( a => a . Items , new List < SelectedItem > ( )
128- {
129- new ( "1" , "Test 1" ) ,
130- new ( "2" , "Test 2" )
131- } ) ;
157+ pb . Add ( a => a . Items , items ) ;
132158 } ) ;
133159 Assert . Contains ( "checkbox-list" , cut . Markup ) ;
134160
@@ -148,124 +174,38 @@ public async Task StringValue_Ok()
148174 }
149175
150176 [ Fact ]
151- public async Task OnSelectedChanged_Ok ( )
177+ public async Task IsButton_Ok ( )
152178 {
153- var selected = false ;
154- var foo = Foo . Generate ( Localizer ) ;
155- var cut = Context . RenderComponent < CheckboxList < IEnumerable < string > > > ( pb =>
179+ var items = new List < SelectedItem < Foo > > ( )
156180 {
157- pb . Add ( a => a . Items , Foo . GenerateHobbies ( Localizer ) ) ;
158- pb . Add ( a => a . Value , foo . Hobby ) ;
159- pb . Add ( a => a . OnSelectedChanged , ( v1 , v2 ) =>
160- {
161- selected = true ;
162- return Task . CompletedTask ;
163- } ) ;
164- } ) ;
165-
166- var item = cut . FindComponent < Checkbox < bool > > ( ) ;
167- await cut . InvokeAsync ( item . Instance . OnToggleClick ) ;
168- Assert . True ( selected ) ;
169- }
170-
171- [ Fact ]
172- public void EnumValue_Ok ( )
173- {
174- var selectedEnumValues = new List < EnumEducation > { EnumEducation . Middle , EnumEducation . Primary } ;
175- var cut = Context . RenderComponent < CheckboxList < IEnumerable < EnumEducation > > > ( pb =>
176- {
177- pb . Add ( a => a . Value , selectedEnumValues ) ;
178- } ) ;
179- Assert . Contains ( "form-check-input" , cut . Markup ) ;
180- }
181-
182- [ Fact ]
183- public async Task IntValue_Ok ( )
184- {
185- var ret = new List < int > ( ) ;
186- var selectedIntValues = new List < int > { 1 , 2 } ;
187- var cut = Context . RenderComponent < CheckboxList < IEnumerable < int > > > ( pb =>
188- {
189- pb . Add ( a => a . Value , selectedIntValues ) ;
190- pb . Add ( a => a . Items , new List < SelectedItem > ( )
191- {
192- new ( "1" , "Test 1" ) ,
193- new ( "2" , "Test 2" )
194- } ) ;
195- pb . Add ( a => a . OnSelectedChanged , ( v1 , v2 ) =>
196- {
197- ret . AddRange ( v2 ) ;
198- return Task . CompletedTask ;
199- } ) ;
200- } ) ;
201- var item = cut . FindComponent < Checkbox < bool > > ( ) ;
202- await cut . InvokeAsync ( item . Instance . OnToggleClick ) ;
203-
204- // 选中 2
205- Assert . Equal ( 2 , ret . First ( ) ) ;
206- }
207-
208- [ Fact ]
209- public void NotSupportedException_Error ( )
210- {
211- Assert . Throws < NotSupportedException > ( ( ) => Context . RenderComponent < CheckboxList < CheckboxListGenericMock < int > > > ( ) ) ;
212- Assert . Throws < NotSupportedException > ( ( ) => Context . RenderComponent < CheckboxList < int > > ( ) ) ;
213- }
214-
215- [ Fact ]
216- public void FormatValue_Ok ( )
217- {
218- var cut = Context . RenderComponent < FormatValueTestCheckboxList > ( ) ;
219- cut . InvokeAsync ( ( ) =>
220- {
221- Assert . Null ( cut . Instance . NullValueTest ( ) ) ;
222- Assert . NotNull ( cut . Instance . NotNullValueTest ( ) ) ;
223- } ) ;
224- }
225-
226- [ Fact ]
227- public void FormatGenericValue_Ok ( )
228- {
229- var cut = Context . RenderComponent < FormatValueTestGenericCheckboxList > ( ) ;
230- cut . InvokeAsync ( ( ) =>
231- {
232- Assert . Equal ( string . Empty , cut . Instance . NullValueTest ( ) ) ;
233- Assert . Equal ( "test" , cut . Instance . NotNullValueTest ( ) ) ;
234- } ) ;
235- }
236-
237- [ Fact ]
238- public void IsButton_Ok ( )
239- {
240- var cut = Context . RenderComponent < CheckboxList < IEnumerable < int > > > ( pb =>
181+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
182+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" )
183+ } ;
184+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( pb =>
241185 {
242186 pb . Add ( a => a . IsButton , true ) ;
243- pb . Add ( a => a . Color , Color . Danger ) ;
244- pb . Add ( a => a . Items , new List < SelectedItem > ( )
245- {
246- new ( "1" , "Test 1" ) ,
247- new ( "2" , "Test 2" )
248- } ) ;
187+ pb . Add ( a => a . Color , Color . None ) ;
188+ pb . Add ( a => a . Items , items ) ;
249189 } ) ;
250- cut . InvokeAsync ( ( ) =>
190+ var item = cut . Find ( ".btn" ) ;
191+ await cut . InvokeAsync ( ( ) =>
251192 {
252- var item = cut . Find ( ".btn" ) ;
253193 item . Click ( ) ;
254- cut . Contains ( "btn active bg-danger" ) ;
255194 } ) ;
195+ cut . Contains ( "btn active bg-primary" ) ;
256196 }
257197
258198 [ Fact ]
259199 public async Task OnMaxSelectedCountExceed_Ok ( )
260200 {
261201 bool max = false ;
262- var items = new List < SelectedItem > ( )
202+ var items = new List < SelectedItem < Foo > > ( )
263203 {
264- new ( "1" , "Test 1" ) ,
265- new ( "2" , "Test 2" ) ,
266- new ( "3" , "Test 3" )
204+ new ( new Foo ( ) { Id = 1 , Name = "Test1" } , "Test 1" ) ,
205+ new ( new Foo ( ) { Id = 2 , Name = "Test2" } , "Test 2" ) ,
206+ new ( new Foo ( ) { Id = 3 , Name = "Test3" } , "Test 3" )
267207 } ;
268- var cut = Context . RenderComponent < CheckboxList < string > > ( pb =>
208+ var cut = Context . RenderComponent < CheckboxListGeneric < Foo > > ( pb =>
269209 {
270210 pb . Add ( a => a . MaxSelectedCount , 2 ) ;
271211 pb . Add ( a => a . Items , items ) ;
@@ -312,31 +252,9 @@ await cut.InvokeAsync(async () =>
312252 Assert . False ( max ) ;
313253 }
314254
315- private class CheckboxListGenericMock < T >
316- {
317-
318- }
319-
320- private class FormatValueTestCheckboxList : CheckboxList < string ? >
321- {
322- public string ? NullValueTest ( ) => base . FormatValueAsString ( null ) ;
323-
324- public string ? NotNullValueTest ( ) => base . FormatValueAsString ( "test" ) ;
325- }
326-
327- private class FormatValueTestGenericCheckboxList : CheckboxList < IEnumerable < string > ? >
328- {
329- public string ? NullValueTest ( ) => base . FormatValueAsString ( null ) ;
330-
331- public string ? NotNullValueTest ( )
332- {
333- Items = new List < SelectedItem > ( ) { new ( "test" , "test" ) { Active = true } } ;
334- return base . FormatValueAsString ( new List < string > ( ) { "test" } ) ;
335- }
336- }
337-
338255 private class Dummy
339256 {
257+ [ Required ]
340258 public List < Foo > ? Data { get ; set ; }
341259 }
342260}
0 commit comments