/
to-have-description.js
138 lines (115 loc) · 4.28 KB
/
to-have-description.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
import {render} from './helpers/test-utils'
describe('.toHaveDescription', () => {
test('handles positive test cases', () => {
const {queryByTestId} = render(`
<div id="description">The description</div>
<div data-testid="single" aria-describedby="description"></div>
<div data-testid="invalid_id" aria-describedby="invalid"></div>
<div data-testid="without"></div>
`)
expect(queryByTestId('single')).toHaveDescription('The description')
expect(queryByTestId('single')).toHaveDescription(
expect.stringContaining('The'),
)
expect(queryByTestId('single')).toHaveDescription(/The/)
expect(queryByTestId('single')).toHaveDescription(
expect.stringMatching(/The/),
)
expect(queryByTestId('single')).toHaveDescription(/description/)
expect(queryByTestId('single')).not.toHaveDescription('Something else')
expect(queryByTestId('single')).not.toHaveDescription('The')
expect(queryByTestId('invalid_id')).not.toHaveDescription()
expect(queryByTestId('invalid_id')).toHaveDescription('')
expect(queryByTestId('without')).not.toHaveDescription()
expect(queryByTestId('without')).toHaveDescription('')
})
test('handles multiple ids', () => {
const {queryByTestId} = render(`
<div id="first">First description</div>
<div id="second">Second description</div>
<div id="third">Third description</div>
<div data-testid="multiple" aria-describedby="first second third"></div>
`)
expect(queryByTestId('multiple')).toHaveDescription(
'First description Second description Third description',
)
expect(queryByTestId('multiple')).toHaveDescription(
/Second description Third/,
)
expect(queryByTestId('multiple')).toHaveDescription(
expect.stringContaining('Second description Third'),
)
expect(queryByTestId('multiple')).toHaveDescription(
expect.stringMatching(/Second description Third/),
)
expect(queryByTestId('multiple')).not.toHaveDescription('Something else')
expect(queryByTestId('multiple')).not.toHaveDescription('First')
})
test('handles negative test cases', () => {
const {queryByTestId} = render(`
<div id="description">The description</div>
<div data-testid="target" aria-describedby="description"></div>
`)
expect(() =>
expect(queryByTestId('other')).toHaveDescription('The description'),
).toThrowError()
expect(() =>
expect(queryByTestId('target')).toHaveDescription('Something else'),
).toThrowError()
expect(() =>
expect(queryByTestId('target')).not.toHaveDescription('The description'),
).toThrowError()
})
test('normalizes whitespace', () => {
const {queryByTestId} = render(`
<div id="first">
Step
1
of
4
</div>
<div id="second">
And
extra
description
</div>
<div data-testid="target" aria-describedby="first second"></div>
`)
expect(queryByTestId('target')).toHaveDescription(
'Step 1 of 4 And extra description',
)
})
test('can handle multiple levels with content spread across decendants', () => {
const {queryByTestId} = render(`
<span id="description">
<span>Step</span>
<span> 1</span>
<span><span>of</span></span>
4</span>
</span>
<div data-testid="target" aria-describedby="description"></div>
`)
expect(queryByTestId('target')).toHaveDescription('Step 1 of 4')
})
test('handles extra whitespace with multiple ids', () => {
const {queryByTestId} = render(`
<div id="first">First description</div>
<div id="second">Second description</div>
<div id="third">Third description</div>
<div data-testid="multiple" aria-describedby=" first
second third
"></div>
`)
expect(queryByTestId('multiple')).toHaveDescription(
'First description Second description Third description',
)
})
test('is case-sensitive', () => {
const {queryByTestId} = render(`
<span id="description">Sensitive text</span>
<div data-testid="target" aria-describedby="description"></div>
`)
expect(queryByTestId('target')).toHaveDescription('Sensitive text')
expect(queryByTestId('target')).not.toHaveDescription('sensitive text')
})
})