Skip to content

Commit

Permalink
update Modal's mock to not render its children when it is not visible
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineDoubovetzky committed Oct 6, 2021
1 parent 778845a commit cdab5f0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 2 additions & 4 deletions Libraries/Modal/__tests__/__snapshots__/Modal-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ exports[`<Modal /> should not render its children when mocked with visible=false
<Modal
hardwareAccelerated={false}
visible={false}
>
<View />
</Modal>
/>
`;

exports[`<Modal /> should render as <Modal> when mocked 1`] = `
Expand All @@ -22,7 +20,7 @@ exports[`<Modal /> should render as <RCTModalHostView> when not mocked 1`] = `
<RCTModalHostView
animationType="none"
hardwareAccelerated={false}
identifier={2}
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

0 comments on commit cdab5f0

Please sign in to comment.