forked from enzymejs/enzyme
/
Utils.js
372 lines (311 loc) · 10.3 KB
/
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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* eslint no-use-before-define: 0 */
import isEqual from 'lodash.isequal';
import is from 'object-is';
import entries from 'object.entries';
import functionName from 'function.prototype.name';
import has from 'has';
import flat from 'array.prototype.flat';
import trim from 'string.prototype.trim';
import { get } from './configuration';
import { childrenOfNode } from './RSTTraversal';
import realGetAdapter from './getAdapter';
export const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
export function getAdapter(options = {}) {
console.warn('getAdapter from Utils is deprecated; please use ./getAdapter instead');
return realGetAdapter(options);
}
function validateMountOptions(attachTo, hydrateIn) {
if (attachTo && hydrateIn && attachTo !== hydrateIn) {
throw new TypeError('If both the `attachTo` and `hydrateIn` options are provided, they must be === (for backwards compatibility)');
}
}
export function makeOptions(options) {
const { attachTo: configAttachTo, hydrateIn: configHydrateIn, ...config } = get();
validateMountOptions(configAttachTo, configHydrateIn);
const { attachTo, hydrateIn } = options;
validateMountOptions(attachTo, hydrateIn);
// neither present: both undefined
// only attachTo present: attachTo set, hydrateIn undefined
// only hydrateIn present: both set to hydrateIn
// both present (and ===, per above): both set to hydrateIn
const finalAttachTo = hydrateIn || configHydrateIn || configAttachTo || attachTo || undefined;
const finalHydrateIn = hydrateIn || configHydrateIn || undefined;
const mountTargets = {
...(finalAttachTo && { attachTo: finalAttachTo }),
...(finalHydrateIn && { hydrateIn: finalHydrateIn }),
};
return {
...config,
...options,
...mountTargets,
};
}
export function isCustomComponentElement(inst, adapter) {
if (adapter.isCustomComponentElement) {
return !!adapter.isCustomComponentElement(inst);
}
return !!inst && adapter.isValidElement(inst) && typeof inst.type === 'function';
}
export function propsOfNode(node) {
return entries((node && node.props) || {})
.filter(([, value]) => typeof value !== 'undefined')
.reduce((acc, [key, value]) => Object.assign(acc, { [key]: value }), {});
}
export function typeOfNode(node) {
return node ? node.type : null;
}
export function nodeHasType(node, type) {
if (!type || !node) return false;
const adapter = realGetAdapter();
if (adapter.displayNameOfNode) {
const displayName = adapter.displayNameOfNode(node);
return displayName === type;
}
if (!node.type) return false;
if (typeof node.type === 'string') return node.type === type;
return (
typeof node.type === 'function' ? functionName(node.type) === type : node.type.name === type
) || node.type.displayName === type;
}
function internalChildrenCompare(a, b, lenComp, isLoose) {
const nodeCompare = isLoose ? nodeMatches : nodeEqual;
if (a === b) return true;
if (!Array.isArray(a) && !Array.isArray(b)) {
return nodeCompare(a, b, lenComp);
}
const flatA = flat(a, Infinity);
const flatB = flat(b, Infinity);
if (flatA.length !== flatB.length) return false;
if (flatA.length === 0 && flatB.length === 0) return true;
for (let i = 0; i < flatA.length; i += 1) {
if (!nodeCompare(flatA[i], flatB[i], lenComp)) return false;
}
return true;
}
function childrenMatch(a, b, lenComp) {
return internalChildrenCompare(a, b, lenComp, true);
}
function childrenEqual(a, b, lenComp) {
return internalChildrenCompare(a, b, lenComp, false);
}
function removeNullaryReducer(acc, [key, value]) {
const addition = value == null ? {} : { [key]: value };
return { ...acc, ...addition };
}
function internalNodeCompare(a, b, lenComp, isLoose) {
if (a === b) return true;
if (!a || !b) return false;
if (a.type !== b.type) return false;
let left = propsOfNode(a);
let right = propsOfNode(b);
if (isLoose) {
left = entries(left).reduce(removeNullaryReducer, {});
right = entries(right).reduce(removeNullaryReducer, {});
}
const leftKeys = Object.keys(left);
for (let i = 0; i < leftKeys.length; i += 1) {
const prop = leftKeys[i];
// we will check children later
if (prop === 'children') {
// continue;
} else if (!(prop in right)) {
return false;
} else if (right[prop] === left[prop]) {
// continue;
} else if (typeof right[prop] === typeof left[prop] && typeof left[prop] === 'object') {
if (!isEqual(left[prop], right[prop])) return false;
} else {
return false;
}
}
const leftHasChildren = 'children' in left;
const rightHasChildren = 'children' in right;
const childCompare = isLoose ? childrenMatch : childrenEqual;
if (leftHasChildren || rightHasChildren) {
if (!childCompare(
childrenToSimplifiedArray(left.children, isLoose),
childrenToSimplifiedArray(right.children, isLoose),
lenComp,
)) {
return false;
}
}
if (!isTextualNode(a)) {
const rightKeys = Object.keys(right);
return lenComp(leftKeys.length - leftHasChildren, rightKeys.length - rightHasChildren);
}
return false;
}
export function nodeMatches(a, b, lenComp = is) {
return internalNodeCompare(a, b, lenComp, true);
}
export function nodeEqual(a, b, lenComp = is) {
return internalNodeCompare(a, b, lenComp, false);
}
export function containsChildrenSubArray(match, node, subArray) {
const children = childrenOfNode(node);
const checker = (_, i) => arraysEqual(match, children.slice(i, i + subArray.length), subArray);
return children.some(checker);
}
function arraysEqual(match, left, right) {
return left.length === right.length && left.every((el, i) => match(el, right[i]));
}
function childrenToArray(children) {
const result = [];
const push = (el) => {
if (el === null || el === false || typeof el === 'undefined') return;
result.push(el);
};
if (Array.isArray(children)) {
children.forEach(push);
} else {
push(children);
}
return result;
}
export function childrenToSimplifiedArray(nodeChildren, isLoose = false) {
const childrenArray = childrenToArray(nodeChildren);
const simplifiedArray = [];
for (let i = 0; i < childrenArray.length; i += 1) {
const child = childrenArray[i];
const previousChild = simplifiedArray.pop();
if (typeof previousChild === 'undefined') {
simplifiedArray.push(child);
} else if (isTextualNode(child) && isTextualNode(previousChild)) {
simplifiedArray.push(previousChild + child);
} else {
simplifiedArray.push(previousChild);
simplifiedArray.push(child);
}
}
if (isLoose) {
return simplifiedArray.map(x => (typeof x === 'string' ? trim(x) : x));
}
return simplifiedArray;
}
function isTextualNode(node) {
return typeof node === 'string' || typeof node === 'number';
}
export function isReactElementAlike(arg, adapter) {
return adapter.isValidElement(arg) || isTextualNode(arg) || Array.isArray(arg);
}
// TODO(lmr): can we get rid of this outside of the adapter?
export function withSetStateAllowed(fn) {
// NOTE(lmr):
// this is currently here to circumvent a React bug where `setState()` is
// not allowed without global being defined.
let cleanup = false;
if (typeof global.document === 'undefined') {
cleanup = true;
global.document = {};
}
fn();
if (cleanup) {
// This works around a bug in node/jest in that developers aren't able to
// delete things from global when running in a node vm.
global.document = undefined;
delete global.document;
}
}
export function AND(fns) {
const fnsReversed = fns.slice().reverse();
return x => fnsReversed.every(fn => fn(x));
}
export function displayNameOfNode(node) {
if (!node) return null;
const { type } = node;
if (!type) return null;
return type.displayName || (typeof type === 'function' ? functionName(type) : type.name || type);
}
export function sym(s) {
return typeof Symbol === 'function' ? Symbol.for(`enzyme.${s}`) : s;
}
export function privateSet(obj, prop, value) {
Object.defineProperty(obj, prop, {
value,
enumerable: false,
writable: true,
});
}
export function cloneElement(adapter, el, props) {
return adapter.createElement(
el.type,
{ ...el.props, ...props },
);
}
export function spyMethod(instance, methodName, getStub = () => {}) {
let lastReturnValue;
const originalMethod = instance[methodName];
const hasOwn = has(instance, methodName);
let descriptor;
if (hasOwn) {
descriptor = Object.getOwnPropertyDescriptor(instance, methodName);
}
Object.defineProperty(instance, methodName, {
configurable: true,
enumerable: !descriptor || !!descriptor.enumerable,
value: getStub(originalMethod) || function spied(...args) {
const result = originalMethod.apply(this, args);
lastReturnValue = result;
return result;
},
});
return {
restore() {
if (hasOwn) {
if (descriptor) {
Object.defineProperty(instance, methodName, descriptor);
} else {
/* eslint-disable no-param-reassign */
instance[methodName] = originalMethod;
/* eslint-enable no-param-reassign */
}
} else {
/* eslint-disable no-param-reassign */
delete instance[methodName];
/* eslint-enable no-param-reassign */
}
},
getLastReturnValue() {
return lastReturnValue;
},
};
}
// adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68
export function shallowEqual(objA, objB) {
if (is(objA, objB)) {
return true;
}
if (!objA || !objB || typeof objA !== 'object' || typeof objB !== 'object') {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
keysA.sort();
keysB.sort();
// Test for A's keys different from B.
for (let i = 0; i < keysA.length; i += 1) {
if (!has(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
export function isEmptyValue(renderedValue) {
return renderedValue === null || renderedValue === false;
}
export function renderedDive(nodes) {
if (isEmptyValue(nodes)) {
return true;
}
return [].concat(nodes).every((n) => {
if (n) {
const { rendered } = n;
return isEmptyValue(rendered) || renderedDive(rendered);
}
return isEmptyValue(n);
});
}