Skip to content

Commit fda19ef

Browse files
akwasniewskij-piasecki
authored andcommitted
[jest] Fixing enabled prop in buttons (#3612)
## Description Passing `enabled={false}` to buttons and pressable in jest tests now has effect. I properly configured the corresponding mocks. Change was motivated by issue #2385. ## Test plan Tested using React Native 80.1 ```ts import { render, userEvent } from '@testing-library/react-native'; import { RectButton } from 'react-native-gesture-handler'; export const user = userEvent.setup() describe('Testing disabled Button', () => { it('onPress does not trigger', async () => { const onPress = jest.fn(); const { getByTestId } = render( <RectButton testID="btn" onPress={onPress} enabled={false} /> ); const btn = getByTestId('btn'); expect(onPress).not.toHaveBeenCalled(); await user.press(btn); expect(onPress).not.toHaveBeenCalled(); }); }); ```
1 parent 8f13556 commit fda19ef

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1-
jest.mock('./src/RNGestureHandlerModule', () => require('./src/mocks'));
1+
jest.mock('./src/RNGestureHandlerModule', () => require('./src/mocks/mocks'));
2+
jest.mock('./src/components/GestureButtons', () => require('./src/mocks/mocks'));
3+
jest.mock('./src/components/Pressable/Pressable', () => require('./src/mocks/Pressable'));
4+
5+
26
jest.mock('./lib/commonjs/RNGestureHandlerModule', () =>
3-
require('./lib/commonjs/mocks')
7+
require('./lib/commonjs/mocks/mocks')
48
);
9+
jest.mock('./lib/commonjs/components/GestureButtons', () =>
10+
require('./lib/commonjs/mocks/mocks')
11+
);
12+
jest.mock('./lib/commonjs/components/Pressable', () =>
13+
require('./lib/commonjs/mocks/Pressable')
14+
);
15+
16+
517
jest.mock('./lib/module/RNGestureHandlerModule', () =>
6-
require('./lib/module/mocks')
18+
require('./lib/module/mocks/mocks')
19+
);
20+
jest.mock('./lib/module/components/GestureButtons', () =>
21+
require('./lib/module/mocks/mocks')
22+
);
23+
jest.mock('./lib/module/components/Pressable', () =>
24+
require('./lib/module/mocks/Pressable')
725
);
26+

packages/react-native-gesture-handler/src/__mocks__/RNGestureHandlerModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Mocks from '../mocks';
1+
import Mocks from '../mocks/mocks';
22

33
export default {
44
...Mocks,

packages/react-native-gesture-handler/src/components/GestureButtonsProps.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,28 @@ export interface RawButtonProps
6464

6565
/**
6666
* Used for testing-library compatibility, not passed to the native component.
67+
* @deprecated test-only props are deprecated and will be removed in the future.
6768
*/
6869
// eslint-disable-next-line @typescript-eslint/ban-types
6970
testOnly_onPress?: Function | null;
7071

7172
/**
7273
* Used for testing-library compatibility, not passed to the native component.
74+
* @deprecated test-only props are deprecated and will be removed in the future.
7375
*/
7476
// eslint-disable-next-line @typescript-eslint/ban-types
7577
testOnly_onPressIn?: Function | null;
7678

7779
/**
7880
* Used for testing-library compatibility, not passed to the native component.
81+
* @deprecated test-only props are deprecated and will be removed in the future.
7982
*/
8083
// eslint-disable-next-line @typescript-eslint/ban-types
8184
testOnly_onPressOut?: Function | null;
8285

8386
/**
8487
* Used for testing-library compatibility, not passed to the native component.
88+
* @deprecated test-only props are deprecated and will be removed in the future.
8589
*/
8690
// eslint-disable-next-line @typescript-eslint/ban-types
8791
testOnly_onLongPress?: Function | null;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Pressable as default } from 'react-native';

packages/react-native-gesture-handler/src/mocks.tsx renamed to packages/react-native-gesture-handler/src/mocks/mocks.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
DrawerLayoutAndroid,
1212
View,
1313
} from 'react-native';
14-
import { State } from './State';
15-
import { Directions } from './Directions';
14+
import { State } from '../State';
15+
import { Directions } from '../Directions';
1616

1717
const NOOP = () => {
1818
// Do nothing
@@ -31,14 +31,14 @@ const LongPressGestureHandler = View;
3131
const PinchGestureHandler = View;
3232
const RotationGestureHandler = View;
3333
const FlingGestureHandler = View;
34-
const RawButton = ({ enabled, ...rest }: any) => (
35-
<TouchableNativeFeedback disabled={!enabled} {...rest}>
34+
export const RawButton = ({ enabled, ...rest }: any) => (
35+
<TouchableNativeFeedback disabled={enabled === false} {...rest}>
3636
<View />
3737
</TouchableNativeFeedback>
3838
);
39-
const BaseButton = RawButton;
40-
const RectButton = RawButton;
41-
const BorderlessButton = TouchableNativeFeedback;
39+
export const BaseButton = RawButton;
40+
export const RectButton = RawButton;
41+
export const BorderlessButton = TouchableNativeFeedback;
4242

4343
export default {
4444
TouchableHighlight,
@@ -57,10 +57,6 @@ export default {
5757
PinchGestureHandler,
5858
RotationGestureHandler,
5959
FlingGestureHandler,
60-
RawButton,
61-
BaseButton,
62-
RectButton,
63-
BorderlessButton,
6460
PanGestureHandler,
6561
attachGestureHandler,
6662
createGestureHandler,

0 commit comments

Comments
 (0)