|
| 1 | +import 'react-native-gesture-handler/jestSetup.js'; |
| 2 | +import React from 'react'; |
| 3 | +import { View } from 'react-native'; |
| 4 | +import { Pressable } from 'react-native-gesture-handler'; |
| 5 | + |
| 6 | +import { fireEvent, render, screen, userEvent } from '..'; |
| 7 | +import { createEventLogger, getEventsNames } from '../test-utils'; |
| 8 | + |
| 9 | +test('fireEvent can invoke press events for RNGH Pressable', () => { |
| 10 | + const onPress = jest.fn(); |
| 11 | + const onPressIn = jest.fn(); |
| 12 | + const onPressOut = jest.fn(); |
| 13 | + const onLongPress = jest.fn(); |
| 14 | + |
| 15 | + render( |
| 16 | + <View> |
| 17 | + <Pressable |
| 18 | + testID="pressable" |
| 19 | + onPress={onPress} |
| 20 | + onPressIn={onPressIn} |
| 21 | + onPressOut={onPressOut} |
| 22 | + onLongPress={onLongPress} |
| 23 | + /> |
| 24 | + </View>, |
| 25 | + ); |
| 26 | + |
| 27 | + const pressable = screen.getByTestId('pressable'); |
| 28 | + |
| 29 | + fireEvent.press(pressable); |
| 30 | + expect(onPress).toHaveBeenCalled(); |
| 31 | + |
| 32 | + fireEvent(pressable, 'pressIn'); |
| 33 | + expect(onPressIn).toHaveBeenCalled(); |
| 34 | + |
| 35 | + fireEvent(pressable, 'pressOut'); |
| 36 | + expect(onPressOut).toHaveBeenCalled(); |
| 37 | + |
| 38 | + fireEvent(pressable, 'longPress'); |
| 39 | + expect(onLongPress).toHaveBeenCalled(); |
| 40 | +}); |
| 41 | + |
| 42 | +test('userEvent can invoke press events for RNGH Pressable', async () => { |
| 43 | + const { events, logEvent } = createEventLogger(); |
| 44 | + const user = userEvent.setup(); |
| 45 | + |
| 46 | + render( |
| 47 | + <View> |
| 48 | + <Pressable |
| 49 | + testID="pressable" |
| 50 | + onPress={logEvent('press')} |
| 51 | + onPressIn={logEvent('pressIn')} |
| 52 | + onPressOut={logEvent('pressOut')} |
| 53 | + onLongPress={logEvent('longPress')} |
| 54 | + /> |
| 55 | + </View>, |
| 56 | + ); |
| 57 | + |
| 58 | + const pressable = screen.getByTestId('pressable'); |
| 59 | + await user.press(pressable); |
| 60 | + expect(getEventsNames(events)).toEqual(['pressIn', 'pressOut', 'press']); |
| 61 | +}); |
0 commit comments