-
Notifications
You must be signed in to change notification settings - Fork 4
/
EnzymePageRenderer.js
120 lines (88 loc) · 2.5 KB
/
EnzymePageRenderer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const {
ClientPageRenderer,
PageRenderer,
PageRendererFactory,
Window,
} = require('@ima/core');
const { mount } = require('enzyme');
const ReactDOM = require('react-dom');
// @TODO: Delete this file with next major version as we are moving away from Enzyme and
// this file is not being used in any known projects.
class EnzymeReactDOM {
constructor() {
this._instances = [];
}
get __instances() {
return this._instances;
}
render(element, container, callback = () => {}) {
this.unmountComponentAtNode(container);
const wrapper = mount(element, { attachTo: container });
this._instances.push({ container, wrapper });
callback();
return wrapper;
}
hydrate(element, container, callback = () => {}) {
this._dropInstanceAtNode(container);
const wrapper = mount(element, { hydrateIn: container });
this._instances.push({ container, wrapper });
callback();
return wrapper;
}
unmountComponentAtNode(container) {
const instance = this._dropInstanceAtNode(container);
if (!instance) {
return false;
}
instance.wrapper.detach();
return true;
}
findDOMNode(...args) {
// eslint-disable-next-line react/no-find-dom-node
return ReactDOM.findDOMNode(...args);
}
createPortal(...args) {
return ReactDOM.createPortal(...args);
}
_dropInstanceAtNode(container) {
const instanceIndex = this._getInstanceContainerIndex(container);
if (!~instanceIndex) {
return null;
}
const instance = this._instances[instanceIndex];
this._instances.splice(instanceIndex, 1);
return instance;
}
_getInstanceContainerIndex(container) {
return this._instances.findIndex(
instance => instance.container === container
);
}
}
class EnzymePageRenderer extends ClientPageRenderer {
static initTestPageRenderer(ns, oc) {
oc.provide(PageRenderer, EnzymePageRenderer, [
PageRendererFactory,
'$Helper',
'$ReactDOM',
'$Dispatcher',
'$Settings',
Window,
]);
return { wrapper: () => oc.get(PageRenderer).__wrapper() };
}
constructor(...args) {
super(...args);
this._ReactDOM = new EnzymeReactDOM(this._viewContainer);
}
__wrapper() {
if (this._ReactDOM.__instances.length === 0) {
return null;
}
if (this._ReactDOM.__instances.length === 1) {
return this._ReactDOM.__instances[0].wrapper;
}
return this._ReactDOM.__instances.map(({ wrapper }) => wrapper);
}
}
module.exports = EnzymePageRenderer;