react-native/jest/mockComponent.js
Luna Wei f52aa9bf26 Support ES modules in mockComponent
Summary: Changelog: [Internal] Support ES modules in mockComponent

Reviewed By: christophpurrer

Differential Revision: D42270009

fbshipit-source-id: 31a857b3bb0486de3d4f05bf72973325f64dce34
2022-12-28 12:35:03 -08:00

65 lines
1.9 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
module.exports = (moduleName, instanceMethods, isESModule) => {
const RealComponent = isESModule
? jest.requireActual(moduleName).default
: jest.requireActual(moduleName);
const React = require('react');
const SuperClass =
typeof RealComponent === 'function' ? RealComponent : React.Component;
const name =
RealComponent.displayName ||
RealComponent.name ||
(RealComponent.render // handle React.forwardRef
? RealComponent.render.displayName || RealComponent.render.name
: 'Unknown');
const nameWithoutPrefix = name.replace(/^(RCT|RK)/, '');
const Component = class extends SuperClass {
static displayName = 'Component';
render() {
const props = Object.assign({}, RealComponent.defaultProps);
if (this.props) {
Object.keys(this.props).forEach(prop => {
// We can't just assign props on top of defaultProps
// because React treats undefined as special and different from null.
// If a prop is specified but set to undefined it is ignored and the
// default prop is used instead. If it is set to null, then the
// null value overwrites the default value.
if (this.props[prop] !== undefined) {
props[prop] = this.props[prop];
}
});
}
return React.createElement(nameWithoutPrefix, props, this.props.children);
}
};
Component.displayName = nameWithoutPrefix;
Object.keys(RealComponent).forEach(classStatic => {
Component[classStatic] = RealComponent[classStatic];
});
if (instanceMethods != null) {
Object.assign(Component.prototype, instanceMethods);
}
return Component;
};