|
| 1 | +import * as React from 'react'; |
| 2 | +import { Pressable, Text, TouchableOpacity, View } from 'react-native'; |
| 3 | + |
| 4 | +import { render, screen } from '../../..'; |
| 5 | +import type { SyntheticEvent } from '../event'; |
| 6 | +import { createEvent } from '../event'; |
| 7 | +import type { PressabilityCallbackName } from '../pressability'; |
| 8 | +import { dispatchPressabilityEvent } from '../pressability'; |
| 9 | + |
| 10 | +const callbackNames: PressabilityCallbackName[] = [ |
| 11 | + 'onPress', |
| 12 | + 'onPressIn', |
| 13 | + 'onPressOut', |
| 14 | + 'onLongPress', |
| 15 | +]; |
| 16 | + |
| 17 | +function createTouchEvent() { |
| 18 | + return createEvent('touchEnd', { nativeEvent: { pageX: 10 } }) as SyntheticEvent; |
| 19 | +} |
| 20 | + |
| 21 | +function logCallbacks(calls: string[], id: string) { |
| 22 | + const log = (name: PressabilityCallbackName) => () => { |
| 23 | + calls.push(`${id}.${name}`); |
| 24 | + }; |
| 25 | + return { |
| 26 | + onPress: log('onPress'), |
| 27 | + onPressIn: log('onPressIn'), |
| 28 | + onPressOut: log('onPressOut'), |
| 29 | + onLongPress: log('onLongPress'), |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +test.each(callbackNames)('calls only %s of Pressable and TouchableOpacity', async (name) => { |
| 34 | + const calls: string[] = []; |
| 35 | + await render( |
| 36 | + <> |
| 37 | + <Pressable {...logCallbacks(calls, 'pressable')}> |
| 38 | + <Text>Pressable</Text> |
| 39 | + </Pressable> |
| 40 | + <TouchableOpacity {...logCallbacks(calls, 'touchable')}> |
| 41 | + <Text>Touchable</Text> |
| 42 | + </TouchableOpacity> |
| 43 | + </>, |
| 44 | + ); |
| 45 | + |
| 46 | + await dispatchPressabilityEvent(screen.getByText('Pressable'), name, createTouchEvent()); |
| 47 | + await dispatchPressabilityEvent(screen.getByText('Touchable'), name, createTouchEvent()); |
| 48 | + |
| 49 | + expect(calls).toEqual([`pressable.${name}`, `touchable.${name}`]); |
| 50 | +}); |
| 51 | + |
| 52 | +test.each(callbackNames)('calls only %s of host elements, incl. testOnly_ props', async (name) => { |
| 53 | + const calls: string[] = []; |
| 54 | + await render( |
| 55 | + <> |
| 56 | + <Text {...logCallbacks(calls, 'text')}>Text</Text> |
| 57 | + <View |
| 58 | + testID="view" |
| 59 | + {...{ [`testOnly_${name}`]: () => calls.push(`view.testOnly_${name}`) }} |
| 60 | + /> |
| 61 | + </>, |
| 62 | + ); |
| 63 | + |
| 64 | + await dispatchPressabilityEvent(screen.getByText('Text'), name, createTouchEvent()); |
| 65 | + await dispatchPressabilityEvent(screen.getByTestId('view'), name, createTouchEvent()); |
| 66 | + |
| 67 | + expect(calls).toEqual([`text.${name}`, `view.testOnly_${name}`]); |
| 68 | +}); |
| 69 | + |
| 70 | +test('passes the event, with the hit target and the responder', async () => { |
| 71 | + const onPressIn = jest.fn((event) => ({ |
| 72 | + currentTarget: event.currentTarget, |
| 73 | + target: event.target, |
| 74 | + })); |
| 75 | + await render( |
| 76 | + <Pressable testID="pressable" onPressIn={onPressIn}> |
| 77 | + <Text>Press me</Text> |
| 78 | + </Pressable>, |
| 79 | + ); |
| 80 | + const event = createTouchEvent(); |
| 81 | + |
| 82 | + expect(await dispatchPressabilityEvent(screen.getByText('Press me'), 'onPressIn', event)).toBe( |
| 83 | + true, |
| 84 | + ); |
| 85 | + |
| 86 | + expect(onPressIn).toHaveBeenCalledWith(event); |
| 87 | + expect(onPressIn.mock.results[0].value).toEqual({ |
| 88 | + currentTarget: screen.getByTestId('pressable'), |
| 89 | + target: screen.getByText('Press me'), |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +test('host element with another press callback claims the touch, but gets nothing', async () => { |
| 94 | + const onPressIn = jest.fn(); |
| 95 | + await render( |
| 96 | + <Pressable onPressIn={onPressIn}> |
| 97 | + <Text onPress={jest.fn()}>Press me</Text> |
| 98 | + </Pressable>, |
| 99 | + ); |
| 100 | + |
| 101 | + expect( |
| 102 | + await dispatchPressabilityEvent(screen.getByText('Press me'), 'onPressIn', createTouchEvent()), |
| 103 | + ).toBe(false); |
| 104 | + expect(onPressIn).not.toHaveBeenCalled(); |
| 105 | +}); |
| 106 | + |
| 107 | +test('returns false when the responder has no such callback', async () => { |
| 108 | + await render( |
| 109 | + <Pressable onPress={jest.fn()}> |
| 110 | + <Text>Press me</Text> |
| 111 | + </Pressable>, |
| 112 | + ); |
| 113 | + |
| 114 | + expect( |
| 115 | + await dispatchPressabilityEvent( |
| 116 | + screen.getByText('Press me'), |
| 117 | + 'onLongPress', |
| 118 | + createTouchEvent(), |
| 119 | + ), |
| 120 | + ).toBe(false); |
| 121 | +}); |
| 122 | + |
| 123 | +test('does not call callbacks of disabled Pressable', async () => { |
| 124 | + const calls: string[] = []; |
| 125 | + await render( |
| 126 | + <Pressable {...logCallbacks(calls, 'pressable')} disabled> |
| 127 | + <Text>Press me</Text> |
| 128 | + </Pressable>, |
| 129 | + ); |
| 130 | + |
| 131 | + for (const name of callbackNames) { |
| 132 | + await dispatchPressabilityEvent(screen.getByText('Press me'), name, createTouchEvent()); |
| 133 | + } |
| 134 | + |
| 135 | + expect(calls).toEqual([]); |
| 136 | +}); |
0 commit comments