| 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 |
654x
653x
653x
653x
653x
426x
426x
566x
565x
135x
135x
549x
237x
134x
9x
9x
9x
38x
38x
38x
29x
29x
3x
29x
9x
2x
64x
64x
60x
60x
31x
29x
20x
9x
30x
30x
30x
1x
94x
165x
5x
143x
1x
142x
12x
130x
92x
1x
20x
20x
20x
17x
17x
14x
3x
33x
33x
16x
17x
1x
16x
| import React from 'react';
import { isEmpty } from 'underscore';
import isSubset from 'is-subset';
import {
coercePropValue,
propsOfNode,
isSimpleSelector,
splitSelector,
selectorError,
isCompoundSelector,
selectorType,
AND,
SELECTOR,
} from './Utils';
export function childrenOfNode(node) {
if (!node) return [];
const maybeArray = propsOfNode(node).children;
const result = [];
React.Children.forEach(maybeArray, child => result.push(child));
return result;
}
export function hasClassName(node, className) {
const classes = propsOfNode(node).className || '';
return ` ${classes} `.indexOf(` ${className} `) > -1;
}
export function treeForEach(tree, fn) {
fn(tree);
childrenOfNode(tree).forEach(node => treeForEach(node, fn));
}
export function treeFilter(tree, fn) {
const results = [];
treeForEach(tree, node => {
if (fn(node)) {
results.push(node);
}
});
return results;
}
export function pathToNode(node, root) {
const queue = [root];
const path = [];
while (queue.length) {
const current = queue.pop();
const children = childrenOfNode(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.apply(queue, children);
}
return null;
}
export function parentsOfNode(node, root) {
return pathToNode(node, root).reverse();
}
export function nodeHasId(node, id) {
return propsOfNode(node).id === id;
}
export function nodeHasProperty(node, propKey, stringifiedPropValue) {
const nodeProps = propsOfNode(node);
const propValue = coercePropValue(propKey, stringifiedPropValue);
const nodePropValue = nodeProps[propKey];
if (nodePropValue === undefined) {
return false;
}
if (propValue) {
return nodePropValue === propValue;
}
return nodeProps.hasOwnProperty(propKey);
}
export function nodeHasType(node, type) {
Iif (!type || !node) return false;
Iif (!node.type) return false;
if (typeof node.type === 'string') return node.type === type;
return node.type.name === type || node.type.displayName === type;
}
export function nodeMatchesObjectProps(node, props) {
return isSubset(propsOfNode(node), props);
}
export function buildPredicate(selector) {
switch (typeof selector) {
case 'function':
// selector is a component constructor
return node => node && node.type === selector;
case 'string':
if (!isSimpleSelector(selector)) {
throw selectorError(selector);
}
if (isCompoundSelector.test(selector)) {
return AND(splitSelector(selector).map(buildPredicate));
}
switch (selectorType(selector)) {
case SELECTOR.CLASS_TYPE:
return node => hasClassName(node, selector.substr(1));
case SELECTOR.ID_TYPE:
return node => nodeHasId(node, selector.substr(1));
case SELECTOR.PROP_TYPE:
const propKey = selector.split(/\[([a-zA-Z\-]*?)(=|\])/)[1];
const propValue = selector.split(/=(.*?)\]/)[1];
return node => nodeHasProperty(node, propKey, propValue);
default:
// selector is a string. match to DOM tag or constructor displayName
return node => nodeHasType(node, selector);
}
break;
case 'object':
if (!Array.isArray(selector) && selector !== null && !isEmpty(selector)) {
return node => nodeMatchesObjectProps(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`);
}
}
export function getTextFromNode(node) {
Iif (node === null || node === undefined) {
return '';
}
if (typeof node === 'string' || typeof node === 'number') {
return String(node);
}
if (node.type && typeof node.type === 'function') {
return `<${node.type.name || node.type.displayName} />`;
}
return childrenOfNode(node).map(getTextFromNode).join('').replace(/\s+/, ' ');
}
|