Commit e386b0c
authored
Add legacy mocks (#4055)
## Description
This PR:
- Adds missing legacy mocks
- Adds info to docs about how to import jest utility functions
- Removes `react-native-worklets` mock from our `jestSetup`
## Test plan
New `mock` test
<details>
<summary>Also tested on this test code in new app:</summary>
```tsx
import React from 'react';
import { Text, View } from 'react-native';
import { render } from '@testing-library/react-native';
import {
// v3 components
RawButton,
BaseButton,
RectButton,
BorderlessButton,
Clickable,
Pressable,
ScrollView,
FlatList,
Switch,
TextInput,
RefreshControl,
GestureHandlerRootView,
// Legacy components
LegacyRawButton,
LegacyBaseButton,
LegacyRectButton,
LegacyBorderlessButton,
LegacyPressable,
LegacyScrollView,
LegacyFlatList,
LegacySwitch,
LegacyTextInput,
LegacyRefreshControl,
} from 'react-native-gesture-handler';
import {
fireGestureHandler,
getByGestureTestId,
} from 'react-native-gesture-handler/jest-utils';
import { State } from 'react-native-gesture-handler';
describe('v3 components render without crashing', () => {
test('RawButton', () => {
expect(() =>
render(
<GestureHandlerRootView>
<RawButton />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('BaseButton', () => {
expect(() =>
render(
<GestureHandlerRootView>
<BaseButton />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('RectButton', () => {
expect(() =>
render(
<GestureHandlerRootView>
<RectButton />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('BorderlessButton', () => {
expect(() =>
render(
<GestureHandlerRootView>
<BorderlessButton />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('Clickable', () => {
expect(() =>
render(
<GestureHandlerRootView>
<Clickable />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('Pressable', () => {
expect(() =>
render(
<GestureHandlerRootView>
<Pressable />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('ScrollView', () => {
expect(() =>
render(
<GestureHandlerRootView>
<ScrollView />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('FlatList', () => {
expect(() =>
render(
<GestureHandlerRootView>
<FlatList data={[]} renderItem={() => null} />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('Switch', () => {
expect(() =>
render(
<GestureHandlerRootView>
<Switch />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('TextInput', () => {
expect(() =>
render(
<GestureHandlerRootView>
<TextInput />
</GestureHandlerRootView>,
),
).not.toThrow();
});
test('RefreshControl', () => {
expect(() =>
render(
<GestureHandlerRootView>
<ScrollView refreshControl={<RefreshControl refreshing={false} />} />
</GestureHandlerRootView>,
),
).not.toThrow();
});
});
describe('Legacy components render without crashing', () => {
test('LegacyRawButton', () => {
expect(() => render(<LegacyRawButton />)).not.toThrow();
});
test('LegacyBaseButton', () => {
expect(() => render(<LegacyBaseButton />)).not.toThrow();
});
test('LegacyRectButton', () => {
expect(() => render(<LegacyRectButton />)).not.toThrow();
});
test('LegacyBorderlessButton', () => {
expect(() => render(<LegacyBorderlessButton />)).not.toThrow();
});
test('LegacyPressable', () => {
expect(() => render(<LegacyPressable />)).not.toThrow();
});
test('LegacyScrollView', () => {
expect(() => render(<LegacyScrollView />)).not.toThrow();
});
test('LegacyFlatList', () => {
expect(() =>
render(<LegacyFlatList data={[]} renderItem={() => null} />),
).not.toThrow();
});
test('LegacySwitch', () => {
expect(() => render(<LegacySwitch />)).not.toThrow();
});
test('LegacyTextInput', () => {
expect(() => render(<LegacyTextInput />)).not.toThrow();
});
test('LegacyRefreshControl', () => {
expect(() =>
render(<LegacyRefreshControl refreshing={false} />),
).not.toThrow();
});
});
describe('fireGestureHandler works with v3 components', () => {
test('Clickable onPress fires via gesture handler', () => {
const onPress = jest.fn();
render(
<GestureHandlerRootView>
<Clickable testID="clickable" onPress={onPress}>
<Text>Press Me</Text>
</Clickable>
</GestureHandlerRootView>,
);
const gesture = getByGestureTestId('clickable');
fireGestureHandler(gesture, [
{ oldState: State.UNDETERMINED, state: State.BEGAN },
{ oldState: State.BEGAN, state: State.ACTIVE },
{ oldState: State.ACTIVE, state: State.END },
]);
expect(onPress).toHaveBeenCalledTimes(1);
});
test('RectButton onPress fires via gesture handler', () => {
const onPress = jest.fn();
render(
<GestureHandlerRootView>
<RectButton testID="rect" onPress={onPress} />
</GestureHandlerRootView>,
);
const gesture = getByGestureTestId('rect');
fireGestureHandler(gesture, [
{ oldState: State.UNDETERMINED, state: State.BEGAN },
{ oldState: State.BEGAN, state: State.ACTIVE },
{ oldState: State.ACTIVE, state: State.END },
]);
expect(onPress).toHaveBeenCalledTimes(1);
});
});
```
</details>1 parent d1096d5 commit e386b0c
7 files changed
Lines changed: 171 additions & 30 deletions
File tree
- packages
- docs-gesture-handler/docs/guides
- react-native-gesture-handler
- src
- __tests__
- mocks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| 85 | + | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
| |||
27 | 33 | | |
28 | 34 | | |
29 | 35 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
| |||
Lines changed: 136 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
Lines changed: 8 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | | - | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Lines changed: 7 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
11 | 8 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
0 commit comments