1
1
import { configure } from '../config'
2
- import { screen } from '..'
3
- import { renderIntoDocument } from './helpers/test-utils'
2
+ import { screen , getSuggestedQuery } from '..'
3
+ import { renderIntoDocument , render } from './helpers/test-utils'
4
4
5
5
beforeAll ( ( ) => {
6
6
configure ( { throwSuggestions : true } )
@@ -72,7 +72,7 @@ test(`should not suggest if the suggestion would give different results`, () =>
72
72
test ( 'should suggest by label over title' , ( ) => {
73
73
renderIntoDocument ( `<label><span>bar</span><input title="foo" /></label>` )
74
74
75
- expect ( ( ) => screen . getByTitle ( 'foo' ) ) . toThrowError ( / g e t B y L a b e l T e x t \( " b a r " \) / )
75
+ expect ( ( ) => screen . getByTitle ( 'foo' ) ) . toThrowError ( / g e t B y L a b e l T e x t \( ' b a r ' \) / )
76
76
} )
77
77
78
78
test ( 'should not suggest if there would be mixed suggestions' , ( ) => {
@@ -99,7 +99,7 @@ test('should suggest getByRole when used with getBy', () => {
99
99
100
100
expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
101
101
"A better query is available, try this:
102
- getByRole(" button" , {name: /submit/i})
102
+ getByRole(' button' , { name: /submit/i })
103
103
104
104
105
105
<body>
@@ -120,7 +120,7 @@ test('should suggest getAllByRole when used with getAllByTestId', () => {
120
120
expect ( ( ) => screen . getAllByTestId ( 'foo' ) )
121
121
. toThrowErrorMatchingInlineSnapshot ( `
122
122
"A better query is available, try this:
123
- getAllByRole(" button" , {name: /submit/i})
123
+ getAllByRole(' button' , { name: /submit/i })
124
124
125
125
126
126
<body>
@@ -148,18 +148,18 @@ test('should suggest findByRole when used with findByTestId', async () => {
148
148
` )
149
149
150
150
await expect ( screen . findByTestId ( 'foo' ) ) . rejects . toThrowError (
151
- / f i n d B y R o l e \( " b u t t o n " , \{ n a m e : \/ s u b m i t \/ i \} \) / ,
151
+ / f i n d B y R o l e \( ' b u t t o n ' , \{ n a m e : \/ s u b m i t \/ i \} \) / ,
152
152
)
153
153
await expect ( screen . findAllByTestId ( / f o o / ) ) . rejects . toThrowError (
154
- / f i n d A l l B y R o l e \( " b u t t o n " , \{ n a m e : \/ s u b m i t \/ i \} \) / ,
154
+ / f i n d A l l B y R o l e \( ' b u t t o n ' , \{ n a m e : \/ s u b m i t \/ i \} \) / ,
155
155
)
156
156
} )
157
157
158
158
test ( 'should suggest img role w/ alt text' , ( ) => {
159
159
renderIntoDocument ( `<img data-testid="img" alt="Incredibles 2 Poster" />` )
160
160
161
161
expect ( ( ) => screen . getByAltText ( 'Incredibles 2 Poster' ) ) . toThrowError (
162
- / g e t B y R o l e \( " i m g " , \{ n a m e : \/ i n c r e d i b l e s 2 p o s t e r \/ i \} \) / ,
162
+ / g e t B y R o l e \( ' i m g ' , \{ n a m e : \/ i n c r e d i b l e s 2 p o s t e r \/ i \} \) / ,
163
163
)
164
164
} )
165
165
@@ -169,7 +169,7 @@ test('escapes regular expressions in suggestion', () => {
169
169
)
170
170
171
171
expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowError (
172
- / g e t B y R o l e \( " i m g " , \{ n a m e : \/ t h e p r o b l e m \\ \( p i c t u r e o f a q u e s t i o n m a r k \\ \) \/ i \} \) / ,
172
+ / g e t B y R o l e \( ' i m g ' , \{ n a m e : \/ t h e p r o b l e m \\ \( p i c t u r e o f a q u e s t i o n m a r k \\ \) \/ i \} \) / ,
173
173
)
174
174
} )
175
175
@@ -178,7 +178,7 @@ test('should suggest getByLabelText when no role available', () => {
178
178
`<label for="foo">Username</label><input data-testid="foo" id="foo" />` ,
179
179
)
180
180
expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowError (
181
- / g e t B y L a b e l T e x t \( " U s e r n a m e " \) / ,
181
+ / g e t B y L a b e l T e x t \( ' U s e r n a m e ' \) / ,
182
182
)
183
183
} )
184
184
@@ -191,7 +191,7 @@ test(`should suggest getByLabel on non form elements`, () => {
191
191
` )
192
192
193
193
expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowError (
194
- / g e t B y L a b e l T e x t \( " S e c t i o n O n e " \) / ,
194
+ / g e t B y L a b e l T e x t \( ' S e c t i o n O n e ' \) / ,
195
195
)
196
196
} )
197
197
@@ -203,24 +203,24 @@ test.each([
203
203
renderIntoDocument ( html )
204
204
205
205
expect ( ( ) => screen . getByLabelText ( 'Username' ) ) . toThrowError (
206
- / g e t B y R o l e \( " t e x t b o x " , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
206
+ / g e t B y R o l e \( ' t e x t b o x ' , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
207
207
)
208
208
expect ( ( ) => screen . getAllByLabelText ( 'Username' ) ) . toThrowError (
209
- / g e t A l l B y R o l e \( " t e x t b o x " , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
209
+ / g e t A l l B y R o l e \( ' t e x t b o x ' , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
210
210
)
211
211
212
212
expect ( ( ) => screen . queryByLabelText ( 'Username' ) ) . toThrowError (
213
- / q u e r y B y R o l e \( " t e x t b o x " , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
213
+ / q u e r y B y R o l e \( ' t e x t b o x ' , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
214
214
)
215
215
expect ( ( ) => screen . queryAllByLabelText ( 'Username' ) ) . toThrowError (
216
- / q u e r y A l l B y R o l e \( " t e x t b o x " , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
216
+ / q u e r y A l l B y R o l e \( ' t e x t b o x ' , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
217
217
)
218
218
219
219
await expect ( screen . findByLabelText ( 'Username' ) ) . rejects . toThrowError (
220
- / f i n d B y R o l e \( " t e x t b o x " , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
220
+ / f i n d B y R o l e \( ' t e x t b o x ' , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
221
221
)
222
222
await expect ( screen . findAllByLabelText ( / U s e r n a m e / ) ) . rejects . toThrowError (
223
- / f i n d A l l B y R o l e \( " t e x t b o x " , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
223
+ / f i n d A l l B y R o l e \( ' t e x t b o x ' , \{ n a m e : \/ u s e r n a m e \/ i \} \) / ,
224
224
)
225
225
} )
226
226
@@ -230,23 +230,23 @@ test(`should suggest label over placeholder text`, () => {
230
230
)
231
231
232
232
expect ( ( ) => screen . getByPlaceholderText ( 'Username' ) ) . toThrowError (
233
- / g e t B y L a b e l T e x t \( " U s e r n a m e " \) / ,
233
+ / g e t B y L a b e l T e x t \( ' U s e r n a m e ' \) / ,
234
234
)
235
235
} )
236
236
237
237
test ( `should suggest getByPlaceholderText` , ( ) => {
238
238
renderIntoDocument ( `<input data-testid="foo" placeholder="Username" />` )
239
239
240
240
expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowError (
241
- / g e t B y P l a c e h o l d e r T e x t \( " U s e r n a m e " \) / ,
241
+ / g e t B y P l a c e h o l d e r T e x t \( ' U s e r n a m e ' \) / ,
242
242
)
243
243
} )
244
244
245
245
test ( `should suggest getByText for simple elements` , ( ) => {
246
246
renderIntoDocument ( `<div data-testid="foo">hello there</div>` )
247
247
248
248
expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowError (
249
- / g e t B y T e x t \( " h e l l o t h e r e " \) / ,
249
+ / g e t B y T e x t \( ' h e l l o t h e r e ' \) / ,
250
250
)
251
251
} )
252
252
@@ -256,7 +256,7 @@ test(`should suggest getByDisplayValue`, () => {
256
256
document . getElementById ( 'lastName' ) . value = 'Prine' // RIP John Prine
257
257
258
258
expect ( ( ) => screen . getByTestId ( 'lastName' ) ) . toThrowError (
259
- / g e t B y D i s p l a y V a l u e \( " P r i n e " \) / ,
259
+ / g e t B y D i s p l a y V a l u e \( ' P r i n e ' \) / ,
260
260
)
261
261
} )
262
262
@@ -269,10 +269,10 @@ test(`should suggest getByAltText`, () => {
269
269
` )
270
270
271
271
expect ( ( ) => screen . getByTestId ( 'input' ) ) . toThrowError (
272
- / g e t B y A l t T e x t \( " l a s t n a m e " \) / ,
272
+ / g e t B y A l t T e x t \( ' l a s t n a m e ' \) / ,
273
273
)
274
274
expect ( ( ) => screen . getByTestId ( 'area' ) ) . toThrowError (
275
- / g e t B y A l t T e x t \( " C o m p u t e r " \) / ,
275
+ / g e t B y A l t T e x t \( ' C o m p u t e r ' \) / ,
276
276
)
277
277
} )
278
278
@@ -285,25 +285,67 @@ test(`should suggest getByTitle`, () => {
285
285
</svg>` )
286
286
287
287
expect ( ( ) => screen . getByTestId ( 'delete' ) ) . toThrowError (
288
- / g e t B y T i t l e \( " D e l e t e " \) / ,
288
+ / g e t B y T i t l e \( ' D e l e t e ' \) / ,
289
289
)
290
290
expect ( ( ) => screen . getAllByTestId ( 'delete' ) ) . toThrowError (
291
- / g e t A l l B y T i t l e \( " D e l e t e " \) / ,
291
+ / g e t A l l B y T i t l e \( ' D e l e t e ' \) / ,
292
292
)
293
293
expect ( ( ) => screen . queryByTestId ( 'delete' ) ) . toThrowError (
294
- / q u e r y B y T i t l e \( " D e l e t e " \) / ,
294
+ / q u e r y B y T i t l e \( ' D e l e t e ' \) / ,
295
295
)
296
296
expect ( ( ) => screen . queryAllByTestId ( 'delete' ) ) . toThrowError (
297
- / q u e r y A l l B y T i t l e \( " D e l e t e " \) / ,
297
+ / q u e r y A l l B y T i t l e \( ' D e l e t e ' \) / ,
298
298
)
299
299
expect ( ( ) => screen . queryAllByTestId ( 'delete' ) ) . toThrowError (
300
- / q u e r y A l l B y T i t l e \( " D e l e t e " \) / ,
300
+ / q u e r y A l l B y T i t l e \( ' D e l e t e ' \) / ,
301
301
)
302
302
expect ( ( ) => screen . queryAllByTestId ( 'delete' ) ) . toThrowError (
303
- / q u e r y A l l B y T i t l e \( " D e l e t e " \) / ,
303
+ / q u e r y A l l B y T i t l e \( ' D e l e t e ' \) / ,
304
304
)
305
305
306
306
// Since `ByTitle` and `ByText` will both return the <title> element
307
307
// `getByText` will always be the suggested query as it is higher up the list.
308
- expect ( ( ) => screen . getByTestId ( 'svg' ) ) . toThrowError ( / g e t B y T e x t \( " C l o s e " \) / )
308
+ expect ( ( ) => screen . getByTestId ( 'svg' ) ) . toThrowError ( / g e t B y T e x t \( ' C l o s e ' \) / )
309
+ } )
310
+
311
+ test ( 'getSuggestedQuery handles `variant` and defaults to `get`' , ( ) => {
312
+ const button = render ( `<button>submit</button>` ) . container . firstChild
313
+
314
+ expect ( getSuggestedQuery ( button ) . toString ( ) ) . toMatch ( / g e t B y R o l e / )
315
+ expect ( getSuggestedQuery ( button , 'get' ) . toString ( ) ) . toMatch ( / g e t B y R o l e / )
316
+ expect ( getSuggestedQuery ( button , 'getAll' ) . toString ( ) ) . toMatch ( / g e t A l l B y R o l e / )
317
+ expect ( getSuggestedQuery ( button , 'query' ) . toString ( ) ) . toMatch ( / q u e r y B y R o l e / )
318
+ expect ( getSuggestedQuery ( button , 'queryAll' ) . toString ( ) ) . toMatch (
319
+ / q u e r y A l l B y R o l e / ,
320
+ )
321
+ expect ( getSuggestedQuery ( button , 'find' ) . toString ( ) ) . toMatch ( / f i n d B y R o l e / )
322
+ expect ( getSuggestedQuery ( button , 'findAll' ) . toString ( ) ) . toMatch (
323
+ / f i n d A l l B y R o l e / ,
324
+ )
325
+ } )
326
+
327
+ test ( 'getSuggestedQuery returns rich data for tooling' , ( ) => {
328
+ const button = render ( `<button>submit</button>` ) . container . firstChild
329
+
330
+ expect ( getSuggestedQuery ( button ) ) . toMatchObject ( {
331
+ queryName : 'Role' ,
332
+ queryMethod : 'getByRole' ,
333
+ queryArgs : [ 'button' , { name : / s u b m i t / i} ] ,
334
+ variant : 'get' ,
335
+ } )
336
+
337
+ expect ( getSuggestedQuery ( button ) . toString ( ) ) . toEqual (
338
+ `getByRole('button', { name: /submit/i })` ,
339
+ )
340
+
341
+ const div = render ( `<a>cancel</a>` ) . container . firstChild
342
+
343
+ expect ( getSuggestedQuery ( div ) ) . toMatchObject ( {
344
+ queryName : 'Text' ,
345
+ queryMethod : 'getByText' ,
346
+ queryArgs : [ 'cancel' ] ,
347
+ variant : 'get' ,
348
+ } )
349
+
350
+ expect ( getSuggestedQuery ( div ) . toString ( ) ) . toEqual ( `getByText('cancel')` )
309
351
} )
0 commit comments