-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathreact-utils.js
118 lines (86 loc) · 4.01 KB
/
react-utils.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
/*global window Node*/
(function () {
const ELEMENT_NODE = 1;
const COMMENT_NODE = 8;
function copyReactObject (obj) {
var copiedObj = {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && prop !== 'children')
copiedObj[prop] = obj[prop];
}
return copiedObj;
}
function getComponentForDOMNode (el) {
if (!el || !(el.nodeType === ELEMENT_NODE || el.nodeType === COMMENT_NODE)) return null;
let component = null;
const emptyComponentFound = window['%testCafeReactEmptyComponent%'] &&
window['%testCafeReactEmptyComponent%'].length;
if (emptyComponentFound && el.nodeType === COMMENT_NODE)
component = window['%testCafeReactEmptyComponent%'].shift().__$$reactInstance;
else if (window['%testCafeReactFoundComponents%'].length)
component = window['%testCafeReactFoundComponents%'].filter(desc => desc.node === el)[0].component;
const props = component.stateNode && component.stateNode.props || component.memoizedProps;
const state = component.stateNode && component.stateNode.state || component.memoizedState;
const key = component.key;
return { props, state, key };
}
/*eslint-enable no-unused-vars*/
function getReact (node, fn) {
/*eslint-disable no-unused-vars*/
const componentInstance = getComponentForDOMNode(node);
if (!componentInstance) return null;
delete window['%testCafeReactSelector%'];
delete window['%testCafeReactEmptyComponent%'];
delete window['%testCafeReactFoundComponents%'];
if (typeof fn === 'function') {
return fn({
state: copyReactObject(componentInstance.state),
props: copyReactObject(componentInstance.props),
key: componentInstance.key
});
}
return {
state: copyReactObject(componentInstance.state),
props: copyReactObject(componentInstance.props),
key: componentInstance.key
};
}
function scanDOMNodeForReactInstance (el) {
if (!el || !(el.nodeType === ELEMENT_NODE || el.nodeType === COMMENT_NODE)) return null;
if (el.nodeType === COMMENT_NODE)
return el.__$$reactInstance.return.child;
for (var prop of Object.keys(el)) {
if (!/^__reactInternalInstance/.test(prop)) continue;
let nestedComponent = el[prop];
if (typeof nestedComponent.type !== 'string')
return nestedComponent;
let parentComponent = nestedComponent;
do {
nestedComponent = parentComponent;
parentComponent = nestedComponent.return;
} while (parentComponent && parentComponent.type && !(parentComponent.stateNode instanceof Node));
return nestedComponent;
}
}
function getRenderedComponentVersion (component, rootInstances) {
if (!component.alternate) return component;
let component1 = component;
let component2 = component.alternate;
while (component1.return) component1 = component1.return;
while (component2.return) component2 = component2.return;
if (rootInstances.indexOf(component1) > -1) return component;
return component.alternate;
}
function scanDOMNodeForReactComponent (domNode) {
const rootInstances = window['$testCafeReact16Roots'].map(rootEl => rootEl.return || rootEl);
const reactInstance = scanDOMNodeForReactInstance(domNode);
return getRenderedComponentVersion(reactInstance, rootInstances);
}
function getFoundComponentInstances () {
return window['%testCafeReactFoundComponents%'].map(desc => desc.component);
}
function getComponentKey (instance) {
return instance.key;
}
return { getReact, getComponentForDOMNode, scanDOMNodeForReactComponent, getFoundComponentInstances, getComponentKey };
})();