1+ using AngleSharp . Html . Dom ;
2+ using Bunit ;
3+ using FluentAssertions ;
4+ using Microsoft . AspNetCore . Components ;
5+ using Xunit ;
6+
7+ namespace SimpleBlazorMultiselect . Tests ;
8+
9+ public class EnumTests : BaseTest
10+ {
11+ [ Fact ]
12+ public void Component_WithEmptySet_ShouldShowNoChecks ( )
13+ {
14+ var options = Enum . GetValues < TestEnum > ( ) . ToList ( ) ;
15+ var selectedItems = new HashSet < TestEnum > ( ) ;
16+
17+ var component = RenderComponent < SimpleMultiselect < TestEnum > > ( parameters => parameters
18+ . Add ( p => p . Options , options )
19+ . Add ( p => p . SelectedOptions , selectedItems )
20+ . Add ( p => p . StringSelector , item => item . ToString ( ) )
21+ . Add ( p => p . DefaultText , "empty" )
22+ . Add ( p => p . SelectedOptionsChanged , EventCallback . Factory . Create < HashSet < TestEnum > > ( this , newSelection => { selectedItems = newSelection ; } ) ) ) ;
23+
24+ var button = component . Find ( "button" ) ;
25+ button . TextContent . Should ( ) . Be ( "empty" ) ;
26+ button . Click ( ) ;
27+
28+ // Check if all options are unchecked
29+ var dropdownItems = component . FindAll ( ".dropdown-item" ) ;
30+ foreach ( var item in dropdownItems )
31+ {
32+ var checkbox = item . QuerySelector ( "input[type='checkbox']" ) as IHtmlInputElement ;
33+ checkbox . Should ( ) . NotBeNull ( ) ;
34+ checkbox . IsChecked . Should ( ) . BeFalse ( ) ;
35+ }
36+ }
37+
38+ [ Fact ]
39+ public void Component_WithPrefilledOptions_ShouldShowCorrectCheck ( )
40+ {
41+ var options = Enum . GetValues < TestEnum > ( ) . ToList ( ) ;
42+ var selectedItems = new HashSet < TestEnum >
43+ {
44+ TestEnum . OptionB
45+ } ;
46+
47+ var component = RenderComponent < SimpleMultiselect < TestEnum > > ( parameters => parameters
48+ . Add ( p => p . Options , options )
49+ . Add ( p => p . SelectedOptions , selectedItems )
50+ . Add ( p => p . StringSelector , item => item . ToString ( ) )
51+ . Add ( p => p . DefaultText , "empty" )
52+ . Add ( p => p . SelectedOptionsChanged , EventCallback . Factory . Create < HashSet < TestEnum > > ( this , newSelection => { selectedItems = newSelection ; } ) ) ) ;
53+
54+ var button = component . Find ( "button" ) ;
55+ button . TextContent . Should ( ) . Be ( nameof ( TestEnum . OptionB ) ) ;
56+ button . Click ( ) ;
57+
58+ // Check if all options are unchecked except OptionB
59+ var dropdownItems = component . FindAll ( ".dropdown-item" ) ;
60+ foreach ( var item in dropdownItems )
61+ {
62+ var checkbox = item . QuerySelector ( "input[type='checkbox']" ) as IHtmlInputElement ;
63+ checkbox . Should ( ) . NotBeNull ( ) ;
64+
65+ var optionText = item . TextContent . Trim ( ) ;
66+ if ( optionText == nameof ( TestEnum . OptionB ) )
67+ {
68+ checkbox . Should ( ) . NotBeNull ( ) ;
69+ checkbox . IsChecked . Should ( ) . BeTrue ( ) ;
70+ }
71+ else
72+ {
73+ checkbox . IsChecked . Should ( ) . BeFalse ( ) ;
74+ }
75+ }
76+ }
77+
78+ [ Fact ]
79+ public void Component_CanUncheckPrefilled ( )
80+ {
81+ var options = Enum . GetValues < TestEnum > ( ) . ToList ( ) ;
82+ var selectedItems = new HashSet < TestEnum >
83+ {
84+ TestEnum . OptionB
85+ } ;
86+
87+ var component = RenderComponent < SimpleMultiselect < TestEnum > > ( parameters => parameters
88+ . Add ( p => p . Options , options )
89+ . Add ( p => p . SelectedOptions , selectedItems )
90+ . Add ( p => p . StringSelector , item => item . ToString ( ) )
91+ . Add ( p => p . DefaultText , "empty" )
92+ . Add ( p => p . SelectedOptionsChanged , EventCallback . Factory . Create < HashSet < TestEnum > > ( this , newSelection => { selectedItems = newSelection ; } ) ) ) ;
93+
94+ var button = component . Find ( "button" ) ;
95+ button . TextContent . Should ( ) . Be ( nameof ( TestEnum . OptionB ) ) ;
96+ button . Click ( ) ;
97+
98+ // Check if option B is checked
99+ var dropdownItems = component . FindAll ( ".dropdown-item" ) ;
100+ var optionBItem = dropdownItems . First ( item => item . TextContent . Trim ( ) == nameof ( TestEnum . OptionB ) ) ;
101+ var optionBCheckbox = optionBItem . QuerySelector ( "input[type='checkbox']" ) as IHtmlInputElement ;
102+ optionBCheckbox . Should ( ) . NotBeNull ( ) ;
103+ optionBCheckbox . IsChecked . Should ( ) . BeTrue ( ) ;
104+
105+ // Click to uncheck OptionB
106+ optionBItem . Click ( ) ;
107+
108+ // After clicking, OptionB should be deselected
109+ selectedItems . Should ( ) . BeEmpty ( ) ;
110+ button = component . Find ( "button" ) ;
111+ button . TextContent . Should ( ) . Be ( "empty" ) ;
112+ }
113+
114+ [ Fact ]
115+ public void Component_CanCheckUncheckedOption ( )
116+ {
117+ var options = Enum . GetValues < TestEnum > ( ) . ToList ( ) ;
118+ var selectedItems = new HashSet < TestEnum > ( ) ;
119+
120+ var component = RenderComponent < SimpleMultiselect < TestEnum > > ( parameters => parameters
121+ . Add ( p => p . Options , options )
122+ . Add ( p => p . SelectedOptions , selectedItems )
123+ . Add ( p => p . StringSelector , item => item . ToString ( ) )
124+ . Add ( p => p . DefaultText , "empty" )
125+ . Add ( p => p . SelectedOptionsChanged , EventCallback . Factory . Create < HashSet < TestEnum > > ( this , newSelection => { selectedItems = newSelection ; } ) ) ) ;
126+
127+ var button = component . Find ( "button" ) ;
128+ button . TextContent . Should ( ) . Be ( "empty" ) ;
129+ button . Click ( ) ;
130+
131+ // Check if all options are unchecked
132+ var dropdownItems = component . FindAll ( ".dropdown-item" ) ;
133+ var optionCItem = dropdownItems . First ( item => item . TextContent . Trim ( ) == nameof ( TestEnum . OptionC ) ) ;
134+ var optionCCheckbox = optionCItem . QuerySelector ( "input[type='checkbox']" ) as IHtmlInputElement ;
135+ optionCCheckbox . Should ( ) . NotBeNull ( ) ;
136+ optionCCheckbox . IsChecked . Should ( ) . BeFalse ( ) ;
137+
138+ // Click to check OptionC
139+ optionCItem . Click ( ) ;
140+
141+ // After clicking, OptionC should be selected
142+ selectedItems . Should ( ) . Contain ( TestEnum . OptionC ) ;
143+ button = component . Find ( "button" ) ;
144+ button . TextContent . Should ( ) . Be ( nameof ( TestEnum . OptionC ) ) ;
145+ }
146+
147+ private enum TestEnum
148+ {
149+ OptionA ,
150+ OptionB ,
151+ OptionC
152+ }
153+ }
0 commit comments