-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhelpers.js
189 lines (159 loc) · 4.23 KB
/
helpers.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
import React from 'react';
import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import * as cheerio from 'cheerio';
configure({ adapter: new Adapter() });
function _initStats() {
const _stats = {
success: 0,
fail: 0,
total: 0,
};
const fail = (addBy) => {
_stats.fail += addBy !== undefined ? addBy : 1;
_stats.total += addBy !== undefined ? addBy : 1;
};
const success = (addBy) => {
_stats.success += addBy !== undefined ? addBy : 1;
_stats.total += addBy !== undefined ? addBy : 1;
};
const setStats = ({ fail, success, total }) => {
_stats.success = success || 0;
_stats.fail = fail || 0;
_stats.total = total || 0;
};
return {
_stats,
fail,
success,
setStats,
};
}
function buildReactComponentList(
components,
prefix,
mockedProps = {},
customShallowComps = [],
scopeByKey = []
) {
const compKeys = Object.keys(components);
const { fail, success, _stats } = new _initStats();
const list = { _stats };
compKeys.forEach((compKey) => {
if (scopeByKey.length && scopeByKey.indexOf(compKey) === -1) {
return false;
}
const Comp = components[compKey];
let $, attr, className, shallowComp, identifier;
try {
// 1. try a simple shallow render
try {
shallowComp = shallow(<Comp {...mockedProps} />);
} catch (_e) {
// 2. try it with children
try {
shallowComp = shallow(
<Comp key="children">
<li>...</li>
</Comp>
);
} catch (_e) {
for (let i = 0; i < customShallowComps.length; i++) {
// 3. try any custom shallow renders
try {
shallowComp = shallow(customShallowComps[i](Comp));
break;
} catch (_e) {
// do nothing if it fails until a little later
}
}
}
}
attr = shallowComp.props();
className = attr?.className;
if (!className) {
// try cheerio
$ = cheerio.load(shallowComp.html());
className = findClassName($, $('body'));
}
if (className) {
identifier = cleanupSelector(className, prefix);
if (identifier && !list[identifier]) {
list[identifier] = compKey;
success();
} else {
console.log(
`${prefix}${compKey}: Failed to find a unique identifier.`
);
fail();
}
} else {
console.log(`${prefix}${compKey}: Failed to find a unique identifier.`);
fail();
}
} catch (_error) {
console.log(`${prefix}${compKey}: Failed to render.`);
fail();
}
});
return list;
}
function findClassName($, comp) {
// depends on cheerio interface
const children = comp.children();
const nextUp = [];
let selectors = [];
for (let i = 0; i < children.length; i++) {
const child = $(children[i]);
const selector = child.attr('class');
selectors.push(selector);
nextUp.push(child);
}
selectors = selectors.filter((s) => s);
if (!selectors.length) {
for (let i = 0; i < nextUp.length; i++) {
const selector = findClassName($, nextUp[i]);
if (selector) {
selectors.push(selector);
break;
}
}
}
return selectors.filter((d) => d).join('+');
}
function cleanupSelector(fullSelector, prefix) {
fullSelector = fullSelector.trim().split('+');
fullSelector = fullSelector
.map((selector) => {
selector = selector
.replace(/[^\w\d-_\s]/g, '') // stripping out undesirable characters
.split(' ')
.filter((singleClassName) => {
const filterModifier =
singleClassName.replace(`${prefix}`, '').indexOf('--') === -1;
return filterModifier;
});
if (selector.length) {
return '.' + selector.join('.');
}
return '';
})
.filter((s) => s);
fullSelector = fullSelector.join('+');
return fullSelector;
}
function camelCase(str) {
return str
.split(' ')
.map((str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
})
.join('');
}
export {
buildReactComponentList,
findClassName,
cleanupSelector,
_initStats,
camelCase,
};