|
| 1 | +import * as React from 'react'; |
| 2 | +import { Text } from 'react-native'; |
| 3 | +import { createEventLogger } from '../../../test-utils'; |
| 4 | +import { render } from '../../..'; |
| 5 | +import { userEvent } from '../..'; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + jest.resetAllMocks(); |
| 9 | +}); |
| 10 | + |
| 11 | +describe('user.press()', () => { |
| 12 | + it('dispatches required events on Text', async () => { |
| 13 | + const { events, logEvent } = createEventLogger(); |
| 14 | + const user = userEvent.setup(); |
| 15 | + const screen = render( |
| 16 | + <Text |
| 17 | + testID="view" |
| 18 | + onPress={logEvent('press')} |
| 19 | + onPressIn={logEvent('pressIn')} |
| 20 | + onPressOut={logEvent('pressOut')} |
| 21 | + /> |
| 22 | + ); |
| 23 | + |
| 24 | + await user.press(screen.getByTestId('view')); |
| 25 | + |
| 26 | + const eventNames = events.map((event) => event.name); |
| 27 | + expect(eventNames).toEqual(['pressIn', 'press', 'pressOut']); |
| 28 | + expect(events).toMatchSnapshot(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('supports direct access', async () => { |
| 32 | + const { events, logEvent } = createEventLogger(); |
| 33 | + const screen = render( |
| 34 | + <Text |
| 35 | + testID="view" |
| 36 | + onPress={logEvent('press')} |
| 37 | + onPressIn={logEvent('pressIn')} |
| 38 | + onPressOut={logEvent('pressOut')} |
| 39 | + /> |
| 40 | + ); |
| 41 | + |
| 42 | + await userEvent.press(screen.getByTestId('view')); |
| 43 | + |
| 44 | + const eventNames = events.map((event) => event.name); |
| 45 | + expect(eventNames).toEqual(['pressIn', 'press', 'pressOut']); |
| 46 | + }); |
| 47 | + |
| 48 | + it.each(['modern', 'legacy'])('works with fake %s timers', async (type) => { |
| 49 | + jest.useFakeTimers({ legacyFakeTimers: type === 'legacy' }); |
| 50 | + |
| 51 | + const { events, logEvent } = createEventLogger(); |
| 52 | + const user = userEvent.setup(); |
| 53 | + const screen = render( |
| 54 | + <Text |
| 55 | + testID="view" |
| 56 | + onPress={logEvent('press')} |
| 57 | + onPressIn={logEvent('pressIn')} |
| 58 | + onPressOut={logEvent('pressOut')} |
| 59 | + /> |
| 60 | + ); |
| 61 | + |
| 62 | + await user.press(screen.getByTestId('view')); |
| 63 | + |
| 64 | + const eventNames = events.map((event) => event.name); |
| 65 | + expect(eventNames).toEqual(['pressIn', 'press', 'pressOut']); |
| 66 | + }); |
| 67 | +}); |
0 commit comments