Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Modal's mock to not render its children when it is not visible #32346

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Libraries/Modal/__tests__/Modal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ describe('<Modal />', () => {
expect(instance).toMatchSnapshot();
});

it('should not render its children when mocked with visible=false', () => {
const instance = render.create(
<Modal visible={false}>
<View />
</Modal>,
);
expect(instance).toMatchSnapshot();
});

it('should shallow render as <Modal> when mocked', () => {
const output = render.shallow(
<Modal>
Expand Down
9 changes: 8 additions & 1 deletion Libraries/Modal/__tests__/__snapshots__/Modal-test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<Modal /> should not render its children when mocked with visible=false 1`] = `
<Modal
hardwareAccelerated={false}
visible={false}
/>
`;

exports[`<Modal /> should render as <Modal> when mocked 1`] = `
<Modal
hardwareAccelerated={false}
Expand All @@ -13,7 +20,7 @@ exports[`<Modal /> should render as <RCTModalHostView> when not mocked 1`] = `
<RCTModalHostView
animationType="none"
hardwareAccelerated={false}
identifier={1}
identifier={4}
onDismiss={[Function]}
onStartShouldSetResponder={[Function]}
presentationStyle="fullScreen"
Expand Down
32 changes: 32 additions & 0 deletions jest/mockModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

/* eslint-env jest */

'use strict';

const React = require('react');
const Modal = require('../Libraries/Modal/Modal');

function mockModal(BaseComponent: $FlowFixMe) {
class ModalMock extends BaseComponent {
render(): React.Element<typeof Modal> {
return (
<BaseComponent
{...this.props}
children={this.props.visible !== true ? null : this.props.children}
/>
);
}
}
return ModalMock;
}

module.exports = (mockModal: $FlowFixMe);
8 changes: 5 additions & 3 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ jest
getNativeRef: jest.fn(),
}),
)
.mock('../Libraries/Modal/Modal', () =>
mockComponent('../Libraries/Modal/Modal'),
)
.mock('../Libraries/Modal/Modal', () => {
const baseComponent = mockComponent('../Libraries/Modal/Modal');
const mockModal = jest.requireActual('./mockModal');
return mockModal(baseComponent);
})
.mock('../Libraries/Components/View/View', () =>
mockComponent('../Libraries/Components/View/View', MockNativeMethods),
)
Expand Down