2016-10-17 15:40:11 +00:00
|
|
|
/**
|
2021-12-30 23:08:43 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-10-17 15:40:11 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-09 07:47:48 +00:00
|
|
|
*
|
|
|
|
* @format
|
2016-10-17 15:40:11 +00:00
|
|
|
*/
|
2018-05-09 07:47:48 +00:00
|
|
|
|
2016-11-21 01:49:21 +00:00
|
|
|
'use strict';
|
2016-10-17 15:40:11 +00:00
|
|
|
|
2022-12-28 20:35:03 +00:00
|
|
|
module.exports = (moduleName, instanceMethods, isESModule) => {
|
|
|
|
const RealComponent = isESModule
|
|
|
|
? jest.requireActual(moduleName).default
|
|
|
|
: jest.requireActual(moduleName);
|
2016-10-17 15:40:11 +00:00
|
|
|
const React = require('react');
|
|
|
|
|
2018-05-09 07:47:48 +00:00
|
|
|
const SuperClass =
|
|
|
|
typeof RealComponent === 'function' ? RealComponent : React.Component;
|
|
|
|
|
2020-07-28 20:06:03 +00:00
|
|
|
const name =
|
|
|
|
RealComponent.displayName ||
|
|
|
|
RealComponent.name ||
|
|
|
|
(RealComponent.render // handle React.forwardRef
|
|
|
|
? RealComponent.render.displayName || RealComponent.render.name
|
|
|
|
: 'Unknown');
|
|
|
|
|
|
|
|
const nameWithoutPrefix = name.replace(/^(RCT|RK)/, '');
|
|
|
|
|
2018-05-09 07:47:48 +00:00
|
|
|
const Component = class extends SuperClass {
|
2018-06-26 14:38:22 +00:00
|
|
|
static displayName = 'Component';
|
|
|
|
|
2016-10-17 15:40:11 +00:00
|
|
|
render() {
|
2018-06-01 17:11:23 +00:00
|
|
|
const props = Object.assign({}, RealComponent.defaultProps);
|
|
|
|
|
|
|
|
if (this.props) {
|
2020-03-25 04:35:58 +00:00
|
|
|
Object.keys(this.props).forEach(prop => {
|
2018-06-01 17:11:23 +00:00
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:06:03 +00:00
|
|
|
return React.createElement(nameWithoutPrefix, props, this.props.children);
|
2016-10-17 15:40:11 +00:00
|
|
|
}
|
|
|
|
};
|
2018-05-09 07:47:48 +00:00
|
|
|
|
2020-07-28 20:06:03 +00:00
|
|
|
Component.displayName = nameWithoutPrefix;
|
|
|
|
|
2020-03-25 04:35:58 +00:00
|
|
|
Object.keys(RealComponent).forEach(classStatic => {
|
2018-06-20 04:28:00 +00:00
|
|
|
Component[classStatic] = RealComponent[classStatic];
|
|
|
|
});
|
2018-05-09 07:47:57 +00:00
|
|
|
|
2018-05-09 07:47:48 +00:00
|
|
|
if (instanceMethods != null) {
|
|
|
|
Object.assign(Component.prototype, instanceMethods);
|
|
|
|
}
|
|
|
|
|
2016-10-17 15:40:11 +00:00
|
|
|
return Component;
|
|
|
|
};
|