-
Notifications
You must be signed in to change notification settings - Fork 24
/
preact-render-spy.js
441 lines (373 loc) · 11.3 KB
/
preact-render-spy.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
const {render, rerender, h, Component} = require('preact');
const isEqual = require('lodash.isequal');
const renderToString = require('preact-render-to-string/jsx');
const {isWhere} = require('./is-where');
const {selToWhere} = require('./sel-to-where');
const config = {
SPY_PRIVATE_KEY: 'SPY_PRIVATE_KEY',
createFragment: () => document.createDocumentFragment(),
toStringOptions: {shallow: true, skipFalseAttributes: false},
};
const spyWalk = (context, vdom, depth) => {
if (!vdom) {
return vdom;
}
const spyCreator = depth > context.renderedDepth ? createNoopSpy : createSpy;
if (typeof vdom.nodeName === 'function' && !vdom.nodeName.isSpy) {
vdom = Object.assign({}, vdom, {
nodeName: spyCreator(context, vdom.nodeName),
attributes: Object.assign({}, vdom.attributes, {
[config.SPY_PRIVATE_KEY]: {vdom, depth},
}),
});
}
else if (vdom.children) {
vdom = Object.assign({}, vdom, {
children: vdom.children.map(child => spyWalk(context, child, depth)),
});
}
return vdom;
};
const popSpyKey = _props => {
const {vdom: spyKey, depth: spyDepth} = _props[config.SPY_PRIVATE_KEY];
delete _props[config.SPY_PRIVATE_KEY];
return [spyKey, spyDepth, _props];
};
const setVDom = (context, spyKey, vdom) => {
context.vdomMap.set(spyKey, vdom);
context.vdomRevision++;
return vdom;
};
const NoopSpy = ()=>{};
const _createNoopSpy = (context, Component) => {
return function(_props) {
const [,, props] = popSpyKey(_props);
const vdom = {
nodeName: NoopSpy,
attributes: Object.assign({
component: Component,
}, props),
children: props.children,
};
delete vdom.attributes.children;
return vdom;
};
};
const createFuncSpy = (context, Component) => {
return function(_props, ...args) {
const [spyKey, depth, props] = popSpyKey(_props);
const output = Component.call(this, props, ...args);
return spyWalk(context, setVDom(context, spyKey, output), depth + 1);
};
};
const createClassSpy = (context, Component) => {
class Spy extends Component {
constructor(_props, ...args) {
const [spyKey, spyDepth, props] = popSpyKey(_props);
super(props, ...args);
context.keyMap.set(this, spyKey);
context.depthMap.set(this, spyDepth);
}
componentWillReceiveProps(_props, ...args) {
const [spyKey, spyDepth, props] = popSpyKey(_props);
context.keyMap.set(this, spyKey);
context.depthMap.set(this, spyDepth);
if (super.componentWillReceiveProps) {
super.componentWillReceiveProps(props, ...args);
}
}
render(...args) {
const spyKey = context.keyMap.get(this);
const spyDepth = context.depthMap.get(this);
context.componentMap.set(spyKey, this);
return spyWalk(context, setVDom(context, spyKey, super.render(...args)), spyDepth + 1);
}
}
return Spy;
};
const createNoopSpy = (context, Component) => {
if (context.componentNoopMap.get(Component)) {return context.componentNoopMap.get(Component);}
const Spy = _createNoopSpy(context, Component);
Spy.isSpy = true;
context.componentNoopMap.set(Component, Spy);
return Spy;
};
const createSpy = (context, Component) => {
if (context.componentMap.get(Component)) {return context.componentMap.get(Component);}
let Spy;
if (!Component.prototype || !Component.prototype.render) {
Spy = createFuncSpy(context, Component);
}
else {
Spy = createClassSpy(context, Component);
}
Spy.isSpy = true;
context.componentMap.set(Component, Spy);
return Spy;
};
const vdomIter = function* (vdomMap, vdom) {
if (!vdom) {
return;
}
yield vdom;
if (typeof vdom.nodeName === 'function' && vdomMap.has(vdom)) {
yield* vdomIter(vdomMap, vdomMap.get(vdom));
}
else {
for (const child of (vdom.children || [])) {
yield* vdomIter(vdomMap, child);
}
}
};
const vdomWalk = function* (vdomMap, iter) {
for (const vdom of iter) {
yield* vdomIter(vdomMap, vdom);
}
};
const skip = function* (count, iter) {
for (let i = 0; i < count; i++) {
if (iter.next().done) {break;}
}
yield* iter;
};
const vdomFilter = (pred, vdomMap, vdom) => {
return Array.from(skip(1, vdomIter(vdomMap, vdom))).filter(pred);
};
const verifyFoundNodes = wrapper => {
if (wrapper.vdomRevision !== wrapper.context.vdomRevision) {
console.warn('preact-render-spy: Warning! Performing operation on stale find() result.');
}
};
class FindWrapper {
constructor(context, _iter, selector) {
// Set a non-enumerable property for context. In case a user does an deep
// equal comparison this removes the chance for recursive comparisons.
setHiddenProp(this, 'context', context || this);
setHiddenProp(this, 'vdomRevision', this.context.vdomRevision);
this.length = 0;
let iter = _iter;
if (selector) {
Object.defineProperty(this, 'selector', {enumerable: false, value: selector});
iter = [].concat(...Array.from(iter, root => (
Array.from(vdomFilter(isWhere(selToWhere(selector)), context.vdomMap, root))
)));
}
iter
.forEach((element, index) => {
this[index] = element;
this.length = index + 1;
});
}
at(index) {
if (index >= this.length) {
throw new Error(`preact-render-spy: Must have enough results for .at(${index}).`);
}
verifyFoundNodes(this);
return new FindWrapper(this.context, [this[index]]);
}
attr(name) {
if (this.length > 1 || this.length === 0) {
throw new Error(`preact-render-spy: Must have only 1 result for .attr(${name})`);
}
verifyFoundNodes(this);
const item = this[0];
if (
typeof item === 'object' &&
item.attributes &&
item.attributes[name]
) {
return item.attributes[name];
}
}
/**
* Return an object copy of the attributes from the first node that matched.
*/
attrs() {
if (this.length > 1 || this.length === 0) {
throw new Error('preact-render-spy: Must have only 1 result for .attrs().');
}
verifyFoundNodes(this);
return Object.assign({}, this[0].attributes);
}
/**
* Return the text of all nested children concatenated together.
*/
text() {
verifyFoundNodes(this);
return Array.from(vdomWalk(this.context.vdomMap, Array.from(this)))
// Filter for strings (text nodes)
.filter(value => typeof value === 'string')
// Concatenate all strings together
.join('');
}
contains(vdom) {
verifyFoundNodes(this);
return Array.from(vdomWalk(this.context.vdomMap, Array.from(this)))
.filter(value => isEqual(vdom, value))
.length > 0;
}
simulate(event, ...args) {
verifyFoundNodes(this);
for (let i = 0; i < this.length; i++) {
const vdom = this[i];
const eventlc = event.toLowerCase();
const eventKeys = new Set([`on${eventlc}`, `on${eventlc}capture`]);
for (const key in vdom.attributes) {
if (eventKeys.has(key.toLowerCase())) {
vdom.attributes[key](...args);
break;
}
}
}
rerender();
}
find(selector) {
verifyFoundNodes(this);
return new FindWrapper(this.context, Array.from(this), selector);
}
filter(selector) {
verifyFoundNodes(this);
return new FindWrapper(
this.context,
Array.from(this).filter(isWhere(selToWhere(selector)))
);
}
component() {
if (this.length !== 1) {
throw new Error('preact-render-spy: component method can only be used on a single node');
}
verifyFoundNodes(this);
const ref = this.context.componentMap.get(this[0]);
if (!ref) {
throw new Error('preact-render-spy: not a component node, no instance to return');
}
return ref;
}
setState(...args) {
const val = this.component().setState(...args);
rerender();
return val;
}
state(key) {
const state = this.component().state;
if (key) {
return state[key];
}
return state;
}
output() {
if (this.length > 1 || this.length === 0) {
throw new Error('preact-render-spy: Must have only 1 result for .output().');
}
if (!this[0] || typeof this[0].nodeName !== 'function') {
throw new Error('preact-render-spy: Must have a result of a preact class or function component for .output()');
}
verifyFoundNodes(this);
const getOutput = node => {
if (!node || typeof node !== 'object') {
return node;
}
// recursively resolve the node
let nodeOutput = node;
while (this.context.vdomMap.has(nodeOutput)) {
nodeOutput = this.context.vdomMap.get(nodeOutput);
}
// in case the node output null or false...
if (!nodeOutput || typeof nodeOutput !== 'object') {
return nodeOutput;
}
const clone = h(
nodeOutput.nodeName,
nodeOutput.attributes,
nodeOutput.children && nodeOutput.children.map(getOutput)
);
return clone;
};
return getOutput(this[0]);
}
toString() {
verifyFoundNodes(this);
const render = (jsx, index) => {
if (typeof jsx.nodeName === 'function') {
jsx = this.at(index).output();
}
if (!jsx) return `{${JSON.stringify(jsx)}}`;
return renderToString(jsx, {}, config.toStringOptions, true);
};
const header = `preact-render-spy (${this.length} nodes)`;
if (!this.length) {
return header;
}
return `${header}
-------
${Array.from(this).map(render).join('\n')}
`;
}
}
const setHiddenProp = (object, prop, value) => {
Object.defineProperty(object, prop, {
enumerable: false,
configurable: true,
writable: true,
value,
});
return value;
};
class ContextRootWrapper extends Component {
constructor(props) {
super(props);
const { vdom, renderRef } = props;
this.state = {vdom};
// setup the re-renderer - call the `renderRef` callback with a
// function that will re-render the content here
renderRef(vdom => {
this.setState({ vdom });
rerender();
});
}
render({}, {vdom}) {
return vdom;
}
}
class RenderContext extends FindWrapper {
constructor({depth}) {
super(null, []);
setHiddenProp(this, 'context', this);
setHiddenProp(this, 'renderedDepth', (depth || Infinity) - 1);
setHiddenProp(this, 'fragment', config.createFragment());
setHiddenProp(this, 'vdomRevision', 0);
// Create our Maps
['keyMap', 'depthMap', 'componentMap', 'componentNoopMap', 'vdomMap'].forEach(prop => {
setHiddenProp(this, prop, new Map());
});
// Render an Empty ContextRootWrapper. This sets up `this.contextRender`.
render({
nodeName: ContextRootWrapper,
attributes: {
vdom: null,
renderRef: contextRender => setHiddenProp(this, 'contextRender', contextRender),
},
}, this.fragment);
}
render(vdom) {
// Add what we need to be a FindWrapper:
this[0] = vdom;
this.length = 1;
this.contextRender(spyWalk(this, setVDom(this, 'root', vdom), 0));
return this;
}
rerender() {
rerender();
}
}
const deep = (vdom, {depth = Infinity} = {}) => new RenderContext({depth}).render(vdom);
const shallow = vdom => deep(vdom, {depth: 1});
exports = module.exports = deep;
exports.config = config;
exports.deep = deep;
exports.default = deep;
exports.render = deep;
exports.rerender = rerender;
exports.shallow = shallow;
exports.RenderContext = RenderContext;
exports.FindWrapper = FindWrapper;