10
10
* governing permissions and limitations under the License.
11
11
*/
12
12
13
+ import { act , render } from '@testing-library/react' ;
13
14
import { AriaCheckboxGroupItemProps , AriaCheckboxGroupProps } from '@react-types/checkbox' ;
14
15
import { CheckboxGroupState , useCheckboxGroupState } from '@react-stately/checkbox' ;
15
16
import React , { useRef } from 'react' ;
16
- import { render } from '@testing-library/react' ;
17
17
import { useCheckboxGroup , useCheckboxGroupItem } from '../' ;
18
18
import userEvent from '@testing-library/user-event' ;
19
19
20
20
function Checkbox ( { checkboxGroupState, ...props } : AriaCheckboxGroupItemProps & { checkboxGroupState : CheckboxGroupState } ) {
21
21
const ref = useRef < HTMLInputElement > ( ) ;
22
22
const { children} = props ;
23
23
const { inputProps} = useCheckboxGroupItem ( props , checkboxGroupState , ref ) ;
24
- return < label > { children } < input ref = { ref } { ...inputProps } /> </ label > ;
24
+ return < label > < input ref = { ref } { ...inputProps } /> { children } </ label > ;
25
25
}
26
26
27
27
function CheckboxGroup ( { groupProps, checkboxProps} : { groupProps : AriaCheckboxGroupProps , checkboxProps : AriaCheckboxGroupItemProps [ ] } ) {
@@ -63,18 +63,18 @@ describe('useCheckboxGroup', () => {
63
63
expect ( checkboxes [ 1 ] . value ) . toBe ( 'cats' ) ;
64
64
expect ( checkboxes [ 2 ] . value ) . toBe ( 'dragons' ) ;
65
65
66
- expect ( checkboxes [ 0 ] . checked ) . toBe ( false ) ;
67
- expect ( checkboxes [ 1 ] . checked ) . toBe ( false ) ;
68
- expect ( checkboxes [ 2 ] . checked ) . toBe ( false ) ;
66
+ expect ( checkboxes [ 0 ] . checked ) . toBeFalsy ( ) ;
67
+ expect ( checkboxes [ 1 ] . checked ) . toBeFalsy ( ) ;
68
+ expect ( checkboxes [ 2 ] . checked ) . toBeFalsy ( ) ;
69
69
70
70
let dragons = getByLabelText ( 'Dragons' ) ;
71
71
userEvent . click ( dragons ) ;
72
72
expect ( onChangeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
73
73
expect ( onChangeSpy ) . toHaveBeenCalledWith ( [ 'dragons' ] ) ;
74
74
75
- expect ( checkboxes [ 0 ] . checked ) . toBe ( false ) ;
76
- expect ( checkboxes [ 1 ] . checked ) . toBe ( false ) ;
77
- expect ( checkboxes [ 2 ] . checked ) . toBe ( true ) ;
75
+ expect ( checkboxes [ 0 ] . checked ) . toBeFalsy ( ) ;
76
+ expect ( checkboxes [ 1 ] . checked ) . toBeFalsy ( ) ;
77
+ expect ( checkboxes [ 2 ] . checked ) . toBeTruthy ( ) ;
78
78
} ) ;
79
79
80
80
it ( 'can have a default value' , ( ) => {
@@ -159,13 +159,15 @@ describe('useCheckboxGroup', () => {
159
159
} ) ;
160
160
161
161
it ( 'sets aria-disabled and makes checkboxes disabled when isDisabled is true' , ( ) => {
162
- let { getAllByRole, getByRole} = render (
162
+ let groupOnChangeSpy = jest . fn ( ) ;
163
+ let checkboxOnChangeSpy = jest . fn ( ) ;
164
+ let { getAllByRole, getByRole, getByLabelText} = render (
163
165
< CheckboxGroup
164
- groupProps = { { label : 'Favorite Pet' , isDisabled : true } }
166
+ groupProps = { { label : 'Favorite Pet' , isDisabled : true , onChange : groupOnChangeSpy } }
165
167
checkboxProps = { [
166
168
{ value : 'dogs' , children : 'Dogs' } ,
167
169
{ value : 'cats' , children : 'Cats' } ,
168
- { value : 'dragons' , children : 'Dragons' }
170
+ { value : 'dragons' , children : 'Dragons' , onChange : checkboxOnChangeSpy }
169
171
] } />
170
172
) ;
171
173
@@ -174,8 +176,15 @@ describe('useCheckboxGroup', () => {
174
176
175
177
let checkboxes = getAllByRole ( 'checkbox' ) as HTMLInputElement [ ] ;
176
178
expect ( checkboxes [ 0 ] ) . toHaveAttribute ( 'disabled' ) ;
177
- expect ( checkboxes [ 0 ] ) . toHaveAttribute ( 'disabled' ) ;
178
- expect ( checkboxes [ 0 ] ) . toHaveAttribute ( 'disabled' ) ;
179
+ expect ( checkboxes [ 1 ] ) . toHaveAttribute ( 'disabled' ) ;
180
+ expect ( checkboxes [ 2 ] ) . toHaveAttribute ( 'disabled' ) ;
181
+ let dragons = getByLabelText ( 'Dragons' ) ;
182
+
183
+ act ( ( ) => { userEvent . click ( dragons ) ; } ) ;
184
+
185
+ expect ( groupOnChangeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
186
+ expect ( checkboxOnChangeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
187
+ expect ( checkboxes [ 2 ] . checked ) . toBeFalsy ( ) ;
179
188
} ) ;
180
189
181
190
it ( 'doesn\'t set aria-disabled or make checkboxes disabled by default' , ( ) => {
@@ -194,8 +203,8 @@ describe('useCheckboxGroup', () => {
194
203
195
204
let checkboxes = getAllByRole ( 'checkbox' ) as HTMLInputElement [ ] ;
196
205
expect ( checkboxes [ 0 ] ) . not . toHaveAttribute ( 'disabled' ) ;
197
- expect ( checkboxes [ 0 ] ) . not . toHaveAttribute ( 'disabled' ) ;
198
- expect ( checkboxes [ 0 ] ) . not . toHaveAttribute ( 'disabled' ) ;
206
+ expect ( checkboxes [ 1 ] ) . not . toHaveAttribute ( 'disabled' ) ;
207
+ expect ( checkboxes [ 2 ] ) . not . toHaveAttribute ( 'disabled' ) ;
199
208
} ) ;
200
209
201
210
it ( 'doesn\'t set aria-disabled or make checkboxes disabled when isDisabled is false' , ( ) => {
@@ -214,25 +223,35 @@ describe('useCheckboxGroup', () => {
214
223
215
224
let checkboxes = getAllByRole ( 'checkbox' ) as HTMLInputElement [ ] ;
216
225
expect ( checkboxes [ 0 ] ) . not . toHaveAttribute ( 'disabled' ) ;
217
- expect ( checkboxes [ 0 ] ) . not . toHaveAttribute ( 'disabled' ) ;
218
- expect ( checkboxes [ 0 ] ) . not . toHaveAttribute ( 'disabled' ) ;
226
+ expect ( checkboxes [ 1 ] ) . not . toHaveAttribute ( 'disabled' ) ;
227
+ expect ( checkboxes [ 2 ] ) . not . toHaveAttribute ( 'disabled' ) ;
219
228
} ) ;
220
229
221
230
it ( 'sets aria-readonly="true" on each checkbox' , ( ) => {
222
- let { getAllByRole} = render (
231
+ let groupOnChangeSpy = jest . fn ( ) ;
232
+ let checkboxOnChangeSpy = jest . fn ( ) ;
233
+ let { getAllByRole, getByLabelText} = render (
223
234
< CheckboxGroup
224
- groupProps = { { label : 'Favorite Pet' , isReadOnly : true } }
235
+ groupProps = { { label : 'Favorite Pet' , isReadOnly : true , onChange : groupOnChangeSpy } }
225
236
checkboxProps = { [
226
237
{ value : 'dogs' , children : 'Dogs' } ,
227
238
{ value : 'cats' , children : 'Cats' } ,
228
- { value : 'dragons' , children : 'Dragons' }
239
+ { value : 'dragons' , children : 'Dragons' , onChange : checkboxOnChangeSpy }
229
240
] } />
230
241
) ;
231
242
232
243
let checkboxes = getAllByRole ( 'checkbox' ) as HTMLInputElement [ ] ;
233
244
expect ( checkboxes [ 0 ] ) . toHaveAttribute ( 'aria-readonly' , 'true' ) ;
234
245
expect ( checkboxes [ 1 ] ) . toHaveAttribute ( 'aria-readonly' , 'true' ) ;
235
246
expect ( checkboxes [ 2 ] ) . toHaveAttribute ( 'aria-readonly' , 'true' ) ;
247
+ expect ( checkboxes [ 2 ] . checked ) . toBeFalsy ( ) ;
248
+ let dragons = getByLabelText ( 'Dragons' ) ;
249
+
250
+ act ( ( ) => { userEvent . click ( dragons ) ; } ) ;
251
+
252
+ expect ( groupOnChangeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
253
+ expect ( checkboxOnChangeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
254
+ expect ( checkboxes [ 2 ] . checked ) . toBeFalsy ( ) ;
236
255
} ) ;
237
256
238
257
it ( 'should not update state for readonly checkbox' , ( ) => {
@@ -255,6 +274,6 @@ describe('useCheckboxGroup', () => {
255
274
256
275
expect ( groupOnChangeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
257
276
expect ( checkboxOnChangeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
258
- expect ( checkboxes [ 2 ] . checked ) . toBe ( false ) ;
277
+ expect ( checkboxes [ 2 ] . checked ) . toBeFalsy ( ) ;
259
278
} ) ;
260
279
} ) ;
0 commit comments