Skip to content

Commit 8907386

Browse files
authored
Incubator.Dialog - fix test after RN71 bug fix (#2575)
* Incubator.Dialog - fix test after RN71 bug fix * Forgot this
1 parent 77c0f6c commit 8907386

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed
File renamed without changes.

jest-setup.js renamed to jestSetup/jest-setup.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ jest.mock('react-native-gesture-handler',
9797
jest.mock('react-native', () => {
9898
const reactNative = jest.requireActual('react-native');
9999
reactNative.NativeModules.KeyboardTrackingViewTempManager = {};
100+
const OriginalModal = reactNative.Modal;
101+
const React = jest.requireActual('react');
102+
const useDidUpdate = require('./useDidUpdate').default;
103+
Object.defineProperty(reactNative, 'Modal', {
104+
value: props => {
105+
// eslint-disable-next-line react-hooks/rules-of-hooks
106+
useDidUpdate(() => {
107+
if (!props.visible) {
108+
props.onDismiss?.();
109+
}
110+
}, [props.visible]);
111+
return <OriginalModal {...props}/>;
112+
}
113+
});
100114
return reactNative;
101115
});
102116

jestSetup/useDidUpdate.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {useEffect, useRef, DependencyList} from 'react';
2+
3+
/**
4+
* This hook avoid calling useEffect on the initial value of his dependency array
5+
*/
6+
const useDidUpdate = (callback: () => void, dep: DependencyList) => {
7+
const isMounted = useRef<boolean>(false);
8+
9+
useEffect(() => {
10+
if (isMounted.current) {
11+
callback();
12+
} else {
13+
isMounted.current = true;
14+
}
15+
}, dep);
16+
};
17+
18+
export default useDidUpdate;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
"/uilib-docs/"
140140
],
141141
"setupFiles": [
142-
"./jest-setup.js"
142+
"./jestSetup/jest-setup.js"
143143
],
144144
"testMatch": [
145145
"**/*.spec.(js|ts|tsx)"

src/incubator/Dialog/__tests__/index.spec.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ describe('Incubator.Dialog', () => {
5454
const closeButtonDriver = new ButtonDriver({component, testID: 'closeButton'});
5555
await closeButtonDriver.press();
5656
expect(await dialogDriver.exists()).toBeFalsy();
57-
// TODO:
58-
// expect(onDismiss).toHaveBeenCalledTimes(1);
57+
expect(onDismiss).toHaveBeenCalledTimes(1);
5958
});
6059
});

0 commit comments

Comments
 (0)