Skip to content

Commit

Permalink
fix(jest/setup): fix circular dependencies in mockModal (#32964)
Browse files Browse the repository at this point in the history
Summary:
Fixes #32939
It appears there is circular dependencies on the Modal component that causes the modalMock function to be an empty object. Removing the import fixes the issue.
I don't know yet why this is not happening when executing the test suite inside `Modal-test.js` but I will investigate this later.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[General] [Fixed] - Fix error "mockModal is not a function"

Pull Request resolved: #32964

Test Plan:
On a newly initiated project using react-native 0.67.1 I created a ModalComponent:
```
import React from 'react';
import {Modal, Text} from 'react-native';

export const ModalComponent = () => {
  return (
    <Modal visible>
      <Text>Test</Text>
    </Modal>
  );
};
```
and a ModalComponent.test.tsx:
```
import 'react-native';
import React from 'react';
import {ModalComponent} from '../ModalComponent';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<ModalComponent />);
});
```

Running the test throws the error "TypeError: mockModal is not a function".

After modifying the mockModal inside node_modules/react-native/jest/mockModal.js it works correctly.

Reviewed By: christophpurrer

Differential Revision: D33771136

Pulled By: lunaleaps

fbshipit-source-id: c09ada8d2f864f5568b3379616a6cace9fb9921e
  • Loading branch information
AntoineDoubovetzky authored and facebook-github-bot committed Jan 27, 2022
1 parent 8858c21 commit 507b05f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions jest/mockModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
'use strict';

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

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

0 comments on commit 507b05f

Please sign in to comment.