Skip to content

Commit 468a136

Browse files
mdjastrzebskifacebook-github-bot
authored andcommitted
fix: do not render mocked Modal when visible=false (#39157)
Summary: Note: this PR is related only to testing mocks provided by RN, and does not affect runtime code. When building new Jest matchers for React Native Testing Library (callstack/react-native-testing-library#1468) I've noticed that when rendering React Native in Jest using React Test Renderer the mocked `Modal` component renders host `Modal` element when `visible={false}`. This seems to be incorrect as only visible modal should render any host elements. Not visible one should not render any host element even empty ones. Current mock implementation also contradicts the behaviour of non-mocked `Modal` which does not render `RCTModalHostView` in such case. ## Changelog: [General] [Fixed] - Do not render mocked <Modal /> when `visible=false` Pull Request resolved: #39157 Test Plan: I've added test showing that non-mocked Modal renders to `null` and modifies the existing tests so that mocked Modal also renders to `null.` Luna: I've updated relevant snapshots Reviewed By: NickGerleman Differential Revision: D48728277 Pulled By: lunaleaps fbshipit-source-id: cf06495ad959e2d9549241b57f46f75d7beb9eae
1 parent 442b852 commit 468a136

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

packages/react-native/Libraries/Modal/__tests__/Modal-test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ describe('<Modal />', () => {
2626
expect(instance).toMatchSnapshot();
2727
});
2828

29-
it('should not render its children when mocked with visible=false', () => {
29+
it('should not render <Modal> when mocked with visible=false', () => {
3030
const instance = render.create(
3131
<Modal visible={false}>
3232
<View testID="child" />
3333
</Modal>,
3434
);
35-
expect(instance.root.findAllByProps({testID: 'child'})).toHaveLength(0);
35+
expect(instance.toJSON()).toBeNull();
3636
});
3737

3838
it('should shallow render as <Modal> when mocked', () => {
@@ -65,4 +65,15 @@ describe('<Modal />', () => {
6565
);
6666
expect(instance).toMatchSnapshot();
6767
});
68+
69+
it('should not render <RCTModalHostView> when not mocked with visible=false', () => {
70+
jest.dontMock('../Modal');
71+
72+
const instance = render.create(
73+
<Modal visible={false}>
74+
<View />
75+
</Modal>,
76+
);
77+
expect(instance.toJSON()).toBeNull();
78+
});
6879
});

packages/react-native/Libraries/Modal/__tests__/__snapshots__/Modal-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports[`<Modal /> should render as <RCTModalHostView> when not mocked 1`] = `
1313
<RCTModalHostView
1414
animationType="none"
1515
hardwareAccelerated={false}
16-
identifier={4}
16+
identifier={3}
1717
onDismiss={[Function]}
1818
onStartShouldSetResponder={[Function]}
1919
presentationStyle="fullScreen"

packages/react-native/jest/mockModal.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import typeof Modal from '../Libraries/Modal/Modal';
1717

1818
function mockModal(BaseComponent: $FlowFixMe) {
1919
class ModalMock extends BaseComponent {
20-
render(): React.Element<Modal> {
20+
render(): React.Element<Modal> | null {
21+
if (this.props.visible === false) {
22+
return null;
23+
}
24+
2125
return (
22-
<BaseComponent {...this.props}>
23-
{this.props.visible !== true ? null : this.props.children}
24-
</BaseComponent>
26+
<BaseComponent {...this.props}>{this.props.children}</BaseComponent>
2527
);
2628
}
2729
}

0 commit comments

Comments
 (0)