@@ -4,156 +4,219 @@ import "@testing-library/jest-dom/extend-expect";
4
4
import userEvent from "../../src" ;
5
5
6
6
describe ( "userEvent.tab" , ( ) => {
7
- it ( "should cycle elements in document tab order" , ( ) => {
8
- const { getAllByTestId } = render (
9
- < div >
10
- < input data-testid = "element" type = "checkbox" />
11
- < input data-testid = "element" type = "radio" />
12
- < input data-testid = "element" type = "number" />
13
- </ div >
14
- ) ;
7
+ it ( "should cycle elements in document tab order" , ( ) => {
8
+ const { getAllByTestId } = render (
9
+ < div >
10
+ < input data-testid = "element" type = "checkbox" />
11
+ < input data-testid = "element" type = "radio" />
12
+ < input data-testid = "element" type = "number" />
13
+ </ div >
14
+ ) ;
15
15
16
- const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
16
+ const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
17
17
18
- expect ( document . activeElement ) . toBe ( document . body ) ;
18
+ expect ( document . body ) . toHaveFocus ( ) ;
19
19
20
- userEvent . tab ( ) ;
20
+ userEvent . tab ( ) ;
21
21
22
- expect ( document . activeElement ) . toBe ( checkbox ) ;
22
+ expect ( checkbox ) . toHaveFocus ( ) ;
23
23
24
- userEvent . tab ( ) ;
24
+ userEvent . tab ( ) ;
25
25
26
- expect ( document . activeElement ) . toBe ( radio ) ;
26
+ expect ( radio ) . toHaveFocus ( ) ;
27
27
28
- userEvent . tab ( ) ;
28
+ userEvent . tab ( ) ;
29
29
30
- expect ( document . activeElement ) . toBe ( number ) ;
30
+ expect ( number ) . toHaveFocus ( ) ;
31
31
32
- userEvent . tab ( ) ;
32
+ userEvent . tab ( ) ;
33
33
34
- // cycle goes back to first element
35
- expect ( document . activeElement ) . toBe ( checkbox ) ;
36
- } ) ;
34
+ // cycle goes back to first element
35
+ expect ( checkbox ) . toHaveFocus ( ) ;
36
+ } ) ;
37
37
38
- it ( "should go backwards when shift = true" , ( ) => {
39
- const { getAllByTestId } = render (
40
- < div >
41
- < input data-testid = "element" type = "checkbox" />
42
- < input data-testid = "element" type = "radio" />
43
- < input data-testid = "element" type = "number" />
44
- </ div >
45
- ) ;
38
+ it ( "should go backwards when shift = true" , ( ) => {
39
+ const { getAllByTestId } = render (
40
+ < div >
41
+ < input data-testid = "element" type = "checkbox" />
42
+ < input data-testid = "element" type = "radio" />
43
+ < input data-testid = "element" type = "number" />
44
+ </ div >
45
+ ) ;
46
46
47
- const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
47
+ const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
48
48
49
- radio . focus ( ) ;
49
+ radio . focus ( ) ;
50
50
51
- userEvent . tab ( { shift : true } ) ;
51
+ userEvent . tab ( { shift : true } ) ;
52
52
53
- expect ( document . activeElement ) . toBe ( checkbox ) ;
53
+ expect ( checkbox ) . toHaveFocus ( ) ;
54
54
55
- userEvent . tab ( { shift : true } ) ;
55
+ userEvent . tab ( { shift : true } ) ;
56
56
57
- expect ( document . activeElement ) . toBe ( number ) ;
58
- } ) ;
57
+ expect ( number ) . toHaveFocus ( ) ;
58
+ } ) ;
59
59
60
- it ( "should respect tabindex, regardless of dom position" , ( ) => {
61
- const { getAllByTestId } = render (
62
- < div >
63
- < input data-testid = "element" tabIndex = { 2 } type = "checkbox" />
64
- < input data-testid = "element" tabIndex = { 1 } type = "radio" />
65
- < input data-testid = "element" tabIndex = { 3 } type = "number" />
66
- </ div >
67
- ) ;
60
+ it ( "should respect tabindex, regardless of dom position" , ( ) => {
61
+ const { getAllByTestId } = render (
62
+ < div >
63
+ < input data-testid = "element" tabIndex = { 2 } type = "checkbox" />
64
+ < input data-testid = "element" tabIndex = { 1 } type = "radio" />
65
+ < input data-testid = "element" tabIndex = { 3 } type = "number" />
66
+ </ div >
67
+ ) ;
68
68
69
- const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
69
+ const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
70
70
71
- userEvent . tab ( ) ;
71
+ userEvent . tab ( ) ;
72
72
73
- expect ( document . activeElement ) . toBe ( radio ) ;
73
+ expect ( radio ) . toHaveFocus ( ) ;
74
74
75
- userEvent . tab ( ) ;
75
+ userEvent . tab ( ) ;
76
76
77
- expect ( document . activeElement ) . toBe ( checkbox ) ;
77
+ expect ( checkbox ) . toHaveFocus ( ) ;
78
78
79
- userEvent . tab ( ) ;
79
+ userEvent . tab ( ) ;
80
80
81
- expect ( document . activeElement ) . toBe ( number ) ;
81
+ expect ( number ) . toHaveFocus ( ) ;
82
82
83
- userEvent . tab ( ) ;
83
+ userEvent . tab ( ) ;
84
84
85
- expect ( document . activeElement ) . toBe ( radio ) ;
86
- } ) ;
85
+ expect ( radio ) . toHaveFocus ( ) ;
86
+ } ) ;
87
87
88
- it ( ' should respect dom order when tabindex are all the same' , ( ) => {
89
- const { getAllByTestId } = render (
90
- < div >
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 >
95
- ) ;
88
+ it ( " should respect dom order when tabindex are all the same" , ( ) => {
89
+ const { getAllByTestId } = render (
90
+ < div >
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 >
95
+ ) ;
96
96
97
- const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
97
+ const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
98
98
99
- userEvent . tab ( ) ;
99
+ userEvent . tab ( ) ;
100
100
101
- expect ( document . activeElement ) . toBe ( checkbox ) ;
101
+ expect ( checkbox ) . toHaveFocus ( ) ;
102
102
103
- userEvent . tab ( ) ;
103
+ userEvent . tab ( ) ;
104
104
105
- expect ( document . activeElement ) . toBe ( number ) ;
105
+ expect ( number ) . toHaveFocus ( ) ;
106
106
107
- userEvent . tab ( ) ;
107
+ userEvent . tab ( ) ;
108
108
109
- expect ( document . activeElement ) . toBe ( radio ) ;
109
+ expect ( radio ) . toHaveFocus ( ) ;
110
110
111
- userEvent . tab ( ) ;
111
+ userEvent . tab ( ) ;
112
112
113
- expect ( document . activeElement ) . toBe ( checkbox ) ;
114
- } ) ;
113
+ expect ( checkbox ) . toHaveFocus ( ) ;
114
+ } ) ;
115
115
116
- it ( ' should suport a mix of elements with/without tab index' , ( ) => {
117
- const { getAllByTestId } = render (
118
- < div >
119
- < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
120
- < input data-testid = "element" tabIndex = { 1 } type = "radio" />
121
- < input data-testid = "element" type = "number" />
122
- </ div >
123
- ) ;
116
+ it ( " should suport a mix of elements with/without tab index" , ( ) => {
117
+ const { getAllByTestId } = render (
118
+ < div >
119
+ < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
120
+ < input data-testid = "element" tabIndex = { 1 } type = "radio" />
121
+ < input data-testid = "element" type = "number" />
122
+ </ div >
123
+ ) ;
124
124
125
- const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
125
+ const [ checkbox , radio , number ] = getAllByTestId ( "element" ) ;
126
126
127
- userEvent . tab ( ) ;
127
+ userEvent . tab ( ) ;
128
128
129
- expect ( document . activeElement ) . toBe ( checkbox ) ;
130
- userEvent . tab ( ) ;
129
+ expect ( checkbox ) . toHaveFocus ( ) ;
130
+ userEvent . tab ( ) ;
131
131
132
- expect ( document . activeElement ) . toBe ( number ) ;
133
- userEvent . tab ( ) ;
132
+ expect ( number ) . toHaveFocus ( ) ;
133
+ userEvent . tab ( ) ;
134
134
135
- expect ( document . activeElement ) . toBe ( radio ) ;
135
+ expect ( radio ) . toHaveFocus ( ) ;
136
+ } ) ;
136
137
137
- } ) ;
138
+ it ( "should not tab to <a> with no href" , ( ) => {
139
+ const { getAllByTestId } = render (
140
+ < div >
141
+ < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
142
+ < a > ignore this</ a >
143
+ < a data-testid = "element" href = "http://www.testingjavascript.com" >
144
+ a link
145
+ </ a >
146
+ </ div >
147
+ ) ;
138
148
139
- it ( 'should not tab to <a> with no href' , ( ) => {
140
- const { getAllByTestId } = render (
141
- < div >
142
- < input data-testid = "element" tabIndex = { 0 } type = "checkbox" />
143
- < a > ignore this</ a >
144
- < a data-testid = "element" href = "http://www.testingjavascript.com" > a link</ a >
145
- </ div >
146
- ) ;
149
+ const [ checkbox , link ] = getAllByTestId ( "element" ) ;
147
150
148
- const [ checkbox , link ] = getAllByTestId ( "element" ) ;
151
+ userEvent . tab ( ) ;
149
152
150
- userEvent . tab ( ) ;
153
+ expect ( checkbox ) . toHaveFocus ( ) ;
151
154
152
- expect ( document . activeElement ) . toBe ( checkbox ) ;
155
+ userEvent . tab ( ) ;
153
156
154
- userEvent . tab ( ) ;
157
+ expect ( link ) . toHaveFocus ( ) ;
158
+ } ) ;
155
159
156
- expect ( document . activeElement ) . toBe ( link ) ;
157
- } ) ;
160
+ it ( "should stay within a focus trab" , ( ) => {
161
+ const { getAllByTestId, getByTestId } = render (
162
+ < >
163
+ < div data-testid = "div1" >
164
+ < input data-testid = "element" type = "checkbox" />
165
+ < input data-testid = "element" type = "radio" />
166
+ < input data-testid = "element" type = "number" />
167
+ </ div >
168
+ < div data-testid = "div2" >
169
+ < input data-testid = "element" foo = "bar" type = "checkbox" />
170
+ < input data-testid = "element" foo = "bar" type = "radio" />
171
+ < input data-testid = "element" foo = "bar" type = "number" />
172
+ </ div >
173
+ </ >
174
+ ) ;
158
175
176
+ const [ div1 , div2 ] = [ getByTestId ( "div1" ) , getByTestId ( "div2" ) ] ;
177
+ const [
178
+ checkbox1 ,
179
+ radio1 ,
180
+ number1 ,
181
+ checkbox2 ,
182
+ radio2 ,
183
+ number2
184
+ ] = getAllByTestId ( "element" ) ;
185
+
186
+ expect ( document . body ) . toHaveFocus ( ) ;
187
+
188
+ userEvent . tab ( { focusTrap : div1 } ) ;
189
+
190
+ expect ( checkbox1 ) . toHaveFocus ( ) ;
191
+
192
+ userEvent . tab ( { focusTrap : div1 } ) ;
193
+
194
+ expect ( radio1 ) . toHaveFocus ( ) ;
195
+
196
+ userEvent . tab ( { focusTrap : div1 } ) ;
197
+
198
+ expect ( number1 ) . toHaveFocus ( ) ;
199
+
200
+ userEvent . tab ( { focusTrap : div1 } ) ;
201
+
202
+ // cycle goes back to first element
203
+ expect ( checkbox1 ) . toHaveFocus ( ) ;
204
+
205
+ userEvent . tab ( { focusTrap : div2 } ) ;
206
+
207
+ expect ( checkbox2 ) . toHaveFocus ( ) ;
208
+
209
+ userEvent . tab ( { focusTrap : div2 } ) ;
210
+
211
+ expect ( radio2 ) . toHaveFocus ( ) ;
212
+
213
+ userEvent . tab ( { focusTrap : div2 } ) ;
214
+
215
+ expect ( number2 ) . toHaveFocus ( ) ;
216
+
217
+ userEvent . tab ( { focusTrap : div2 } ) ;
218
+
219
+ // cycle goes back to first element
220
+ expect ( checkbox2 ) . toHaveFocus ( ) ;
221
+ } ) ;
159
222
} ) ;
0 commit comments