|
| 1 | +import React from 'react'; |
| 2 | +import { Pressable, Text } from 'react-native'; |
| 3 | +import { render, screen } from '../../../pure'; |
| 4 | +import { userEvent } from '../..'; |
| 5 | +import * as WarnAboutRealTimers from '../utils/warnAboutRealTimers'; |
| 6 | + |
| 7 | +describe('userEvent.longPress with real timers', () => { |
| 8 | + beforeEach(() => { |
| 9 | + jest.useRealTimers(); |
| 10 | + jest.restoreAllMocks(); |
| 11 | + jest.spyOn(WarnAboutRealTimers, 'warnAboutRealTimers').mockImplementation(); |
| 12 | + }); |
| 13 | + |
| 14 | + test('calls onLongPress if the delayLongPress is the default one', async () => { |
| 15 | + const mockOnLongPress = jest.fn(); |
| 16 | + const user = userEvent.setup(); |
| 17 | + |
| 18 | + render( |
| 19 | + <Pressable onLongPress={mockOnLongPress}> |
| 20 | + <Text>press me</Text> |
| 21 | + </Pressable> |
| 22 | + ); |
| 23 | + await user.longPress(screen.getByText('press me')); |
| 24 | + |
| 25 | + expect(mockOnLongPress).toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('calls onLongPress when duration is greater than specified delayLongPress', async () => { |
| 29 | + const mockOnLongPress = jest.fn(); |
| 30 | + const mockOnPress = jest.fn(); |
| 31 | + const user = userEvent.setup(); |
| 32 | + |
| 33 | + render( |
| 34 | + <Pressable |
| 35 | + delayLongPress={800} |
| 36 | + onLongPress={mockOnLongPress} |
| 37 | + onPress={mockOnPress} |
| 38 | + > |
| 39 | + <Text>press me</Text> |
| 40 | + </Pressable> |
| 41 | + ); |
| 42 | + |
| 43 | + await user.longPress(screen.getByText('press me'), { |
| 44 | + duration: 1000, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(mockOnLongPress).toHaveBeenCalled(); |
| 48 | + expect(mockOnPress).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('does not calls onLongPress when duration is lesser than specified delayLongPress', async () => { |
| 52 | + const mockOnLongPress = jest.fn(); |
| 53 | + const mockOnPress = jest.fn(); |
| 54 | + const user = userEvent.setup(); |
| 55 | + |
| 56 | + render( |
| 57 | + <Pressable |
| 58 | + delayLongPress={1000} |
| 59 | + onLongPress={mockOnLongPress} |
| 60 | + onPress={mockOnPress} |
| 61 | + > |
| 62 | + <Text>press me</Text> |
| 63 | + </Pressable> |
| 64 | + ); |
| 65 | + await user.longPress(screen.getByText('press me')); |
| 66 | + |
| 67 | + expect(mockOnLongPress).not.toHaveBeenCalled(); |
| 68 | + expect(mockOnPress).toHaveBeenCalledTimes(1); |
| 69 | + }); |
| 70 | + |
| 71 | + test('does not calls onPress when onLongPress is called', async () => { |
| 72 | + const mockOnLongPress = jest.fn(); |
| 73 | + const mockOnPress = jest.fn(); |
| 74 | + const user = userEvent.setup(); |
| 75 | + |
| 76 | + render( |
| 77 | + <Pressable onLongPress={mockOnLongPress} onPress={mockOnPress}> |
| 78 | + <Text>press me</Text> |
| 79 | + </Pressable> |
| 80 | + ); |
| 81 | + await user.longPress(screen.getByText('press me')); |
| 82 | + |
| 83 | + expect(mockOnLongPress).toHaveBeenCalled(); |
| 84 | + expect(mockOnPress).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + test('longPress is accessible directly in userEvent', async () => { |
| 88 | + const mockOnLongPress = jest.fn(); |
| 89 | + |
| 90 | + render( |
| 91 | + <Pressable onLongPress={mockOnLongPress}> |
| 92 | + <Text>press me</Text> |
| 93 | + </Pressable> |
| 94 | + ); |
| 95 | + |
| 96 | + await userEvent.longPress(screen.getByText('press me')); |
| 97 | + |
| 98 | + expect(mockOnLongPress).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +test('warns about using real timers with userEvent', async () => { |
| 103 | + jest.restoreAllMocks(); |
| 104 | + const mockConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(); |
| 105 | + |
| 106 | + render(<Pressable testID="pressable" />); |
| 107 | + |
| 108 | + await userEvent.longPress(screen.getByTestId('pressable')); |
| 109 | + |
| 110 | + expect(mockConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(` |
| 111 | + "It is recommended to use userEvent with fake timers |
| 112 | + Some events involve duration so your tests may take a long time to run. |
| 113 | + For instance calling userEvent.longPress with real timers will take 500 ms." |
| 114 | + `); |
| 115 | +}); |
0 commit comments