-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathShallowTraversal.js
172 lines (139 loc) · 4.32 KB
/
ShallowTraversal.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
import React from 'react';
import isEmpty from 'lodash/isEmpty';
import isSubset from 'is-subset';
import {
coercePropValue,
propsOfNode,
isSimpleSelector,
splitSelector,
selectorError,
isCompoundSelector,
selectorType,
AND,
SELECTOR,
nodeHasType,
} from './Utils';
export function childrenOfNode(node) {
if (!node) return [];
const maybeArray = propsOfNode(node).children;
const result = [];
React.Children.forEach(maybeArray, child => {
if (child !== null && child !== false && typeof child !== 'undefined') {
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) {
if (tree !== null && tree !== false && typeof tree !== 'undefined') {
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;
}
function pathFilter(path, fn) {
return path.filter(tree => treeFilter(tree, fn).length !== 0);
}
export function pathToNode(node, root) {
const queue = [root];
const path = [];
const hasNode = (testNode) => node === testNode;
while (queue.length) {
const current = queue.pop();
const children = childrenOfNode(current);
if (current === node) return pathFilter(path, hasNode);
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 descriptor = Object.getOwnPropertyDescriptor(nodeProps, propKey);
if (descriptor && descriptor.get) {
return false;
}
const nodePropValue = nodeProps[propKey];
if (nodePropValue === undefined) {
return false;
}
if (propValue) {
return nodePropValue === propValue;
}
return nodeProps.hasOwnProperty(propKey);
}
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);
}
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) {
if (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+/, ' ');
}