/
suggestions.js
355 lines (287 loc) 路 10.6 KB
/
suggestions.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import {configure} from '../config'
import {screen, getSuggestedQuery} from '..'
import {renderIntoDocument, render} from './helpers/test-utils'
beforeAll(() => {
configure({throwSuggestions: true})
})
afterAll(() => {
configure({throwSuggestions: false})
})
test('does not suggest for nested inline style', () => {
renderIntoDocument(
`<div data-testid="style"><style>.hsuHs{margin:auto}.wFncld{margin-top:3px;color:#9AA0A6;height:20px;width:20px}</style></div>`,
)
expect(() => screen.getByTestId('style')).not.toThrow()
})
test('does not suggest for inline script, style', () => {
renderIntoDocument(
`<script data-testid="script">alert('hello')</script><style data-testid="style">.hsuHs{margin:auto}.wFncld{margin-top:3px;color:#9AA0A6;height:20px;width:20px}</style>`,
)
expect(() => screen.getByTestId('script')).not.toThrow()
expect(() => screen.getByTestId('style')).not.toThrow()
})
test('respects ignores', () => {
renderIntoDocument(`<my-thing>foo</my-thing>`)
expect(() =>
screen.queryByText('foo', {ignore: 'my-thing'}),
).not.toThrowError()
})
test('does not suggest query that would give a different element', () => {
renderIntoDocument(`
<div data-testid="foo"><img src="foo" /></div>
<div data-testid="bar"><a href="/foo"><div role="figure"><img src="foo" /></div></a></div>
<a data-testid="baz"><h1>link text</h1></a>
`)
expect(() => screen.getByTestId('foo')).not.toThrowError()
expect(() => screen.getByTestId('bar')).not.toThrowError()
expect(() => screen.getByTestId('baz')).not.toThrowError()
})
test('does not suggest when using getByRole', () => {
renderIntoDocument(`<button data-testid="foo">submit</button>`)
expect(() => screen.getByRole('button', {name: /submit/i})).not.toThrowError()
})
test('should not suggest when nothing available', () => {
renderIntoDocument(`<span data-testid="foo" />`)
expect(() => screen.queryByTestId('foo')).not.toThrowError()
})
test(`should not suggest if the suggestion would give different results`, () => {
renderIntoDocument(`
<input type="text" data-testid="foo" /><span data-testid="foo" />
`)
expect(() =>
screen.getAllByTestId('foo', {suggest: false}),
).not.toThrowError()
})
test('should suggest by label over title', () => {
renderIntoDocument(`<label><span>bar</span><input title="foo" /></label>`)
expect(() => screen.getByTitle('foo')).toThrowError(
/getByLabelText\(\/bar\/i\)/,
)
})
test('should not suggest if there would be mixed suggestions', () => {
renderIntoDocument(`
<button data-testid="foo">submit</button>
<label for="foo">Username</label><input data-testid="foo" id="foo" />`)
expect(() => screen.getAllByTestId('foo')).not.toThrowError()
})
test('should not suggest when suggest is turned off for a query', () => {
renderIntoDocument(`
<button data-testid="foo">submit</button>
<button data-testid="foot">another</button>`)
expect(() => screen.getByTestId('foo', {suggest: false})).not.toThrowError()
expect(() =>
screen.getAllByTestId(/foo/, {suggest: false}),
).not.toThrowError()
})
test('should suggest getByRole when used with getBy', () => {
renderIntoDocument(`<button data-testid="foo">submit</button>`)
expect(() => screen.getByTestId('foo')).toThrowErrorMatchingInlineSnapshot(`
"A better query is available, try this:
getByRole('button', { name: /submit/i })
<body>
<button
data-testid="foo"
>
submit
</button>
</body>"
`)
})
test('should suggest getAllByRole when used with getAllByTestId', () => {
renderIntoDocument(`
<button data-testid="foo">submit</button>
<button data-testid="foo">submit</button>`)
expect(() => screen.getAllByTestId('foo'))
.toThrowErrorMatchingInlineSnapshot(`
"A better query is available, try this:
getAllByRole('button', { name: /submit/i })
<body>
<button
data-testid="foo"
>
submit
</button>
<button
data-testid="foo"
>
submit
</button>
</body>"
`)
})
test('should suggest findByRole when used with findByTestId', async () => {
renderIntoDocument(`
<button data-testid="foo">submit</button>
<button data-testid="foot">submit</button>
`)
await expect(screen.findByTestId('foo')).rejects.toThrowError(
/findByRole\('button', \{ name: \/submit\/i \}\)/,
)
await expect(screen.findAllByTestId(/foo/)).rejects.toThrowError(
/findAllByRole\('button', \{ name: \/submit\/i \}\)/,
)
})
test('should suggest img role w/ alt text', () => {
renderIntoDocument(`<img data-testid="img" alt="Incredibles 2 Poster" />`)
expect(() => screen.getByAltText('Incredibles 2 Poster')).toThrowError(
/getByRole\('img', \{ name: \/incredibles 2 poster\/i \}\)/,
)
})
test('escapes regular expressions in suggestion', () => {
renderIntoDocument(
`<img src="foo.png" alt="The Problem (picture of a question mark)" data-testid="foo" />`,
)
expect(() => screen.getByTestId('foo')).toThrowError(
/getByRole\('img', \{ name: \/the problem \\\(picture of a question mark\\\)\/i \}\)/,
)
})
test('should suggest getByLabelText when no role available', () => {
renderIntoDocument(
`<label for="foo">Username</label><input data-testid="foo" id="foo" />`,
)
expect(() => screen.getByTestId('foo')).toThrowError(
/getByLabelText\(\/username\/i\)/,
)
})
test(`should suggest getByLabel on non form elements`, () => {
renderIntoDocument(`
<div data-testid="foo" aria-labelledby="section-one-header">
<span id="section-one-header">Section One</span>
<p>some content</p>
</div>
`)
expect(() => screen.getByTestId('foo')).toThrowError(
/getByLabelText\(\/section one\/i\)/,
)
})
test.each([
`<label id="username-label">Username</label><input aria-labelledby="username-label" type="text" />`,
`<label><span>Username</span><input type="text" /></label>`,
`<label for="foo">Username</label><input id="foo" type="text" />`,
])('%s\nshould suggest getByRole over', async html => {
renderIntoDocument(html)
expect(() => screen.getByLabelText('Username')).toThrowError(
/getByRole\('textbox', \{ name: \/username\/i \}\)/,
)
expect(() => screen.getAllByLabelText('Username')).toThrowError(
/getAllByRole\('textbox', \{ name: \/username\/i \}\)/,
)
expect(() => screen.queryByLabelText('Username')).toThrowError(
/queryByRole\('textbox', \{ name: \/username\/i \}\)/,
)
expect(() => screen.queryAllByLabelText('Username')).toThrowError(
/queryAllByRole\('textbox', \{ name: \/username\/i \}\)/,
)
await expect(screen.findByLabelText('Username')).rejects.toThrowError(
/findByRole\('textbox', \{ name: \/username\/i \}\)/,
)
await expect(screen.findAllByLabelText(/Username/)).rejects.toThrowError(
/findAllByRole\('textbox', \{ name: \/username\/i \}\)/,
)
})
test(`should suggest label over placeholder text`, () => {
renderIntoDocument(
`<label for="foo">Username</label><input id="foo" data-testid="foo" placeholder="Username" />`,
)
expect(() => screen.getByPlaceholderText('Username')).toThrowError(
/getByLabelText\(\/username\/i\)/,
)
})
test(`should suggest getByPlaceholderText`, () => {
renderIntoDocument(`<input data-testid="foo" placeholder="Username" />`)
expect(() => screen.getByTestId('foo')).toThrowError(
/getByPlaceholderText\(\/username\/i\)/,
)
})
test(`should suggest getByText for simple elements`, () => {
renderIntoDocument(`<div data-testid="foo">hello there</div>`)
expect(() => screen.getByTestId('foo')).toThrowError(
/getByText\(\/hello there\/i\)/,
)
})
test(`should suggest getByDisplayValue`, () => {
renderIntoDocument(`<input id="lastName" data-testid="lastName" />`)
document.getElementById('lastName').value = 'Prine' // RIP John Prine
expect(() => screen.getByTestId('lastName')).toThrowError(
/getByDisplayValue\(\/prine\/i\)/,
)
})
test(`should suggest getByAltText`, () => {
renderIntoDocument(`
<input data-testid="input" alt="last name" />
<map name="workmap">
<area data-testid="area" shape="rect" coords="34,44,270,350" alt="Computer">
</map>
`)
expect(() => screen.getByTestId('input')).toThrowError(
/getByAltText\(\/last name\/i\)/,
)
expect(() => screen.getByTestId('area')).toThrowError(
/getByAltText\(\/computer\/i\)/,
)
})
test(`should suggest getByTitle`, () => {
renderIntoDocument(`
<span title="Delete" data-testid="delete"></span>
<svg>
<title data-testid="svg">Close</title>
<g><path /></g>
</svg>`)
expect(() => screen.getByTestId('delete')).toThrowError(
/getByTitle\(\/delete\/i\)/,
)
expect(() => screen.getAllByTestId('delete')).toThrowError(
/getAllByTitle\(\/delete\/i\)/,
)
expect(() => screen.queryByTestId('delete')).toThrowError(
/queryByTitle\(\/delete\/i\)/,
)
expect(() => screen.queryAllByTestId('delete')).toThrowError(
/queryAllByTitle\(\/delete\/i\)/,
)
expect(() => screen.queryAllByTestId('delete')).toThrowError(
/queryAllByTitle\(\/delete\/i\)/,
)
expect(() => screen.queryAllByTestId('delete')).toThrowError(
/queryAllByTitle\(\/delete\/i\)/,
)
// Since `ByTitle` and `ByText` will both return the <title> element
// `getByText` will always be the suggested query as it is higher up the list.
expect(() => screen.getByTestId('svg')).toThrowError(
/getByText\(\/close\/i\)/,
)
})
test('getSuggestedQuery handles `variant` and defaults to `get`', () => {
const button = render(`<button>submit</button>`).container.firstChild
expect(getSuggestedQuery(button).toString()).toMatch(/getByRole/)
expect(getSuggestedQuery(button, 'get').toString()).toMatch(/getByRole/)
expect(getSuggestedQuery(button, 'getAll').toString()).toMatch(/getAllByRole/)
expect(getSuggestedQuery(button, 'query').toString()).toMatch(/queryByRole/)
expect(getSuggestedQuery(button, 'queryAll').toString()).toMatch(
/queryAllByRole/,
)
expect(getSuggestedQuery(button, 'find').toString()).toMatch(/findByRole/)
expect(getSuggestedQuery(button, 'findAll').toString()).toMatch(
/findAllByRole/,
)
})
test('getSuggestedQuery returns rich data for tooling', () => {
const button = render(`<button>submit</button>`).container.firstChild
expect(getSuggestedQuery(button)).toMatchObject({
queryName: 'Role',
queryMethod: 'getByRole',
queryArgs: ['button', {name: /submit/i}],
variant: 'get',
})
expect(getSuggestedQuery(button).toString()).toEqual(
`getByRole('button', { name: /submit/i })`,
)
const div = render(`<a>cancel</a>`).container.firstChild
expect(getSuggestedQuery(div)).toMatchObject({
queryName: 'Text',
queryMethod: 'getByText',
queryArgs: [/cancel/i],
variant: 'get',
})
expect(getSuggestedQuery(div).toString()).toEqual(`getByText(/cancel/i)`)
})