-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.js
497 lines (385 loc) · 13 KB
/
index.spec.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
let sinon = require('sinon');
let d = require('.');
let { JSDOM } = require('jsdom');
let { assert } = require('chai');
let jsdom = new JSDOM('<!doctype html>');
for (let k of [
'Comment',
'HTMLElement',
'NodeFilter',
'Text',
'document',
]) {
global[k] = jsdom.window[k];
}
describe('Binding', () => {
describe('constructor', () => {
it('accepts and stores BindingGetterSetter object', () => {
let props = { get: () => 1, set: x => null };
let b = new d.Binding(props);
assert.equal(b.get, props.get);
assert.equal(b.set, props.set);
});
it('accepts and stores BindingGetters array', () => {
let getters = [() => 1, () => 2];
let b = new d.Binding(getters);
assert.equal(b.get, getters);
});
it('accepts and stores a single BindingGetter', () => {
let getter = () => 1;
let b = new d.Binding(getter);
assert.equal(b.get, getter);
});
});
});
describe('binding', () => {
it('is a function that creates a Binding', () => {
let getter = () => 1;
let b = d.binding(getter);
assert.instanceOf(b, d.Binding);
assert.equal(b.get, getter);
});
});
describe('el', () => {
describe('when type implements IRenderable', () => {
let instance;
beforeEach(() => { instance = null });
class TestComponent {
constructor(props) {
this.props = props;
instance = this;
}
render() {
return null;
}
}
it('instantiates it, calls the render method, and returns the result', () => {
let props = { test: 123 };
let el = d.el(TestComponent, props);
assert.isNull(el);
assert.instanceOf(instance, TestComponent);
assert.equal(instance.props.test, 123);
});
it('passes flattened children array as prop', () => {
let children = [[d.el('div')], [[d.el('div')]]];
let el = d.el(TestComponent, null, children);
assert.deepEqual(instance.props.children, children.flat(10));
});
});
describe('when type is any other function', () => {
it('calls the function and returns the result', () => {
let el = document.createElement('div');
let fn = sinon.fake.returns(el);
let props = { test: 123 };
let el2 = d.el(fn, props);
assert.equal(el, el2);
assert.isTrue(fn.called);
assert.deepEqual(fn.args[0][0].test, 123);
});
it('passes flattened children array as prop', () => {
let fn = sinon.fake();
let children = [[d.el('div')], [[d.el('div')]]];
let el = d.el(fn, null, children);
assert.deepEqual(fn.args[0][0].children, children.flat(10));
});
});
describe('when type is a string', () => {
it('creates the specified element', () => {
let el = d.el('div');
assert.instanceOf(el, HTMLElement);
assert.equal(el && el.tagName, 'DIV');
});
describe('when props are supplied', () => {
it('adds event listeners for event listener props', () => {
let fn = sinon.fake();
let el = d.el('button', { onClick: fn });
assert.isFalse(fn.called);
el.click();
assert.isTrue(fn.called);
});
it('stores Binding props on el.bindings', () => {
let b = d.binding({ get: () => 1, set: x => null });
let el = d.el('input', { value: b });
assert.deepEqual(el.bindings, [b]);
});
it('wraps non-event listener function props as Bindings and store on el.bindings', () => {
let fn = () => 'test';
let el = d.el('input', { value: fn });
assert.instanceOf(el.bindings[0], d.Binding);
assert.equal(el.bindings[0].get, fn);
});
it('sets class prop to the created element (array)', () => {
let el = d.el('div', { class: ['test1', 'test2'] });
assert.equal(el.className, 'test1 test2');
});
it('sets class prop to the created element (array of space-delimited class names)', () => {
let el = d.el('div', { class: ['test1 test2', 'test3 test4 '] });
assert.equal(el.className, 'test1 test2 test3 test4');
});
it('sets class prop to the created element (string of space-delimited class names)', () => {
let el = d.el('div', { class: 'test1 test2' });
assert.equal(el.className, 'test1 test2');
});
it('sets style prop to the created element (object)', () => {
let el = d.el('div', { style: { textAlign: 'center' } });
assert.equal(el.style.getPropertyValue('text-align'), 'center');
});
it('sets style prop to the created element (string)', () => {
let el = d.el('div', { style: 'text-align: center' });
assert.equal(el.style.getPropertyValue('text-align'), 'center');
});
it('sets regular props to the created element', () => {
let el = d.el('div', { id: 'test' });
assert.equal(el.id, 'test');
});
});
describe('when children are supplied', () => {
it('flattens the array and appends them to the created element', () => {
let children = [[d.el('div')], [[d.el('div')]]];
let el = d.el('div', null, children);
assert.deepEqual([...el.children], children.flat(10));
});
});
});
});
describe('comment', () => {
it('creates and returns the properly formatted comment (when text.length > 0)', () => {
let c = d.comment('test');
assert.instanceOf(c, Comment);
assert.equal(c.textContent, ' test ');
});
it('creates and returns the properly formatted comment (when text.length === 0)', () => {
let c = d.comment();
assert.instanceOf(c, Comment);
assert.equal(c.textContent, ' ');
});
});
describe('if', () => {
it('creates and returns the properly formatted comment node', () => {
let c = d.if(() => false, document.createElement('div'));
assert.instanceOf(c, Comment);
assert.equal(c.textContent, ' if anchor ');
});
it('creates and stores the conditional Binding on c.bindings (thenNode only)', () => {
let fn = () => false;
let el = document.createElement('div');
let c = d.if(fn, el);
assert.instanceOf(c.bindings[0], d.Binding);
assert.equal(c.bindings[0].get, fn);
assert.equal(c.bindings[0].thenNode, el);
});
it('creates and stores the conditional Binding on c.bindings (thenNode + elseNode)', () => {
let fn = () => false;
let thenNode = document.createElement('div');
let elseNode = document.createElement('div');
let c = d.if(fn, thenNode, elseNode);
assert.instanceOf(c.bindings[0], d.Binding);
assert.equal(c.bindings[0].get, fn);
assert.equal(c.bindings[0].thenNode, thenNode);
assert.equal(c.bindings[0].elseNode, elseNode);
});
});
describe('map', () => {
it('creates and returns the properly formatted comment node', () => {
let c = d.map(() => [1], x => document.createElement('div'));
assert.instanceOf(c, Comment);
assert.equal(c.textContent, ' map anchor ');
});
it('creates and stores the conditional Binding on c.bindings', () => {
let getFn = () => [1];
let mapFn = x => document.createElement('div');
let c = d.map(getFn, mapFn);
assert.instanceOf(c.bindings[0], d.Binding);
assert.equal(c.bindings[0].get, getFn);
assert.equal(c.bindings[0].map, mapFn);
});
});
describe('text', () => {
it('creates and returns a text node', () => {
let n = d.text(() => 'hello');
assert.instanceOf(n, Text);
});
it('creates and stores the textContent Binding on n.bindings', () => {
let getFn = () => 'hello';
let n = d.text(getFn);
assert.instanceOf(n.bindings[0], d.Binding);
assert.equal(n.bindings[0].get, getFn);
});
});
describe('processMutations', () => {
let makeBoundNode = () => {
let n = document.createElement('div');
n.bindings = true;
return n;
};
it('adds addedNodes with n.bindings to boundNodes', () => {
let boundNodes = [];
let nodes = [
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
];
let muts = [
{ addedNodes: nodes.slice(0, 4), removedNodes: [] },
{ addedNodes: nodes.slice(4, 8), removedNodes: [] },
];
let update = sinon.fake();
d.processMutations(muts, null, { boundNodes, update });
assert.sameMembers(boundNodes, nodes.filter(n => n.bindings));
});
it('updates addedNodes with n.bindings', () => {
let boundNodes = [];
let nodes = [
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
];
let muts = [
{ addedNodes: nodes.slice(0, 4), removedNodes: [] },
{ addedNodes: nodes.slice(4, 8), removedNodes: [] },
];
let update = sinon.fake();
d.processMutations(muts, null, { boundNodes, update });
for (let n of nodes.filter(n => n.bindings)) {
assert.ok(update.calledWith(n));
}
});
it('removes removedNodes with n.bindings from boundNodes', () => {
let removedNodes = [
makeBoundNode(),
makeBoundNode(),
];
let allNodes = [
removedNodes[0],
makeBoundNode(),
removedNodes[1],
makeBoundNode(),
];
let boundNodes = [];
let muts = [
{ addedNodes: [], removedNodes: [removedNodes[0]] },
{ addedNodes: [], removedNodes: [removedNodes[1]] },
];
let update = sinon.fake();
d.processMutations(muts, null, { boundNodes, update });
for (let n of removedNodes) {
assert.notInclude(boundNodes, n);
}
});
it('adds addedNodes descendants with n.bindings to boundNodes', () => {
let boundNodes = [];
let parentNodes = [
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
];
let descendantNodes = [
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
];
parentNodes[0].append(descendantNodes[0], descendantNodes[1]);
parentNodes[3].append(descendantNodes[2], descendantNodes[3]);
let muts = [
{ addedNodes: parentNodes.slice(0, 2), removedNodes: [] },
{ addedNodes: parentNodes.slice(2, 4), removedNodes: [] },
];
let update = sinon.fake();
d.processMutations(muts, null, { boundNodes, update });
for (let n of descendantNodes.filter(n => n.bindings)) {
assert.include(boundNodes, n);
}
});
it('updates addedNodes descendants with n.bindings', () => {
let boundNodes = [];
let parentNodes = [
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
];
let descendantNodes = [
document.createElement('div'),
makeBoundNode(),
document.createElement('div'),
makeBoundNode(),
];
parentNodes[0].append(descendantNodes[0], descendantNodes[1]);
parentNodes[3].append(descendantNodes[2], descendantNodes[3]);
let muts = [
{ addedNodes: parentNodes.slice(0, 2), removedNodes: [] },
{ addedNodes: parentNodes.slice(2, 4), removedNodes: [] },
];
let update = sinon.fake();
d.processMutations(muts, null, { boundNodes, update });
for (let n of descendantNodes.filter(n => n.bindings)) {
assert.ok(update.calledWith(n));
}
});
it('removes removedNodes descendants with n.bindings from boundNodes', () => {
let removedNodes = [
document.createElement('div'),
document.createElement('div'),
];
let removedDescendantNodes = [
makeBoundNode(),
makeBoundNode(),
];
removedNodes[0]
.append(document.createElement('div'), removedDescendantNodes[0]);
removedNodes[1]
.append(document.createElement('div'), removedDescendantNodes[1]);
let allNodes = [
makeBoundNode(),
removedNodes[0],
removedDescendantNodes[0],
makeBoundNode(),
removedNodes[1],
removedDescendantNodes[1],
];
let boundNodes = [...allNodes];
let muts = [
{ addedNodes: [], removedNodes: [removedNodes[0]] },
{ addedNodes: [], removedNodes: [removedNodes[1]] },
];
let update = sinon.fake();
d.processMutations(muts, null, { boundNodes, update });
for (let n of removedDescendantNodes) {
assert.notInclude(boundNodes, n);
}
});
});
describe('resolve', () => {
it('returns x() when x is a Function', () => {
assert.equal(d.resolve(() => 123), 123);
});
it('returns x when x is not a Function', () => {
assert.equal(d.resolve(321), 321);
});
});
describe('update', () => {
let makeBoundNode = b => {
let n = document.createElement('div');
n.bindings = [b];
return n;
};
it('updates bindings from all boundNodes', () => {
let fns = [1, 2].map(sinon.fake.returns);
let boundNodes = fns.map(fn => d.el('input', { value: fn }));
d.update(null, { boundNodes });
assert.equal(boundNodes[0].value, '1');
assert.equal(boundNodes[1].value, '2');
});
});