Skip to content

Commit ec614c1

Browse files
AntoineDoubovetzkyfacebook-github-bot
AntoineDoubovetzky
authored andcommitted
Update Modal's mock to not render its children when it is not visible (#32346)
Summary: The Modal's mock always render its children (whether it is visible or not), whereas in reality the Modal renders `null` when the Modal is not visible. This causes troubles when trying to test whether the Modal is visible or not. Instead of testing if the children are rendered (using getByText from React Native Testing Library for instance), we are forced to test the value of the visible prop directly (see callstack/react-native-testing-library#508 and callstack/react-native-testing-library#659). This is not ideal because we are forced to test implementation detail and can't test from the user perspective. I also believe the mock should be closest as possible from reality. I had 2 options: 1. Rendering the Modal without its children 2. Not rendering the Modal at all The latter has the advantage of being closer to the reality, but I chose the former to still be able to test the Modal through the visible prop, so there is no breaking change (only snapshots update will be required). ## Changelog [General] [Changed] - Update Modal's mock to not render its children when it is not visible Pull Request resolved: #32346 Test Plan: I added a test case when visible is false, then updated the mock so the children are not rendered. The before / after is here: ![image](https://user-images.githubusercontent.com/17070498/136256142-a351d002-8b77-490a-ba65-1e8ad0d6eb55.png) Reviewed By: yungsters Differential Revision: D31445964 Pulled By: lunaleaps fbshipit-source-id: 08501921455728cde6befd0103016c95074cc1df
1 parent 27304fc commit ec614c1

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Libraries/Modal/__tests__/Modal-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ describe('<Modal />', () => {
2727
expect(instance).toMatchSnapshot();
2828
});
2929

30+
it('should not render its children when mocked with visible=false', () => {
31+
const instance = render.create(
32+
<Modal visible={false}>
33+
<View testID="child" />
34+
</Modal>,
35+
);
36+
expect(instance.root.findAllByProps({testID: 'child'})).toHaveLength(0);
37+
});
38+
3039
it('should shallow render as <Modal> when mocked', () => {
3140
const output = render.shallow(
3241
<Modal>

Libraries/Modal/__tests__/__snapshots__/Modal-test.js.snap

+1-1
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={1}
16+
identifier={4}
1717
onDismiss={[Function]}
1818
onStartShouldSetResponder={[Function]}
1919
presentationStyle="fullScreen"

jest/mockModal.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict-local
9+
*/
10+
11+
/* eslint-env jest */
12+
13+
'use strict';
14+
15+
const React = require('react');
16+
const Modal = require('../Libraries/Modal/Modal');
17+
18+
function mockModal(BaseComponent: $FlowFixMe) {
19+
class ModalMock extends BaseComponent {
20+
render(): React.Element<typeof Modal> {
21+
return (
22+
<BaseComponent {...this.props}>
23+
{this.props.visible !== true ? null : this.props.children}
24+
</BaseComponent>
25+
);
26+
}
27+
}
28+
return ModalMock;
29+
}
30+
31+
module.exports = (mockModal: $FlowFixMe);

jest/setup.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ jest
110110
getNativeRef: jest.fn(),
111111
}),
112112
)
113-
.mock('../Libraries/Modal/Modal', () =>
114-
mockComponent('../Libraries/Modal/Modal'),
115-
)
113+
.mock('../Libraries/Modal/Modal', () => {
114+
const baseComponent = mockComponent('../Libraries/Modal/Modal');
115+
const mockModal = jest.requireActual('./mockModal');
116+
return mockModal(baseComponent);
117+
})
116118
.mock('../Libraries/Components/View/View', () =>
117119
mockComponent('../Libraries/Components/View/View', MockNativeMethods),
118120
)

0 commit comments

Comments
 (0)