-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
MountedTraversal.js
300 lines (267 loc) · 8.1 KB
/
MountedTraversal.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import isEmpty from 'lodash/isEmpty';
import isSubset from 'is-subset';
import {
internalInstance,
coercePropValue,
nodeEqual,
propsOfNode,
isFunctionalComponent,
splitSelector,
selectorType,
isCompoundSelector,
AND,
SELECTOR,
nodeHasType,
} from './Utils';
import {
isDOMComponent,
isCompositeComponent,
isCompositeComponentWithType,
isElement,
findDOMNode,
} from './react-compat';
import { REACT013 } from './version';
export function getNode(inst) {
if (!inst || inst._store || typeof inst === 'string') {
return inst;
}
if (inst._currentElement) {
return inst._currentElement;
}
if (internalInstance(inst)) {
return internalInstance(inst)._currentElement;
}
if (inst._reactInternalInstance) {
return inst._reactInternalInstance._currentElement;
}
if (inst._reactInternalComponent) {
return inst._reactInternalComponent._currentElement;
}
return inst;
}
export function instEqual(a, b, lenComp) {
return nodeEqual(getNode(a), getNode(b), lenComp);
}
export function instHasClassName(inst, className) {
if (!isDOMComponent(inst)) {
return false;
}
const node = findDOMNode(inst);
if (node.classList) {
return node.classList.contains(className);
}
let classes = node.className || '';
if (typeof classes === 'object') {
classes = classes.baseVal;
}
classes = classes.replace(/\s/g, ' ');
return ` ${classes} `.indexOf(` ${className} `) > -1;
}
export function instHasId(inst, id) {
if (!isDOMComponent(inst)) return false;
const instId = findDOMNode(inst).id || '';
return instId === id;
}
function isFunctionalComponentWithType(inst, func) {
return isFunctionalComponent(inst) && getNode(inst).type === func;
}
export function instHasType(inst, type) {
switch (typeof type) {
case 'string':
return nodeHasType(getNode(inst), type);
case 'function':
return isCompositeComponentWithType(inst, type) ||
isFunctionalComponentWithType(inst, type);
default:
return false;
}
}
export function instHasProperty(inst, propKey, stringifiedPropValue) {
if (!isDOMComponent(inst)) return false;
const node = getNode(inst);
const nodeProps = propsOfNode(node);
const descriptor = Object.getOwnPropertyDescriptor(nodeProps, propKey);
if (descriptor && descriptor.get) {
return false;
}
const nodePropValue = nodeProps[propKey];
const propValue = coercePropValue(propKey, stringifiedPropValue);
// intentionally not matching node props that are undefined
if (nodePropValue === undefined) {
return false;
}
if (propValue) {
return nodePropValue === propValue;
}
return Object.prototype.hasOwnProperty.call(nodeProps, propKey);
}
// called with private inst
export function renderedChildrenOfInst(inst) {
return REACT013
? inst._renderedComponent._renderedChildren
: inst._renderedChildren;
}
// called with a private instance
export function childrenOfInstInternal(inst) {
if (!inst) {
return [];
}
if (!inst.getPublicInstance) {
const internal = internalInstance(inst);
return childrenOfInstInternal(internal);
}
const publicInst = inst.getPublicInstance();
const currentElement = inst._currentElement;
if (isDOMComponent(publicInst)) {
const renderedChildren = renderedChildrenOfInst(inst);
return Object.keys(renderedChildren || {}).filter((key) => {
if (REACT013 && !renderedChildren[key].getPublicInstance) {
return false;
}
return true;
}).map(key => {
if (!REACT013 && typeof renderedChildren[key]._currentElement.type === 'function') {
return renderedChildren[key]._instance;
}
return renderedChildren[key].getPublicInstance();
});
} else if (
!REACT013 &&
isElement(currentElement) &&
typeof currentElement.type === 'function'
) {
return childrenOfInstInternal(inst._renderedComponent);
} else if (
REACT013 &&
isCompositeComponent(publicInst)
) {
return childrenOfInstInternal(inst._renderedComponent);
}
return [];
}
export function internalInstanceOrComponent(node) {
if (REACT013) {
return node;
} else if (node._reactInternalComponent) {
return node._reactInternalComponent;
} else if (node._reactInternalInstance) {
return node._reactInternalInstance;
}
return node;
}
export function childrenOfInst(node) {
return childrenOfInstInternal(internalInstanceOrComponent(node));
}
export function pathToNode(node, root) {
const queue = [root];
const path = [];
while (queue.length) {
const current = queue.pop();
const children = childrenOfInst(current);
if (current === node) return path;
path.push(current);
if (children.length === 0) {
// leaf node. if it isn't the node we are looking for, we pop.
path.pop();
}
queue.push(...children);
}
return null;
}
export function parentsOfInst(inst, root) {
return pathToNode(inst, root).reverse();
}
export function instMatchesObjectProps(inst, props) {
if (!isDOMComponent(inst)) return false;
const node = getNode(inst);
return isSubset(propsOfNode(node), props);
}
export function buildInstPredicate(selector) {
switch (typeof selector) {
case 'function':
// selector is a component constructor
return inst => instHasType(inst, selector);
case 'string':
if (isCompoundSelector.test(selector)) {
return AND(splitSelector(selector).map(buildInstPredicate));
}
switch (selectorType(selector)) {
case SELECTOR.CLASS_TYPE:
return inst => instHasClassName(inst, selector.substr(1));
case SELECTOR.ID_TYPE:
return inst => instHasId(inst, selector.substr(1));
case SELECTOR.PROP_TYPE: {
const propKey = selector.split(/\[([a-zA-Z][a-zA-Z_\d\-:]*?)(=|\])/)[1];
const propValue = selector.split(/=(.*?)]/)[1];
return node => instHasProperty(node, propKey, propValue);
}
default:
// selector is a string. match to DOM tag or constructor displayName
return inst => instHasType(inst, selector);
}
case 'object':
if (!Array.isArray(selector) && selector !== null && !isEmpty(selector)) {
return node => instMatchesObjectProps(node, selector);
}
throw new TypeError(
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
default:
throw new TypeError('Enzyme::Selector expects a string, object, or Component Constructor');
}
}
// This function should be called with an "internal instance". Nevertheless, if it is
// called with a "public instance" instead, the function will call itself with the
// internal instance and return the proper result.
function findAllInRenderedTreeInternal(inst, test) {
if (!inst) {
return [];
}
if (!inst.getPublicInstance) {
const internal = internalInstance(inst);
return findAllInRenderedTreeInternal(internal, test);
}
const publicInst = inst.getPublicInstance() || inst._instance;
let ret = test(publicInst) ? [publicInst] : [];
const currentElement = inst._currentElement;
if (isDOMComponent(publicInst)) {
const renderedChildren = renderedChildrenOfInst(inst);
Object.keys(renderedChildren || {}).filter((key) => {
if (REACT013 && !renderedChildren[key].getPublicInstance) {
return false;
}
return true;
}).forEach((key) => {
ret = ret.concat(
findAllInRenderedTreeInternal(renderedChildren[key], test)
);
});
} else if (
!REACT013 &&
isElement(currentElement) &&
typeof currentElement.type === 'function'
) {
ret = ret.concat(
findAllInRenderedTreeInternal(
inst._renderedComponent,
test
)
);
} else if (
REACT013 &&
isCompositeComponent(publicInst)
) {
ret = ret.concat(
findAllInRenderedTreeInternal(
inst._renderedComponent,
test
)
);
}
return ret;
}
// This function could be called with a number of different things technically, so we need to
// pass the *right* thing to our internal helper.
export function treeFilter(node, test) {
return findAllInRenderedTreeInternal(internalInstanceOrComponent(node), test);
}