/
to-be-partially-checked.js
122 lines (99 loc) · 4.52 KB
/
to-be-partially-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
import {render} from './helpers/test-utils'
describe('.toBePartiallyChecked', () => {
test('handles input checkbox with aria-checked', () => {
const {queryByTestId} = render(`
<input type="checkbox" aria-checked="mixed" data-testid="checkbox-mixed" />
<input type="checkbox" checked data-testid="checkbox-checked" />
<input type="checkbox" data-testid="checkbox-unchecked" />
`)
expect(queryByTestId('checkbox-mixed')).toBePartiallyChecked()
expect(queryByTestId('checkbox-checked')).not.toBePartiallyChecked()
expect(queryByTestId('checkbox-unchecked')).not.toBePartiallyChecked()
})
test('handles input checkbox set as indeterminate', () => {
const {queryByTestId} = render(`
<input type="checkbox" data-testid="checkbox-mixed" />
<input type="checkbox" checked data-testid="checkbox-checked" />
<input type="checkbox" data-testid="checkbox-unchecked" />
`)
queryByTestId('checkbox-mixed').indeterminate = true
expect(queryByTestId('checkbox-mixed')).toBePartiallyChecked()
expect(queryByTestId('checkbox-checked')).not.toBePartiallyChecked()
expect(queryByTestId('checkbox-unchecked')).not.toBePartiallyChecked()
})
test('handles element with role="checkbox"', () => {
const {queryByTestId} = render(`
<div role="checkbox" aria-checked="mixed" data-testid="aria-checkbox-mixed" />
<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-mixed')).toBePartiallyChecked()
expect(queryByTestId('aria-checkbox-checked')).not.toBePartiallyChecked()
expect(queryByTestId('aria-checkbox-unchecked')).not.toBePartiallyChecked()
})
test('throws when input checkbox is mixed but expected not to be', () => {
const {queryByTestId} = render(
`<input type="checkbox" aria-checked="mixed" data-testid="checkbox-mixed" />`,
)
expect(() =>
expect(queryByTestId('checkbox-mixed')).not.toBePartiallyChecked(),
).toThrowError()
})
test('throws when input checkbox is indeterminate but expected not to be', () => {
const {queryByTestId} = render(
`<input type="checkbox" data-testid="checkbox-mixed" />`,
)
queryByTestId('checkbox-mixed').indeterminate = true
expect(() =>
expect(queryByTestId('input-mixed')).not.toBePartiallyChecked(),
).toThrowError()
})
test('throws when input checkbox is not checked but expected to be', () => {
const {queryByTestId} = render(
`<input type="checkbox" data-testid="checkbox-empty" />`,
)
expect(() =>
expect(queryByTestId('checkbox-empty')).toBePartiallyChecked(),
).toThrowError()
})
test('throws when element with role="checkbox" is partially checked but expected not to be', () => {
const {queryByTestId} = render(
`<div role="checkbox" aria-checked="mixed" data-testid="aria-checkbox-mixed" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox-mixed')).not.toBePartiallyChecked(),
).toThrowError()
})
test('throws when element with role="checkbox" is checked but expected to be partially checked', () => {
const {queryByTestId} = render(
`<div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox-checked')).toBePartiallyChecked(),
).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" />`,
)
expect(() =>
expect(queryByTestId('aria-checkbox')).toBePartiallyChecked(),
).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')).toBePartiallyChecked(),
).toThrowError()
})
test('throws when the element is not a checkbox', () => {
const {queryByTestId} = render(`<select data-testid="select"></select>`)
expect(() =>
expect(queryByTestId('select')).toBePartiallyChecked(),
).toThrowError(
'only inputs with type="checkbox" or elements with role="checkbox" and a valid aria-checked attribute can be used with .toBePartiallyChecked(). Use .toHaveValue() instead',
)
})
})