forked from testing-library/native-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-component.js
53 lines (41 loc) · 1.65 KB
/
mock-component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react';
import { mockNativeMethods } from './mock-native-methods';
function mockComponent(path, { instanceMethods, displayName: customDisplayName } = {}) {
const RealComponent = jest.requireActual(path);
const displayName =
customDisplayName ||
RealComponent.displayName ||
RealComponent.name ||
(RealComponent.render // handle React.forwardRef
? RealComponent.render.displayName || RealComponent.render.name
: 'Unknown');
const SuperClass = typeof RealComponent === 'function' ? RealComponent : React.Component;
class Component extends SuperClass {
static displayName = displayName;
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(displayName, props, this.props.children);
}
}
Object.keys(RealComponent).forEach(classStatic => {
Component[classStatic] = RealComponent[classStatic];
});
Object.assign(Component.prototype, mockNativeMethods);
if (instanceMethods != null) {
Object.assign(Component.prototype, instanceMethods);
}
return Component;
}
export { mockComponent };