@@ -200,50 +200,80 @@ describe('components/Pressable', () => {
200
200
expect ( onContextMenu ) . toBeCalled ( ) ;
201
201
} ) ;
202
202
203
- test ( 'press interaction (keyboard)' , ( ) => {
204
- let container ;
205
- const onPress = jest . fn ( ) ;
206
- const onPressIn = jest . fn ( ) ;
207
- const onPressOut = jest . fn ( ) ;
208
- const ref = React . createRef ( ) ;
203
+ describe ( 'press interaction (keyboard)' , ( ) => {
204
+ test ( 'trigger press when keyup is on the same element' , ( ) => {
205
+ let container ;
206
+ const onPress = jest . fn ( ) ;
207
+ const onPressIn = jest . fn ( ) ;
208
+ const onPressOut = jest . fn ( ) ;
209
+ const ref = React . createRef ( ) ;
209
210
210
- function TestCase ( ) {
211
- const [ shown , setShown ] = React . useState ( true ) ;
212
- return shown ? (
213
- < Pressable
214
- children = { ( { pressed } ) =>
215
- pressed ? < div data-testid = "press-content" /> : null
216
- }
217
- onPress = { ( e ) => {
218
- onPress ( e ) ;
219
- setShown ( false ) ;
220
- } }
221
- onPressIn = { onPressIn }
222
- onPressOut = { onPressOut }
223
- ref = { ref }
224
- style = { ( { pressed } ) => [ pressed && { outline : 'press-ring' } ] }
225
- />
226
- ) : null ;
227
- }
211
+ function TestCase ( ) {
212
+ const [ shown , setShown ] = React . useState ( true ) ;
213
+ return shown ? (
214
+ < Pressable
215
+ children = { ( { pressed } ) =>
216
+ pressed ? < div data-testid = "press-content" /> : null
217
+ }
218
+ onPress = { ( e ) => {
219
+ onPress ( e ) ;
220
+ setShown ( false ) ;
221
+ } }
222
+ onPressIn = { onPressIn }
223
+ onPressOut = { onPressOut }
224
+ ref = { ref }
225
+ style = { ( { pressed } ) => [ pressed && { outline : 'press-ring' } ] }
226
+ />
227
+ ) : null ;
228
+ }
228
229
229
- act ( ( ) => {
230
- ( { container } = render ( < TestCase /> ) ) ;
231
- } ) ;
232
- const target = createEventTarget ( ref . current ) ;
233
- expect ( container . firstChild ) . toMatchSnapshot ( ) ;
234
- act ( ( ) => {
235
- target . keydown ( { key : 'Enter' } ) ;
236
- jest . runAllTimers ( ) ;
230
+ act ( ( ) => {
231
+ ( { container } = render ( < TestCase /> ) ) ;
232
+ } ) ;
233
+ const target = createEventTarget ( ref . current ) ;
234
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
235
+ act ( ( ) => {
236
+ target . keydown ( { key : 'Enter' } ) ;
237
+ jest . runAllTimers ( ) ;
238
+ } ) ;
239
+ expect ( onPressIn ) . toBeCalled ( ) ;
240
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
241
+ act ( ( ) => {
242
+ target . keyup ( { key : 'Enter' } ) ;
243
+ jest . runAllTimers ( ) ;
244
+ } ) ;
245
+ expect ( onPressOut ) . toBeCalled ( ) ;
246
+ expect ( onPress ) . toBeCalled ( ) ;
247
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
237
248
} ) ;
238
- expect ( onPressIn ) . toBeCalled ( ) ;
239
- expect ( container . firstChild ) . toMatchSnapshot ( ) ;
240
- act ( ( ) => {
241
- target . keyup ( { key : 'Enter' } ) ;
242
- jest . runAllTimers ( ) ;
249
+
250
+ test ( 'ignore press when keyup is on a different element' , ( ) => {
251
+ const onPress = jest . fn ( ) ;
252
+ const firstRef = React . createRef ( ) ;
253
+
254
+ function TestCase ( ) {
255
+ return (
256
+ < Pressable
257
+ onPress = { ( e ) => {
258
+ onPress ( e ) ;
259
+ } }
260
+ ref = { firstRef }
261
+ />
262
+ ) ;
263
+ }
264
+
265
+ act ( ( ) => {
266
+ render ( < TestCase /> ) ;
267
+ } ) ;
268
+ const target = createEventTarget ( firstRef . current ) ;
269
+ const body = createEventTarget ( document . body ) ;
270
+ act ( ( ) => {
271
+ target . keydown ( { key : 'Enter' } ) ;
272
+ body . keyup ( { key : 'Enter' } ) ;
273
+ jest . runAllTimers ( ) ;
274
+ } ) ;
275
+ expect ( onPress ) . not . toBeCalled ( ) ;
243
276
} ) ;
244
- expect ( onPressOut ) . toBeCalled ( ) ;
245
- expect ( onPress ) . toBeCalled ( ) ;
246
- expect ( container . firstChild ) . toMatchSnapshot ( ) ;
247
277
} ) ;
248
278
249
279
test ( 'press interaction as button (keyboard)' , ( ) => {
0 commit comments