1
- import React from 'react'
2
- import { render , screen } from '@testing-library/react'
3
- import userEvent from '../../src'
1
+ import userEvent from '..'
2
+ import { setup } from './helpers/utils'
4
3
5
4
test ( 'should cycle elements in document tab order' , ( ) => {
6
- render (
5
+ setup ( `
7
6
<div>
8
7
<input data-testid="element" type="checkbox" />
9
8
<input data-testid="element" type="radio" />
10
9
<input data-testid="element" type="number" />
11
- </ div > ,
12
- )
10
+ </div>` )
13
11
14
- const [ checkbox , radio , number ] = screen . getAllByTestId ( 'element' )
12
+ const [ checkbox , radio , number ] = document . querySelectorAll (
13
+ '[data-testid="element"]' ,
14
+ )
15
15
16
16
expect ( document . body ) . toHaveFocus ( )
17
17
@@ -34,15 +34,16 @@ test('should cycle elements in document tab order', () => {
34
34
} )
35
35
36
36
test ( 'should go backwards when shift = true' , ( ) => {
37
- render (
37
+ setup ( `
38
38
<div>
39
39
<input data-testid="element" type="checkbox" />
40
40
<input data-testid="element" type="radio" />
41
41
<input data-testid="element" type="number" />
42
- </ div > ,
43
- )
42
+ </div>` )
44
43
45
- const [ checkbox , radio , number ] = screen . getAllByTestId ( 'element' )
44
+ const [ checkbox , radio , number ] = document . querySelectorAll (
45
+ '[data-testid="element"]' ,
46
+ )
46
47
47
48
radio . focus ( )
48
49
@@ -56,15 +57,16 @@ test('should go backwards when shift = true', () => {
56
57
} )
57
58
58
59
test ( 'should respect tabindex, regardless of dom position' , ( ) => {
59
- render (
60
+ setup ( `
60
61
<div>
61
- < input data-testid = "element" tabIndex = { 2 } type = "checkbox" />
62
- < input data-testid = "element" tabIndex = { 1 } type = "radio" />
63
- < input data-testid = "element" tabIndex = { 3 } type = "number" />
64
- </ div > ,
65
- )
62
+ <input data-testid="element" tabIndex="2" type="checkbox" />
63
+ <input data-testid="element" tabIndex="1" type="radio" />
64
+ <input data-testid="element" tabIndex="3" type="number" />
65
+ </div>` )
66
66
67
- const [ checkbox , radio , number ] = screen . getAllByTestId ( 'element' )
67
+ const [ checkbox , radio , number ] = document . querySelectorAll (
68
+ '[data-testid="element"]' ,
69
+ )
68
70
69
71
userEvent . tab ( )
70
72
@@ -84,15 +86,16 @@ test('should respect tabindex, regardless of dom position', () => {
84
86
} )
85
87
86
88
test ( 'should respect dom order when tabindex are all the same' , ( ) => {
87
- render (
89
+ setup ( `
88
90
<div>
89
- < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
90
- < input data-testid = "element" tabIndex = { 1 } type = "radio" />
91
- < input data-testid = "element" tabIndex = { 0 } type = "number" />
92
- </ div > ,
93
- )
91
+ <input data-testid="element" tabIndex="0" type="checkbox" />
92
+ <input data-testid="element" tabIndex="1" type="radio" />
93
+ <input data-testid="element" tabIndex="0" type="number" />
94
+ </div>` )
94
95
95
- const [ checkbox , radio , number ] = screen . getAllByTestId ( 'element' )
96
+ const [ checkbox , radio , number ] = document . querySelectorAll (
97
+ '[data-testid="element"]' ,
98
+ )
96
99
97
100
userEvent . tab ( )
98
101
@@ -112,15 +115,16 @@ test('should respect dom order when tabindex are all the same', () => {
112
115
} )
113
116
114
117
test ( 'should suport a mix of elements with/without tab index' , ( ) => {
115
- render (
118
+ setup ( `
116
119
<div>
117
- < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
118
- < input data-testid = "element" tabIndex = { 1 } type = "radio" />
120
+ <input data-testid="element" tabIndex="0" type="checkbox" />
121
+ <input data-testid="element" tabIndex="1" type="radio" />
119
122
<input data-testid="element" type="number" />
120
- </ div > ,
121
- )
123
+ </div>` )
122
124
123
- const [ checkbox , radio , number ] = screen . getAllByTestId ( 'element' )
125
+ const [ checkbox , radio , number ] = document . querySelectorAll (
126
+ '[data-testid="element"]' ,
127
+ )
124
128
125
129
userEvent . tab ( )
126
130
@@ -134,18 +138,17 @@ test('should suport a mix of elements with/without tab index', () => {
134
138
} )
135
139
136
140
test ( 'should not tab to <a> with no href' , ( ) => {
137
- render (
141
+ setup ( `
138
142
<div>
139
- < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
143
+ <input data-testid="element" tabIndex="0" type="checkbox" />
140
144
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
141
145
<a>ignore this</a>
142
146
<a data-testid="element" href="http://www.testingjavascript.com">
143
147
a link
144
148
</a>
145
- </ div > ,
146
- )
149
+ </div>` )
147
150
148
- const [ checkbox , link ] = screen . getAllByTestId ( ' element')
151
+ const [ checkbox , link ] = document . querySelectorAll ( '[data-testid=" element"] ')
149
152
150
153
userEvent . tab ( )
151
154
@@ -157,7 +160,7 @@ test('should not tab to <a> with no href', () => {
157
160
} )
158
161
159
162
test ( 'should stay within a focus trap' , ( ) => {
160
- render (
163
+ setup ( `
161
164
<>
162
165
<div data-testid="div1">
163
166
<input data-testid="element" type="checkbox" />
@@ -169,18 +172,20 @@ test('should stay within a focus trap', () => {
169
172
<input data-testid="element" foo="bar" type="radio" />
170
173
<input data-testid="element" foo="bar" type="number" />
171
174
</div>
172
- </ > ,
173
- )
175
+ </>` )
174
176
175
- const [ div1 , div2 ] = [ screen . getByTestId ( 'div1' ) , screen . getByTestId ( 'div2' ) ]
177
+ const [ div1 , div2 ] = [
178
+ document . querySelector ( '[data-testid="div1"]' ) ,
179
+ document . querySelector ( '[data-testid="div2"]' ) ,
180
+ ]
176
181
const [
177
182
checkbox1 ,
178
183
radio1 ,
179
184
number1 ,
180
185
checkbox2 ,
181
186
radio2 ,
182
187
number2 ,
183
- ] = screen . getAllByTestId ( ' element')
188
+ ] = document . querySelectorAll ( '[data-testid=" element"] ')
184
189
185
190
expect ( document . body ) . toHaveFocus ( )
186
191
@@ -228,34 +233,37 @@ test('should stay within a focus trap', () => {
228
233
test ( 'should support unstable sorting environments like node 10' , ( ) => {
229
234
const letters = 'abcdefghijklmnopqrstuvwxyz'
230
235
231
- render (
236
+ setup ( `
232
237
<div>
233
- { letters . split ( '' ) . map ( letter => (
234
- < input key = { letter } type = "text" data-testid = { letter } />
235
- ) ) }
236
- </ div > ,
237
- )
238
+ ${ letters
239
+ . split ( '' )
240
+ . map (
241
+ letter => `<input key=${ letter } type="text" data-testid=${ letter } />` ,
242
+ ) }
243
+ </div>` )
238
244
239
245
expect . assertions ( 26 )
240
246
241
247
letters . split ( '' ) . forEach ( letter => {
242
248
userEvent . tab ( )
243
- expect ( screen . getByTestId ( letter ) ) . toHaveFocus ( )
249
+ expect ( document . querySelector ( `[data-testid=" ${ letter } "]` ) ) . toHaveFocus ( )
244
250
} )
245
251
} )
246
252
247
253
test ( 'should not focus disabled elements' , ( ) => {
248
- render (
254
+ setup ( `
249
255
<div>
250
256
<input data-testid="one" />
251
- < input tabIndex = { - 1 } />
257
+ <input tabIndex="-1" />
252
258
<button disabled>click</button>
253
259
<input disabled />
254
260
<input data-testid="five" />
255
- </ div > ,
256
- )
261
+ </div>` )
257
262
258
- const [ one , five ] = [ screen . getByTestId ( 'one' ) , screen . getByTestId ( 'five' ) ]
263
+ const [ one , five ] = [
264
+ document . querySelector ( '[data-testid="one"]' ) ,
265
+ document . querySelector ( '[data-testid="five"]' ) ,
266
+ ]
259
267
260
268
userEvent . tab ( )
261
269
expect ( one ) . toHaveFocus ( )
@@ -265,8 +273,7 @@ test('should not focus disabled elements', () => {
265
273
} )
266
274
267
275
test ( 'should keep focus on the document if there are no enabled, focusable elements' , ( ) => {
268
- render ( < button disabled > no clicky</ button > )
269
-
276
+ setup ( `<button disabled>no clicky</button>` )
270
277
userEvent . tab ( )
271
278
expect ( document . body ) . toHaveFocus ( )
272
279
@@ -275,8 +282,8 @@ test('should keep focus on the document if there are no enabled, focusable eleme
275
282
} )
276
283
277
284
test ( 'should respect radio groups' , ( ) => {
278
- render (
279
- < >
285
+ setup ( `
286
+ <div >
280
287
<input
281
288
data-testid="element"
282
289
type="radio"
@@ -300,13 +307,12 @@ test('should respect radio groups', () => {
300
307
type="radio"
301
308
name="second"
302
309
value="second_right"
303
- defaultChecked
310
+ checked
304
311
/>
305
- </ > ,
306
- )
312
+ </div>` )
307
313
308
- const [ firstLeft , firstRight , , secondRight ] = screen . getAllByTestId (
309
- 'element' ,
314
+ const [ firstLeft , firstRight , , secondRight ] = document . querySelectorAll (
315
+ '[data-testid=" element"] ' ,
310
316
)
311
317
312
318
userEvent . tab ( )
0 commit comments