/** * 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; };