/
to-be-checked.js
198 lines (160 loc) · 7.27 KB
/
to-be-checked.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {render} from './helpers/test-utils'
describe('.toBeChecked', () => {
test('handles checkbox input', () => {
const {queryByTestId} = render(`
<input type="checkbox" checked data-testid="input-checkbox-checked" />
<input type="checkbox" data-testid="input-checkbox-unchecked" />
`)
expect(queryByTestId('input-checkbox-checked')).toBeChecked()
expect(queryByTestId('input-checkbox-unchecked')).not.toBeChecked()
})
test('handles radio input', () => {
const {queryByTestId} = render(`
<input type="radio" checked value="foo" data-testid="input-radio-checked" />
<input type="radio" value="foo" data-testid="input-radio-unchecked" />
`)
expect(queryByTestId('input-radio-checked')).toBeChecked()
expect(queryByTestId('input-radio-unchecked')).not.toBeChecked()
})
test('handles element with role="checkbox"', () => {
const {queryByTestId} = render(`
<div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />
<div role="checkbox" aria-checked="false" data-testid="aria-checkbox-unchecked" />
`)
expect(queryByTestId('aria-checkbox-checked')).toBeChecked()
expect(queryByTestId('aria-checkbox-unchecked')).not.toBeChecked()
})
test('handles element with role="radio"', () => {
const {queryByTestId} = render(`
<div role="radio" aria-checked="true" data-testid="aria-radio-checked" />
<div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" />
`)
expect(queryByTestId('aria-radio-checked')).toBeChecked()
expect(queryByTestId('aria-radio-unchecked')).not.toBeChecked()
})
test('handles element with role="switch"', () => {
const {queryByTestId} = render(`
<div role="switch" aria-checked="true" data-testid="aria-switch-checked" />
<div role="switch" aria-checked="false" data-testid="aria-switch-unchecked" />
`)
expect(queryByTestId('aria-switch-checked')).toBeChecked()
expect(queryByTestId('aria-switch-unchecked')).not.toBeChecked()
})
test('throws when checkbox input is checked but expected not to be', () => {
const {queryByTestId} = render(
`<input type="checkbox" checked data-testid="input-checked" />`,
)
expect(() =>
expect(queryByTestId('input-checked')).not.toBeChecked(),
).toThrowError()
})
test('throws when input checkbox is not checked but expected to be', () => {
const {queryByTestId} = render(
`<input type="checkbox" data-testid="input-empty" />`,
)
expect(() =>
expect(queryByTestId('input-empty')).toBeChecked(),
).toThrowError()
})
test('throws when element with role="checkbox" is checked but expected not to be', () => {
const {queryByTestId} = render(
`<div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox-checked')).not.toBeChecked(),
).toThrowError()
})
test('throws when element with role="checkbox" is not checked but expected to be', () => {
const {queryByTestId} = render(
`<div role="checkbox" aria-checked="false" data-testid="aria-checkbox-unchecked" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox-unchecked')).toBeChecked(),
).toThrowError()
})
test('throws when radio input is checked but expected not to be', () => {
const {queryByTestId} = render(
`<input type="radio" checked data-testid="input-radio-checked" />`,
)
expect(() =>
expect(queryByTestId('input-radio-checked')).not.toBeChecked(),
).toThrowError()
})
test('throws when input radio is not checked but expected to be', () => {
const {queryByTestId} = render(
`<input type="radio" data-testid="input-radio-unchecked" />`,
)
expect(() =>
expect(queryByTestId('input-radio-unchecked')).toBeChecked(),
).toThrowError()
})
test('throws when element with role="radio" is checked but expected not to be', () => {
const {queryByTestId} = render(
`<div role="radio" aria-checked="true" data-testid="aria-radio-checked" />`,
)
expect(() =>
expect(queryByTestId('aria-radio-checked')).not.toBeChecked(),
).toThrowError()
})
test('throws when element with role="radio" is not checked but expected to be', () => {
const {queryByTestId} = render(
`<div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox-unchecked')).toBeChecked(),
).toThrowError()
})
test('throws when element with role="switch" is checked but expected not to be', () => {
const {queryByTestId} = render(
`<div role="switch" aria-checked="true" data-testid="aria-switch-checked" />`,
)
expect(() =>
expect(queryByTestId('aria-switch-checked')).not.toBeChecked(),
).toThrowError()
})
test('throws when element with role="switch" is not checked but expected to be', () => {
const {queryByTestId} = render(
`<div role="switch" aria-checked="false" data-testid="aria-switch-unchecked" />`,
)
expect(() =>
expect(queryByTestId('aria-switch-unchecked')).toBeChecked(),
).toThrowError()
})
test('throws when element with role="checkbox" has an invalid aria-checked attribute', () => {
const {queryByTestId} = render(
`<div role="checkbox" aria-checked="something" data-testid="aria-checkbox-invalid" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox-invalid')).toBeChecked(),
).toThrowError(
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
)
})
test('throws when element with role="radio" has an invalid aria-checked attribute', () => {
const {queryByTestId} = render(
`<div role="radio" aria-checked="something" data-testid="aria-radio-invalid" />`,
)
expect(() =>
expect(queryByTestId('aria-radio-invalid')).toBeChecked(),
).toThrowError(
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
)
})
test('throws when element with role="switch" has an invalid aria-checked attribute', () => {
const {queryByTestId} = render(
`<div role="switch" aria-checked="something" data-testid="aria-switch-invalid" />`,
)
expect(() =>
expect(queryByTestId('aria-switch-invalid')).toBeChecked(),
).toThrowError(
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
)
})
test('throws when the element is not an input', () => {
const {queryByTestId} = render(`<select data-testid="select"></select>`)
expect(() => expect(queryByTestId('select')).toBeChecked()).toThrowError(
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
)
})
})
/* eslint max-lines-per-function:0 */