9
9
10
10
'use strict' ;
11
11
12
- import {
13
- blur ,
14
- focus ,
15
- keydown ,
16
- setPointerEvent ,
17
- platform ,
18
- dispatchPointerDown ,
19
- dispatchPointerUp ,
20
- } from '../test-utils' ;
12
+ import { createEventTarget , setPointerEvent , platform } from '../testing-library' ;
21
13
22
14
let React ;
23
15
let ReactFeatureFlags ;
@@ -73,9 +65,9 @@ describe.each(table)('Focus responder', hasPointerEvents => {
73
65
} ) ;
74
66
75
67
it ( 'does not call callbacks' , ( ) => {
76
- const dispatch = arg => ref . current . dispatchEvent ( arg ) ;
77
- dispatch ( focus ( ) ) ;
78
- dispatch ( blur ( ) ) ;
68
+ const target = createEventTarget ( ref . current ) ;
69
+ target . focus ( ) ;
70
+ target . blur ( ) ;
79
71
expect ( onFocus ) . not . toBeCalled ( ) ;
80
72
expect ( onBlur ) . not . toBeCalled ( ) ;
81
73
} ) ;
@@ -97,9 +89,9 @@ describe.each(table)('Focus responder', hasPointerEvents => {
97
89
} ) ;
98
90
99
91
it ( 'is called after "blur" event' , ( ) => {
100
- const dispatch = arg => ref . current . dispatchEvent ( arg ) ;
101
- dispatch ( focus ( ) ) ;
102
- dispatch ( blur ( ) ) ;
92
+ const target = createEventTarget ( ref . current ) ;
93
+ target . focus ( ) ;
94
+ target . blur ( ) ;
103
95
expect ( onBlur ) . toHaveBeenCalledTimes ( 1 ) ;
104
96
} ) ;
105
97
} ) ;
@@ -127,29 +119,32 @@ describe.each(table)('Focus responder', hasPointerEvents => {
127
119
beforeEach ( componentInit ) ;
128
120
129
121
it ( 'is called after "focus" event' , ( ) => {
130
- ref . current . dispatchEvent ( focus ( ) ) ;
122
+ const target = createEventTarget ( ref . current ) ;
123
+ target . focus ( ) ;
131
124
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
132
125
} ) ;
133
126
134
127
it ( 'is not called if descendants of target receive focus' , ( ) => {
135
- innerRef . current . dispatchEvent ( focus ( ) ) ;
128
+ const target = createEventTarget ( innerRef . current ) ;
129
+ target . focus ( ) ;
136
130
expect ( onFocus ) . not . toBeCalled ( ) ;
137
131
} ) ;
138
132
139
133
it ( 'is called with the correct pointerType: mouse' , ( ) => {
140
- const target = ref . current ;
141
- dispatchPointerDown ( target , { pointerType : 'mouse' } ) ;
142
- dispatchPointerUp ( target , { pointerType : 'mouse' } ) ;
134
+ const target = createEventTarget ( ref . current ) ;
135
+ target . pointerdown ( ) ;
136
+ target . pointerup ( ) ;
143
137
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
144
138
expect ( onFocus ) . toHaveBeenCalledWith (
145
139
expect . objectContaining ( { pointerType : 'mouse' } ) ,
146
140
) ;
147
141
} ) ;
148
142
149
143
it ( 'is called with the correct pointerType: touch' , ( ) => {
150
- const target = ref . current ;
151
- dispatchPointerDown ( target , { pointerType : 'touch' } ) ;
152
- dispatchPointerUp ( target , { pointerType : 'touch' } ) ;
144
+ const target = createEventTarget ( ref . current ) ;
145
+ const pointerType = 'touch' ;
146
+ target . pointerdown ( { pointerType} ) ;
147
+ target . pointerup ( { pointerType} ) ;
153
148
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
154
149
expect ( onFocus ) . toHaveBeenCalledWith (
155
150
expect . objectContaining ( { pointerType : 'touch' } ) ,
@@ -158,9 +153,10 @@ describe.each(table)('Focus responder', hasPointerEvents => {
158
153
159
154
if ( hasPointerEvents ) {
160
155
it ( 'is called with the correct pointerType: pen' , ( ) => {
161
- const target = ref . current ;
162
- dispatchPointerDown ( target , { pointerType : 'pen' } ) ;
163
- dispatchPointerUp ( target , { pointerType : 'pen' } ) ;
156
+ const target = createEventTarget ( ref . current ) ;
157
+ const pointerType = 'pen' ;
158
+ target . pointerdown ( { pointerType} ) ;
159
+ target . pointerup ( { pointerType} ) ;
164
160
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
165
161
expect ( onFocus ) . toHaveBeenCalledWith (
166
162
expect . objectContaining ( { pointerType : 'pen' } ) ,
@@ -169,10 +165,9 @@ describe.each(table)('Focus responder', hasPointerEvents => {
169
165
}
170
166
171
167
it ( 'is called with the correct pointerType using a keyboard' , ( ) => {
172
- const target = ref . current ;
173
- // Keyboard tab
174
- target . dispatchEvent ( keydown ( { key : 'Tab' } ) ) ;
175
- target . dispatchEvent ( focus ( ) ) ;
168
+ const target = createEventTarget ( ref . current ) ;
169
+ target . keydown ( { key : 'Tab' } ) ;
170
+ target . focus ( ) ;
176
171
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
177
172
expect ( onFocus ) . toHaveBeenCalledWith (
178
173
expect . objectContaining ( { pointerType : 'keyboard' } ) ,
@@ -184,10 +179,11 @@ describe.each(table)('Focus responder', hasPointerEvents => {
184
179
jest . resetModules ( ) ;
185
180
initializeModules ( ) ;
186
181
componentInit ( ) ;
187
- const target = ref . current ;
188
182
189
- target . dispatchEvent ( keydown ( { key : 'Tab' , altKey : true } ) ) ;
190
- target . dispatchEvent ( focus ( ) ) ;
183
+ const target = createEventTarget ( ref . current ) ;
184
+ target . keydown ( { key : 'Tab' , altKey : true } ) ;
185
+ target . focus ( ) ;
186
+
191
187
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
192
188
expect ( onFocus ) . toHaveBeenCalledWith (
193
189
expect . objectContaining ( {
@@ -220,20 +216,20 @@ describe.each(table)('Focus responder', hasPointerEvents => {
220
216
} ) ;
221
217
222
218
it ( 'is called after "blur" and "focus" events' , ( ) => {
223
- const target = ref . current ;
224
- target . dispatchEvent ( focus ( ) ) ;
219
+ const target = createEventTarget ( ref . current ) ;
220
+ target . focus ( ) ;
225
221
expect ( onFocusChange ) . toHaveBeenCalledTimes ( 1 ) ;
226
222
expect ( onFocusChange ) . toHaveBeenCalledWith ( true ) ;
227
- target . dispatchEvent ( blur ( ) ) ;
223
+ target . blur ( ) ;
228
224
expect ( onFocusChange ) . toHaveBeenCalledTimes ( 2 ) ;
229
225
expect ( onFocusChange ) . toHaveBeenCalledWith ( false ) ;
230
226
} ) ;
231
227
232
228
it ( 'is not called after "blur" and "focus" events on descendants' , ( ) => {
233
- const target = innerRef . current ;
234
- target . dispatchEvent ( focus ( ) ) ;
229
+ const target = createEventTarget ( innerRef . current ) ;
230
+ target . focus ( ) ;
235
231
expect ( onFocusChange ) . toHaveBeenCalledTimes ( 0 ) ;
236
- target . dispatchEvent ( blur ( ) ) ;
232
+ target . blur ( ) ;
237
233
expect ( onFocusChange ) . toHaveBeenCalledTimes ( 0 ) ;
238
234
} ) ;
239
235
} ) ;
@@ -259,48 +255,52 @@ describe.each(table)('Focus responder', hasPointerEvents => {
259
255
} ) ;
260
256
261
257
it ( 'is called after "focus" and "blur" if keyboard navigation is active' , ( ) => {
262
- const target = ref . current ;
258
+ const target = createEventTarget ( ref . current ) ;
259
+ const containerTarget = createEventTarget ( container ) ;
263
260
// use keyboard first
264
- container . dispatchEvent ( keydown ( { key : 'Tab' } ) ) ;
265
- target . dispatchEvent ( focus ( ) ) ;
261
+ containerTarget . keydown ( { key : 'Tab' } ) ;
262
+ target . focus ( ) ;
266
263
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 1 ) ;
267
264
expect ( onFocusVisibleChange ) . toHaveBeenCalledWith ( true ) ;
268
- target . dispatchEvent ( blur ( { relatedTarget : container } ) ) ;
265
+ target . blur ( { relatedTarget : container } ) ;
269
266
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 2 ) ;
270
267
expect ( onFocusVisibleChange ) . toHaveBeenCalledWith ( false ) ;
271
268
} ) ;
272
269
273
270
it ( 'is called if non-keyboard event is dispatched on target previously focused with keyboard' , ( ) => {
274
- const target = ref . current ;
271
+ const target = createEventTarget ( ref . current ) ;
272
+ const containerTarget = createEventTarget ( container ) ;
275
273
// use keyboard first
276
- container . dispatchEvent ( keydown ( { key : 'Tab' } ) ) ;
277
- target . dispatchEvent ( focus ( ) ) ;
274
+ containerTarget . keydown ( { key : 'Tab' } ) ;
275
+ target . focus ( ) ;
278
276
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 1 ) ;
279
277
expect ( onFocusVisibleChange ) . toHaveBeenCalledWith ( true ) ;
280
278
// then use pointer on the target, focus should no longer be visible
281
- dispatchPointerDown ( target ) ;
279
+ target . pointerdown ( ) ;
282
280
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 2 ) ;
283
281
expect ( onFocusVisibleChange ) . toHaveBeenCalledWith ( false ) ;
284
282
// onFocusVisibleChange should not be called again
285
- target . dispatchEvent ( blur ( { relatedTarget : container } ) ) ;
283
+ target . blur ( { relatedTarget : container } ) ;
286
284
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 2 ) ;
287
285
} ) ;
288
286
289
287
it ( 'is not called after "focus" and "blur" events without keyboard' , ( ) => {
290
- const target = ref . current ;
291
- dispatchPointerDown ( target ) ;
292
- dispatchPointerUp ( target ) ;
293
- dispatchPointerDown ( container ) ;
294
- target . dispatchEvent ( blur ( { relatedTarget : container } ) ) ;
288
+ const target = createEventTarget ( ref . current ) ;
289
+ const containerTarget = createEventTarget ( container ) ;
290
+ target . pointerdown ( ) ;
291
+ target . pointerup ( ) ;
292
+ containerTarget . pointerdown ( ) ;
293
+ target . blur ( { relatedTarget : container } ) ;
295
294
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 0 ) ;
296
295
} ) ;
297
296
298
297
it ( 'is not called after "blur" and "focus" events on descendants' , ( ) => {
299
- const target = innerRef . current ;
300
- container . dispatchEvent ( keydown ( { key : 'Tab' } ) ) ;
301
- target . dispatchEvent ( focus ( ) ) ;
298
+ const innerTarget = createEventTarget ( innerRef . current ) ;
299
+ const containerTarget = createEventTarget ( container ) ;
300
+ containerTarget . keydown ( { key : 'Tab' } ) ;
301
+ innerTarget . focus ( ) ;
302
302
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 0 ) ;
303
- target . dispatchEvent ( blur ( { relatedTarget : container } ) ) ;
303
+ innerTarget . blur ( { relatedTarget : container } ) ;
304
304
expect ( onFocusVisibleChange ) . toHaveBeenCalledTimes ( 0 ) ;
305
305
} ) ;
306
306
} ) ;
@@ -338,10 +338,13 @@ describe.each(table)('Focus responder', hasPointerEvents => {
338
338
339
339
ReactDOM . render ( < Outer /> , container ) ;
340
340
341
- outerRef . current . dispatchEvent ( focus ( ) ) ;
342
- outerRef . current . dispatchEvent ( blur ( ) ) ;
343
- innerRef . current . dispatchEvent ( focus ( ) ) ;
344
- innerRef . current . dispatchEvent ( blur ( ) ) ;
341
+ const innerTarget = createEventTarget ( innerRef . current ) ;
342
+ const outerTarget = createEventTarget ( outerRef . current ) ;
343
+
344
+ outerTarget . focus ( ) ;
345
+ outerTarget . blur ( ) ;
346
+ innerTarget . focus ( ) ;
347
+ innerTarget . blur ( ) ;
345
348
expect ( events ) . toEqual ( [
346
349
'outer: onFocus' ,
347
350
'outer: onFocusChange' ,
0 commit comments