-
Notifications
You must be signed in to change notification settings - Fork 743
/
virtual-node.js
446 lines (372 loc) · 12.7 KB
/
virtual-node.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
describe('VirtualNode', () => {
const VirtualNode = axe.VirtualNode;
let node;
beforeEach(() => {
node = document.createElement('div');
});
it('should be a function', () => {
assert.isFunction(VirtualNode);
});
it('should accept three parameters', () => {
assert.lengthOf(VirtualNode, 3);
});
describe('prototype', () => {
it('should have public properties', () => {
const parent = {};
const vNode = new VirtualNode(node, parent, 'foo');
assert.equal(vNode.shadowId, 'foo');
assert.typeOf(vNode.children, 'array');
assert.equal(vNode.actualNode, node);
assert.equal(vNode.parent, parent);
});
it('should abstract Node properties', () => {
node = document.createElement('input');
node.id = 'monkeys';
const vNode = new VirtualNode(node);
assert.isDefined(vNode.props);
assert.equal(vNode.props.nodeType, 1);
assert.equal(vNode.props.nodeName, 'input');
assert.equal(vNode.props.id, 'monkeys');
assert.equal(vNode.props.type, 'text');
});
for (const [prop, tagName, examplePropValue] of [
['value', 'input', 'test value'],
['selected', 'option', true],
['checked', 'input', true],
['indeterminate', 'input', true],
['multiple', 'select', true]
]) {
describe(`props.${prop}`, () => {
it(`should reflect a ${tagName} element's ${prop} property`, () => {
node = document.createElement(tagName);
let vNode = new VirtualNode(node);
assert.equal(vNode.props[prop], '');
node[prop] = examplePropValue;
vNode = new VirtualNode(node);
assert.equal(vNode.props[prop], examplePropValue);
});
it('should be undefined for a text node', () => {
node = document.createTextNode('text content');
let vNode = new VirtualNode(node);
assert.equal(vNode.props[prop], undefined);
});
// Regression test for #4316
it(`should be resilient to text node with un-gettable ${prop} property`, () => {
node = document.createTextNode('text content');
Object.defineProperty(node, prop, {
get() {
throw new Error('Unqueryable value');
}
});
let vNode = new VirtualNode(node);
assert.throws(() => node[prop]);
assert.doesNotThrow(() => vNode.props[prop]);
assert.equal(vNode.props[prop], undefined);
});
});
}
it('should lowercase type', () => {
node = document.createElement('input');
node.setAttribute('type', 'COLOR');
const vNode = new VirtualNode(node);
assert.equal(vNode.props.type, 'color');
});
it('should default type to text', () => {
node = document.createElement('input');
const vNode = new VirtualNode(node);
assert.equal(vNode.props.type, 'text');
});
it('should default type to text if type is invalid', () => {
node = document.createElement('input');
node.setAttribute('type', 'woohoo');
const vNode = new VirtualNode(node);
assert.equal(vNode.props.type, 'text');
});
it('should lowercase nodeName', () => {
node = {
nodeName: 'FOOBAR'
};
const vNode = new VirtualNode(node);
assert.equal(vNode.props.nodeName, 'foobar');
});
describe('attr', () => {
it('should return the value of the given attribute', () => {
node.setAttribute('data-foo', 'bar');
const vNode = new VirtualNode(node);
assert.equal(vNode.attr('data-foo'), 'bar');
});
it('should return null for text nodes', () => {
node.textContent = 'hello';
const vNode = new VirtualNode(node.firstChild);
assert.isNull(vNode.attr('data-foo'));
});
it('should return null if getAttribute is not a function', () => {
node = {
nodeName: 'DIV',
getAttribute: null
};
const vNode = new VirtualNode(node);
assert.isNull(vNode.attr('data-foo'));
});
});
describe('hasAttr', () => {
it('should return true if the element has the attribute', () => {
node.setAttribute('foo', 'bar');
const vNode = new VirtualNode(node);
assert.isTrue(vNode.hasAttr('foo'));
});
it('should return false if the element does not have the attribute', () => {
const vNode = new VirtualNode(node);
assert.isFalse(vNode.hasAttr('foo'));
});
it('should return false for text nodes', () => {
node.textContent = 'hello';
const vNode = new VirtualNode(node.firstChild);
assert.isFalse(vNode.hasAttr('foo'));
});
it('should return false if hasAttribute is not a function', () => {
node = {
nodeName: 'DIV',
hasAttribute: null
};
const vNode = new VirtualNode(node);
assert.isFalse(vNode.hasAttr('foo'));
});
});
describe('attrNames', () => {
it('should return a list of attribute names', () => {
node.setAttribute('foo', 'bar');
const vNode = new VirtualNode(node);
assert.deepEqual(vNode.attrNames, ['foo']);
});
it('should work with clobbered attributes', () => {
node = document.createElement('form');
node.setAttribute('id', '123');
node.innerHTML = '<select name="attributes"></select>';
const vNode = new VirtualNode(node);
assert.deepEqual(vNode.attrNames, ['id']);
});
it('should return an empty array if there are no attributes', () => {
const vNode = new VirtualNode(node);
assert.deepEqual(vNode.attrNames, []);
});
});
describe('nodeIndex', () => {
it('increments nodeIndex when a parent is passed', () => {
const vHtml = new VirtualNode({ nodeName: 'html' });
const vHead = new VirtualNode({ nodeName: 'head' }, vHtml);
const vTitle = new VirtualNode({ nodeName: 'title' }, vHead);
const vBody = new VirtualNode({ nodeName: 'body' }, vHtml);
assert.equal(vHtml.nodeIndex, 0);
assert.equal(vHead.nodeIndex, 1);
assert.equal(vTitle.nodeIndex, 2);
assert.equal(vBody.nodeIndex, 3);
});
it('resets nodeIndex when no parent is passed', () => {
let vHtml = new VirtualNode({ nodeName: 'html' });
let vHead = new VirtualNode({ nodeName: 'head' }, vHtml);
assert.equal(vHtml.nodeIndex, 0);
assert.equal(vHead.nodeIndex, 1);
vHtml = new VirtualNode({ nodeName: 'html' });
vHead = new VirtualNode({ nodeName: 'head' }, vHtml);
assert.equal(vHtml.nodeIndex, 0);
assert.equal(vHead.nodeIndex, 1);
});
});
describe('checkbox properties', () => {
it('should reflect the checked property', () => {
const div = document.createElement('div');
const vDiv = new VirtualNode(div);
assert.isUndefined(vDiv.props.checked);
node = document.createElement('input');
node.setAttribute('type', 'checkbox');
const vUnchecked = new VirtualNode(node);
assert.isFalse(vUnchecked.props.checked);
node.click();
const vChecked = new VirtualNode(node);
assert.equal(vChecked.props.checked, true);
});
it('reflects the indeterminate property', () => {
const div = document.createElement('div');
const vDiv = new VirtualNode(div);
assert.isUndefined(vDiv.props.indeterminate);
node = document.createElement('input');
node.setAttribute('type', 'checkbox');
const vUnchecked = new VirtualNode(node);
assert.isFalse(vUnchecked.props.indeterminate);
node.indeterminate = true;
const vIndeterminate = new VirtualNode(node);
assert.isTrue(vIndeterminate.props.indeterminate);
});
});
describe.skip('isFocusable', () => {
let commons;
beforeEach(() => {
commons = axe.commons = axe.commons;
});
afterEach(() => {
axe.commons = commons;
});
it('should call dom.isFocusable', () => {
let called = false;
axe.commons = {
dom: {
isFocusable: () => {
called = true;
}
}
};
const vNode = new VirtualNode(node);
vNode.isFocusable;
assert.isTrue(called);
});
it('should only call dom.isFocusable once', () => {
let count = 0;
axe.commons = {
dom: {
isFocusable: () => {
count++;
}
}
};
const vNode = new VirtualNode(node);
vNode.isFocusable;
vNode.isFocusable;
vNode.isFocusable;
assert.equal(count, 1);
});
});
describe.skip('tabbableElements', () => {
let commons;
beforeEach(() => {
commons = axe.commons = axe.commons;
});
afterEach(() => {
axe.commons = commons;
});
it('should call dom.getTabbableElements', () => {
let called = false;
axe.commons = {
dom: {
getTabbableElements: () => {
called = true;
}
}
};
const vNode = new VirtualNode(node);
vNode.tabbableElements;
assert.isTrue(called);
});
it('should only call dom.getTabbableElements once', () => {
let count = 0;
axe.commons = {
dom: {
getTabbableElements: () => {
count++;
}
}
};
const vNode = new VirtualNode(node);
vNode.tabbableElements;
vNode.tabbableElements;
vNode.tabbableElements;
assert.equal(count, 1);
});
});
describe('getComputedStylePropertyValue', () => {
let computedStyle;
beforeEach(() => {
computedStyle = window.getComputedStyle;
});
afterEach(() => {
window.getComputedStyle = computedStyle;
});
it('should call window.getComputedStyle and return the property', () => {
let called = false;
window.getComputedStyle = () => {
called = true;
return {
getPropertyValue: () => {
return 'result';
}
};
};
const vNode = new VirtualNode(node);
const result = vNode.getComputedStylePropertyValue('prop');
assert.isTrue(called);
assert.equal(result, 'result');
});
it('should only call window.getComputedStyle and getPropertyValue once', () => {
let computedCount = 0;
let propertyCount = 0;
window.getComputedStyle = () => {
computedCount++;
return {
getPropertyValue: () => {
propertyCount++;
}
};
};
const vNode = new VirtualNode(node);
vNode.getComputedStylePropertyValue('prop');
vNode.getComputedStylePropertyValue('prop');
vNode.getComputedStylePropertyValue('prop');
assert.equal(computedCount, 1);
assert.equal(propertyCount, 1);
});
});
describe('clientRects', () => {
it('should call node.getClientRects', () => {
let called = false;
node.getClientRects = () => {
called = true;
return [];
};
const vNode = new VirtualNode(node);
vNode.clientRects;
assert.isTrue(called);
});
it('should only call node.getClientRects once', () => {
let count = 0;
node.getClientRects = () => {
count++;
return [];
};
const vNode = new VirtualNode(node);
vNode.clientRects;
vNode.clientRects;
vNode.clientRects;
assert.equal(count, 1);
});
it('should filter out 0 width rects', () => {
node.getClientRects = () => {
return [{ width: 10 }, { width: 0 }, { width: 20 }];
};
const vNode = new VirtualNode(node);
assert.deepEqual(vNode.clientRects, [{ width: 10 }, { width: 20 }]);
});
});
describe('boundingClientRect', () => {
it('should call node.getBoundingClientRect', () => {
let called = false;
node.getBoundingClientRect = () => {
called = true;
};
const vNode = new VirtualNode(node);
vNode.boundingClientRect;
assert.isTrue(called);
});
it('should only call node.getBoundingClientRect once', () => {
let count = 0;
node.getBoundingClientRect = () => {
count++;
};
const vNode = new VirtualNode(node);
vNode.boundingClientRect;
vNode.boundingClientRect;
vNode.boundingClientRect;
assert.equal(count, 1);
});
});
});
});