Skip to content

Commit 4b935ae

Browse files
rickhanloniifacebook-github-bot
authored andcommitted
Correctly mock all components by setting the displayName
Summary: In the next react sync we removed the `displayName` from forwardRef and useMemo components ([PR here](facebook/react#18495 )). This means we need to manually add the displayName in the mock. Changelog: [General] [Fixed] Fix test renderer mocks to use the displayName more often. Reviewed By: TheSavior Differential Revision: D22775470 fbshipit-source-id: 1390dc325e34f7ccea32bbdf1c6a8f6efea3a080
1 parent 7af3f6e commit 4b935ae

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Libraries/Modal/__tests__/Modal-test.js

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

30-
it('should shallow render as <Component> when mocked', () => {
30+
it('should shallow render as <Modal> when mocked', () => {
3131
const output = render.shallow(
3232
<Modal>
3333
<View />

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ exports[`<Modal /> should render as <RCTModalHostView> when not mocked 1`] = `
6161
</RCTModalHostView>
6262
`;
6363
64-
exports[`<Modal /> should shallow render as <Component> when mocked 1`] = `
65-
<Component
64+
exports[`<Modal /> should shallow render as <Modal> when mocked 1`] = `
65+
<Modal
6666
hardwareAccelerated={false}
6767
visible={true}
6868
>
6969
<View />
70-
</Component>
70+
</Modal>
7171
`;
7272
7373
exports[`<Modal /> should shallow render as <Modal> when not mocked 1`] = `

jest/mockComponent.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ module.exports = (moduleName, instanceMethods) => {
1616
const SuperClass =
1717
typeof RealComponent === 'function' ? RealComponent : React.Component;
1818

19+
const name =
20+
RealComponent.displayName ||
21+
RealComponent.name ||
22+
(RealComponent.render // handle React.forwardRef
23+
? RealComponent.render.displayName || RealComponent.render.name
24+
: 'Unknown');
25+
26+
const nameWithoutPrefix = name.replace(/^(RCT|RK)/, '');
27+
1928
const Component = class extends SuperClass {
2029
static displayName = 'Component';
2130

2231
render() {
23-
const name =
24-
RealComponent.displayName ||
25-
RealComponent.name ||
26-
(RealComponent.render // handle React.forwardRef
27-
? RealComponent.render.displayName || RealComponent.render.name
28-
: 'Unknown');
29-
3032
const props = Object.assign({}, RealComponent.defaultProps);
3133

3234
if (this.props) {
@@ -42,14 +44,12 @@ module.exports = (moduleName, instanceMethods) => {
4244
});
4345
}
4446

45-
return React.createElement(
46-
name.replace(/^(RCT|RK)/, ''),
47-
props,
48-
this.props.children,
49-
);
47+
return React.createElement(nameWithoutPrefix, props, this.props.children);
5048
}
5149
};
5250

51+
Component.displayName = nameWithoutPrefix;
52+
5353
Object.keys(RealComponent).forEach(classStatic => {
5454
Component[classStatic] = RealComponent[classStatic];
5555
});

0 commit comments

Comments
 (0)