2016-10-17 18:40:11 +03:00
|
|
|
/**
|
2021-12-31 02:08:43 +03:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-10-17 18:40:11 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03: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 10:47:48 +03:00
|
|
|
*
|
|
|
|
* @format
|
2016-10-17 18:40:11 +03:00
|
|
|
*/
|
2018-05-09 10:47:48 +03:00
|
|
|
|
2016-11-21 04:49:21 +03:00
|
|
|
'use strict';
|
2016-10-17 18:40:11 +03:00
|
|
|
|
2018-05-09 10:47:48 +03:00
|
|
|
module.exports = (moduleName, instanceMethods) => {
|
2018-10-19 01:24:15 +03:00
|
|
|
const RealComponent = jest.requireActual(moduleName);
|
2016-10-17 18:40:11 +03:00
|
|
|
const React = require('react');
|
|
|
|
|
2018-05-09 10:47:48 +03:00
|
|
|
const SuperClass =
|
|
|
|
typeof RealComponent === 'function' ? RealComponent : React.Component;
|
|
|
|
|
2020-07-28 23:06:03 +03: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 10:47:48 +03:00
|
|
|
const Component = class extends SuperClass {
|
2018-06-26 17:38:22 +03:00
|
|
|
static displayName = 'Component';
|
|
|
|
|
2016-10-17 18:40:11 +03:00
|
|
|
render() {
|
2018-06-01 20:11:23 +03:00
|
|
|
const props = Object.assign({}, RealComponent.defaultProps);
|
|
|
|
|
|
|
|
if (this.props) {
|
2020-03-25 07:35:58 +03:00
|
|
|
Object.keys(this.props).forEach(prop => {
|
2018-06-01 20:11:23 +03: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 23:06:03 +03:00
|
|
|
return React.createElement(nameWithoutPrefix, props, this.props.children);
|
2016-10-17 18:40:11 +03:00
|
|
|
}
|
|
|
|
};
|
2018-05-09 10:47:48 +03:00
|
|
|
|
2020-07-28 23:06:03 +03:00
|
|
|
Component.displayName = nameWithoutPrefix;
|
|
|
|
|
2020-03-25 07:35:58 +03:00
|
|
|
Object.keys(RealComponent).forEach(classStatic => {
|
2018-06-20 07:28:00 +03:00
|
|
|
Component[classStatic] = RealComponent[classStatic];
|
|
|
|
});
|
2018-05-09 10:47:57 +03:00
|
|
|
|
2018-05-09 10:47:48 +03:00
|
|
|
if (instanceMethods != null) {
|
|
|
|
Object.assign(Component.prototype, instanceMethods);
|
|
|
|
}
|
|
|
|
|
2016-10-17 18:40:11 +03:00
|
|
|
return Component;
|
|
|
|
};
|