1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | module.exports = (moduleName, instanceMethods) => {
|
13 | const RealComponent = jest.requireActual(moduleName);
|
14 | const React = require('react');
|
15 |
|
16 | const SuperClass =
|
17 | typeof RealComponent === 'function' ? RealComponent : React.Component;
|
18 |
|
19 | const Component = class extends SuperClass {
|
20 | static displayName = 'Component';
|
21 |
|
22 | render() {
|
23 | const name =
|
24 | RealComponent.displayName ||
|
25 | RealComponent.name ||
|
26 | (RealComponent.render
|
27 | ? RealComponent.render.displayName || RealComponent.render.name
|
28 | : 'Unknown');
|
29 |
|
30 | const props = Object.assign({}, RealComponent.defaultProps);
|
31 |
|
32 | if (this.props) {
|
33 | Object.keys(this.props).forEach(prop => {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | if (this.props[prop] !== undefined) {
|
40 | props[prop] = this.props[prop];
|
41 | }
|
42 | });
|
43 | }
|
44 |
|
45 | return React.createElement(
|
46 | name.replace(/^(RCT|RK)/, ''),
|
47 | props,
|
48 | this.props.children,
|
49 | );
|
50 | }
|
51 | };
|
52 |
|
53 | Object.keys(RealComponent).forEach(classStatic => {
|
54 | Component[classStatic] = RealComponent[classStatic];
|
55 | });
|
56 |
|
57 | if (instanceMethods != null) {
|
58 | Object.assign(Component.prototype, instanceMethods);
|
59 | }
|
60 |
|
61 | return Component;
|
62 | };
|