@@ -3,15 +3,45 @@ import {render} from './helpers/test-utils'
3
3
describe ( '.toBeChecked' , ( ) => {
4
4
test ( 'handles checkbox input' , ( ) => {
5
5
const { queryByTestId} = render ( `
6
- <input type="checkbox" checked data-testid="input-checked" />
7
- <input type="checkbox" data-testid="input-empty " />
6
+ <input type="checkbox" checked data-testid="input-checkbox- checked" />
7
+ <input type="checkbox" data-testid="input-checkbox-unchecked " />
8
8
` )
9
9
10
- expect ( queryByTestId ( 'input-checked' ) ) . toBeChecked ( )
11
- expect ( queryByTestId ( 'input-empty ' ) ) . not . toBeChecked ( )
10
+ expect ( queryByTestId ( 'input-checkbox- checked' ) ) . toBeChecked ( )
11
+ expect ( queryByTestId ( 'input-checkbox-unchecked ' ) ) . not . toBeChecked ( )
12
12
} )
13
13
14
- test ( 'throws when checkbox is checked but expected not to be' , ( ) => {
14
+ test ( 'handles radio input' , ( ) => {
15
+ const { queryByTestId} = render ( `
16
+ <input type="radio" checked value="foo" data-testid="input-radio-checked" />
17
+ <input type="radio" value="foo" data-testid="input-radio-unchecked" />
18
+ ` )
19
+
20
+ expect ( queryByTestId ( 'input-radio-checked' ) ) . toBeChecked ( )
21
+ expect ( queryByTestId ( 'input-radio-unchecked' ) ) . not . toBeChecked ( )
22
+ } )
23
+
24
+ test ( 'handles element with role="checkbox"' , ( ) => {
25
+ const { queryByTestId} = render ( `
26
+ <div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />
27
+ <div role="checkbox" aria-checked="false" data-testid="aria-checkbox-unchecked" />
28
+ ` )
29
+
30
+ expect ( queryByTestId ( 'aria-checkbox-checked' ) ) . toBeChecked ( )
31
+ expect ( queryByTestId ( 'aria-checkbox-unchecked' ) ) . not . toBeChecked ( )
32
+ } )
33
+
34
+ test ( 'handles element with role="radio"' , ( ) => {
35
+ const { queryByTestId} = render ( `
36
+ <div role="radio" aria-checked="true" data-testid="aria-radio-checked" />
37
+ <div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" />
38
+ ` )
39
+
40
+ expect ( queryByTestId ( 'aria-radio-checked' ) ) . toBeChecked ( )
41
+ expect ( queryByTestId ( 'aria-radio-unchecked' ) ) . not . toBeChecked ( )
42
+ } )
43
+
44
+ test ( 'throws when checkbox input is checked but expected not to be' , ( ) => {
15
45
const { queryByTestId} = render (
16
46
`<input type="checkbox" checked data-testid="input-checked" />` ,
17
47
)
@@ -21,7 +51,7 @@ describe('.toBeChecked', () => {
21
51
) . toThrowError ( )
22
52
} )
23
53
24
- test ( 'throws when checkbox is not checked but expected to be' , ( ) => {
54
+ test ( 'throws when input checkbox is not checked but expected to be' , ( ) => {
25
55
const { queryByTestId} = render (
26
56
`<input type="checkbox" data-testid="input-empty" />` ,
27
57
)
@@ -31,20 +61,96 @@ describe('.toBeChecked', () => {
31
61
) . toThrowError ( )
32
62
} )
33
63
34
- test ( 'throws when the element is not an input' , ( ) => {
35
- const { queryByTestId} = render ( `<select data-testid="select"></select>` )
36
- expect ( ( ) => expect ( queryByTestId ( 'select' ) ) . toBeChecked ( ) ) . toThrowError (
37
- 'only inputs with type=checkbox can be used with .toBeChecked(). Use .toHaveFormValues() instead' ,
64
+ test ( 'throws when element with role="checkbox" is checked but expected not to be' , ( ) => {
65
+ const { queryByTestId} = render (
66
+ `<div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />` ,
67
+ )
68
+
69
+ expect ( ( ) =>
70
+ expect ( queryByTestId ( 'aria-checkbox-checked' ) ) . not . toBeChecked ( ) ,
71
+ ) . toThrowError ( )
72
+ } )
73
+
74
+ test ( 'throws when element with role="checkbox" is not checked but expected to be' , ( ) => {
75
+ const { queryByTestId} = render (
76
+ `<div role="checkbox" aria-checked="false" data-testid="aria-checkbox-unchecked" />` ,
77
+ )
78
+
79
+ expect ( ( ) =>
80
+ expect ( queryByTestId ( 'aria-checkbox-unchecked' ) ) . toBeChecked ( ) ,
81
+ ) . toThrowError ( )
82
+ } )
83
+
84
+ test ( 'throws when radio input is checked but expected not to be' , ( ) => {
85
+ const { queryByTestId} = render (
86
+ `<input type="radio" checked data-testid="input-radio-checked" />` ,
87
+ )
88
+
89
+ expect ( ( ) =>
90
+ expect ( queryByTestId ( 'input-radio-checked' ) ) . not . toBeChecked ( ) ,
91
+ ) . toThrowError ( )
92
+ } )
93
+
94
+ test ( 'throws when input radio is not checked but expected to be' , ( ) => {
95
+ const { queryByTestId} = render (
96
+ `<input type="radio" data-testid="input-radio-unchecked" />` ,
97
+ )
98
+
99
+ expect ( ( ) =>
100
+ expect ( queryByTestId ( 'input-radio-unchecked' ) ) . toBeChecked ( ) ,
101
+ ) . toThrowError ( )
102
+ } )
103
+
104
+ test ( 'throws when element with role="radio" is checked but expected not to be' , ( ) => {
105
+ const { queryByTestId} = render (
106
+ `<div role="radio" aria-checked="true" data-testid="aria-radio-checked" />` ,
107
+ )
108
+
109
+ expect ( ( ) =>
110
+ expect ( queryByTestId ( 'aria-radio-checked' ) ) . not . toBeChecked ( ) ,
111
+ ) . toThrowError ( )
112
+ } )
113
+
114
+ test ( 'throws when element with role="radio" is not checked but expected to be' , ( ) => {
115
+ const { queryByTestId} = render (
116
+ `<div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" />` ,
117
+ )
118
+
119
+ expect ( ( ) =>
120
+ expect ( queryByTestId ( 'aria-checkbox-unchecked' ) ) . toBeChecked ( ) ,
121
+ ) . toThrowError ( )
122
+ } )
123
+
124
+ test ( 'throws when element with role="checkbox" has an invalid aria-checked attribute' , ( ) => {
125
+ const { queryByTestId} = render (
126
+ `<div role="checkbox" aria-checked="something" data-testid="aria-checkbox-invalid" />` ,
127
+ )
128
+
129
+ expect ( ( ) =>
130
+ expect ( queryByTestId ( 'aria-checkbox-invalid' ) ) . toBeChecked ( ) ,
131
+ ) . toThrowError (
132
+ 'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead' ,
38
133
)
39
134
} )
40
135
41
- test ( 'throws when the element is not a checkbox input ' , ( ) => {
136
+ test ( 'throws when element with role="radio" has an invalid aria-checked attribute ' , ( ) => {
42
137
const { queryByTestId} = render (
43
- `<input type ="radio" checked data-testid="radio" />` ,
138
+ `<div role ="radio" aria- checked="something" data-testid="aria- radio-invalid " />` ,
44
139
)
45
140
46
- expect ( ( ) => expect ( queryByTestId ( 'radio' ) ) . toBeChecked ( ) ) . toThrowError (
47
- `only inputs with type=checkbox can be used with .toBeChecked(). Use .toHaveFormValues() instead` ,
141
+ expect ( ( ) =>
142
+ expect ( queryByTestId ( 'aria-radio-invalid' ) ) . toBeChecked ( ) ,
143
+ ) . toThrowError (
144
+ 'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead' ,
145
+ )
146
+ } )
147
+
148
+ test ( 'throws when the element is not an input' , ( ) => {
149
+ const { queryByTestId} = render ( `<select data-testid="select"></select>` )
150
+ expect ( ( ) => expect ( queryByTestId ( 'select' ) ) . toBeChecked ( ) ) . toThrowError (
151
+ 'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead' ,
48
152
)
49
153
} )
50
154
} )
155
+
156
+ /* eslint max-lines-per-function:0 */
0 commit comments