/
DOMElement.test.ts
567 lines (496 loc) · 15.3 KB
/
DOMElement.test.ts
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment jsdom
*/
/* eslint-env browser*/
import prettyFormat from '../';
import setPrettyPrint from './setPrettyPrint';
const {DOMElement} = prettyFormat.plugins;
setPrettyPrint([DOMElement]);
describe('pretty-format', () => {
// Test is not related to plugin but is related to jsdom testing environment.
it('prints global window as constructor name alone', () => {
expect(prettyFormat(window)).toEqual('[Window]');
});
});
describe('DOMElement Plugin', () => {
it('supports a single HTML element', () => {
expect(document.createElement('div')).toPrettyPrintTo('<div />');
});
it('supports an HTML element with a class property', () => {
const parent = document.createElement('div');
parent.className = 'classy';
expect(parent).toPrettyPrintTo('<div\n class="classy"\n/>');
});
it('supports an HTML element with a title property', () => {
const parent = document.createElement('div');
parent.title = 'title text';
expect(parent).toPrettyPrintTo('<div\n title="title text"\n/>');
});
test('escapes double quote in attribute value', () => {
const parent = document.createElement('div');
parent.setAttribute('title', '"escape"');
expect(parent).toPrettyPrintTo('<div\n title="\\"escape\\""\n/>');
});
it('supports an HTML element with a single attribute', () => {
const parent = document.createElement('div');
parent.setAttribute('class', 'classy');
expect(parent).toPrettyPrintTo('<div\n class="classy"\n/>');
});
it('supports an HTML element with multiple attributes', () => {
const parent = document.createElement('div');
// set attributes in unsorted order by name to verify sorting
parent.setAttribute('id', '123');
parent.setAttribute('class', 'classy');
expect(parent).toPrettyPrintTo('<div\n class="classy"\n id="123"\n/>');
});
it('supports an HTML element with attribute and text content', () => {
const parent = document.createElement('div');
parent.setAttribute('style', 'color: #99424F');
const text = document.createTextNode('Jest');
parent.appendChild(text);
expect(parent).toPrettyPrintTo(
'<div\n style="color: #99424F"\n>\n Jest\n</div>',
);
});
it('supports an element with text content', () => {
const parent = document.createElement('div');
const child = document.createTextNode('texty texty');
parent.appendChild(child);
expect(parent).toPrettyPrintTo('<div>\n texty texty\n</div>');
});
it('supports nested elements', () => {
const parent = document.createElement('div');
const child = document.createElement('span');
parent.appendChild(child);
expect(parent).toPrettyPrintTo('<div>\n <span />\n</div>');
});
it('supports nested elements with attributes', () => {
const parent = document.createElement('div');
const child = document.createElement('span');
parent.appendChild(child);
// set attributes in sorted order by name
child.setAttribute('class', 'classy');
child.setAttribute('id', '123');
expect(parent).toPrettyPrintTo(
'<div>\n <span\n class="classy"\n id="123"\n />\n</div>',
);
});
it('supports nested elements with attribute and text content', () => {
const parent = document.createElement('div');
const child = document.createElement('span');
parent.appendChild(child);
child.setAttribute('style', 'color: #99424F');
const text = document.createTextNode('Jest');
child.appendChild(text);
expect(parent).toPrettyPrintTo(
'<div>\n <span\n style="color: #99424F"\n >\n Jest\n </span>\n</div>',
);
});
it('supports nested elements with text content', () => {
const parent = document.createElement('div');
const child = document.createElement('span');
parent.appendChild(child);
child.textContent = 'texty texty';
expect(parent).toPrettyPrintTo(
'<div>\n <span>\n texty texty\n </span>\n</div>',
);
});
it('supports siblings', () => {
const parent = document.createElement('div');
parent.innerHTML = '<span>some </span><span>text</span>';
expect(parent).toPrettyPrintTo(
[
'<div>',
' <span>',
' some ',
' </span>',
' <span>',
' text',
' </span>',
'</div>',
].join('\n'),
);
});
it('supports multiline text node in pre', () => {
const parent = document.createElement('pre');
parent.innerHTML = [
// prettier-ignore
'function sum(a, b) {',
' return a + b;',
'}',
].join('\n');
// Ouch. Two lines of text have same indentation for different reason:
// First line of text node because it is at child level.
// Second line of text node because they are in its content.
expect(parent).toPrettyPrintTo(
// prettier-ignore
[
'<pre>',
' function sum(a, b) {',
' return a + b;',
'}',
'</pre>'
].join('\n'),
);
});
it('supports multiline text node preceding span in pre', () => {
const parent = document.createElement('pre');
parent.innerHTML = [
'<span class="token keyword">function</span> sum(a, b) {',
' <span class="token keyword">return</span> a + b;',
'}',
].join('\n');
expect(parent).toPrettyPrintTo(
[
'<pre>',
' <span',
' class="token keyword"',
' >',
' function',
' </span>',
' sum(a, b) {',
' ',
' <span',
' class="token keyword"',
' >',
' return',
' </span>',
' a + b;',
'}',
'</pre>',
].join('\n'),
);
});
it('supports multiline text node in textarea', () => {
const textarea = document.createElement('textarea');
textarea.setAttribute('name', 'tagline');
textarea.innerHTML = `Painless.
JavaScript.
Testing.`;
expect(textarea).toPrettyPrintTo(
[
'<textarea',
' name="tagline"',
'>',
' Painless.',
'JavaScript.',
'Testing.',
'</textarea>',
].join('\n'),
);
});
it('supports empty text node', () => {
// React 16 does not render text in comments (see below)
const parent = document.createElement('span');
const text = document.createTextNode('');
parent.appendChild(text);
const abbr = document.createElement('abbr');
abbr.setAttribute('title', 'meter');
abbr.innerHTML = 'm';
parent.appendChild(abbr);
expect(parent).toPrettyPrintTo(
[
'<span>',
' ',
' <abbr',
' title="meter"',
' >',
' m',
' </abbr>',
'</span>',
].join('\n'),
);
});
it('supports non-empty text node', () => {
// React 16 does not render text in comments (see below)
const parent = document.createElement('p');
parent.innerHTML = [
'<strong>Jest</strong>',
' means ',
'<em>painless</em>',
' Javascript testing',
].join('');
expect(parent).toPrettyPrintTo(
[
'<p>',
' <strong>',
' Jest',
' </strong>',
' means ',
' <em>',
' painless',
' </em>',
' Javascript testing',
'</p>',
].join('\n'),
);
});
it('supports comment node', () => {
// React 15 does render text in comments
const parent = document.createElement('p');
parent.innerHTML = [
'<strong>Jest</strong>',
'<!-- react-text: 3 -->',
' means ',
'<!-- /react-text -->',
'<em>painless</em>',
'<!-- react-text: 5 -->',
' Javascript testing',
'<!-- /react-text -->',
].join('');
expect(parent).toPrettyPrintTo(
[
'<p>',
' <strong>',
' Jest',
' </strong>',
' <!-- react-text: 3 -->',
' means ',
' <!-- /react-text -->',
' <em>',
' painless',
' </em>',
' <!-- react-text: 5 -->',
' Javascript testing',
' <!-- /react-text -->',
'</p>',
].join('\n'),
);
});
it('supports fragment node', () => {
const fragment = document.createDocumentFragment();
const browsers = [
'Firefox',
'Chrome',
'Opera',
'Safari',
'Internet Explorer',
];
browsers.forEach(browser => {
const li = document.createElement('li');
li.textContent = browser;
fragment.appendChild(li);
});
expect(fragment).toPrettyPrintTo(
[
'<DocumentFragment>',
' <li>',
' Firefox',
' </li>',
' <li>',
' Chrome',
' </li>',
' <li>',
' Opera',
' </li>',
' <li>',
' Safari',
' </li>',
' <li>',
' Internet Explorer',
' </li>',
'</DocumentFragment>',
].join('\n'),
);
});
it('supports custom elements', () => {
class CustomElement extends HTMLElement {}
class CustomParagraphElement extends HTMLParagraphElement {}
class CustomExtendedElement extends CustomElement {}
customElements.define('custom-element', CustomElement);
customElements.define('custom-extended-element', CustomExtendedElement);
customElements.define('custom-paragraph', CustomParagraphElement, {
extends: 'p',
});
const parent = document.createElement('div');
parent.innerHTML = [
'<custom-element></custom-element>',
'<custom-extended-element></custom-extended-element>',
'<p is="custom-paragraph"></p>',
].join('');
expect(parent).toPrettyPrintTo(
[
'<div>',
' <custom-element />',
' <custom-extended-element />',
' <p',
' is="custom-paragraph"',
' />',
'</div>',
].join('\n'),
);
});
describe('matches constructor name of SVG elements', () => {
// Too bad, so sad, element.constructor.name of SVG elements
// is HTMLUnknownElement in jsdom v9 and v10
// is Element in jsdom v11
// instead of SVG…Element in browser DOM
const expected = [
'<svg',
' viewBox="0 0 1 1"',
'>',
' <title>',
' JS community logo',
' </title>',
'</svg>',
].join('\n');
test('jsdom 9 and 10', () => {
// Mock element objects to make sure the plugin really matches them.
function SVGSVGElement(attributes, ...children) {
this.nodeType = 1;
this.tagName = 'svg'; // lower case
this.attributes = attributes;
this.childNodes = children;
}
function SVGTitleElement(title) {
this.nodeType = 1;
this.tagName = 'title'; // lower case
this.attributes = [];
this.childNodes = [document.createTextNode(title)];
}
const title = new SVGTitleElement('JS community logo');
const svg = new SVGSVGElement(
[{name: 'viewBox', value: '0 0 1 1'}],
title,
);
expect(svg).toPrettyPrintTo(expected);
});
test('jsdom 11', () => {
// Mock element objects to make sure the plugin really matches them.
function Element(tagName, attributes, ...children) {
this.nodeType = 1;
this.tagName = tagName; // lower case
this.attributes = attributes;
this.childNodes = children;
}
const title = new Element('title', [], 'JS community logo');
const svg = new Element(
'svg',
[{name: 'viewBox', value: '0 0 1 1'}],
title,
);
expect(svg).toPrettyPrintTo(expected);
});
});
it('supports SVG elements', () => {
// In jsdom v9, this is NOT a regression test. See above.
const namespace = 'http://www.w3.org/2000/svg';
const title = document.createElementNS(namespace, 'title');
title.appendChild(document.createTextNode('JS community logo'));
const rect = document.createElementNS(namespace, 'rect');
// printProps sorts attributes in order by name
rect.setAttribute('width', '1');
rect.setAttribute('height', '1');
rect.setAttribute('fill', '#f7df1e');
const polyline = document.createElementNS(namespace, 'polyline');
polyline.setAttribute('id', 'J');
polyline.setAttribute('points', '0.5,0.460 0.5,0.875 0.25,0.875');
const comment = document.createComment('polyline for S');
const g = document.createElementNS(namespace, 'g');
g.setAttribute('fill', 'none');
g.setAttribute('stroke', '#000000');
g.setAttribute('stroke-width', '0.095');
g.appendChild(polyline);
g.appendChild(comment);
const svg = document.createElementNS(namespace, 'svg');
svg.setAttribute('viewBox', '0 0 1 1');
svg.appendChild(title);
svg.appendChild(rect);
svg.appendChild(g);
const parent = document.createElement('div');
parent.setAttribute('id', 'JS');
parent.appendChild(svg);
expect(parent).toPrettyPrintTo(
[
'<div',
' id="JS"',
'>',
' <svg',
' viewBox="0 0 1 1"',
' >',
' <title>',
' JS community logo',
' </title>',
' <rect',
' fill="#f7df1e"',
' height="1"',
' width="1"',
' />',
' <g',
' fill="none"',
' stroke="#000000"',
' stroke-width="0.095"',
' >',
' <polyline',
' id="J"',
' points="0.5,0.460 0.5,0.875 0.25,0.875"',
' />',
' <!--polyline for S-->',
' </g>',
' </svg>',
'</div>',
].join('\n'),
);
});
it('supports indentation for array of elements', () => {
// For example, Array.prototype.slice.call(document.getElementsByTagName(…))
const dd1 = document.createElement('dd');
dd1.innerHTML = 'to talk in a playful manner';
const dd2 = document.createElement('dd');
dd2.innerHTML = 'painless JavaScript testing';
dd2.setAttribute('style', 'color: #99424F');
expect([dd1, dd2]).toPrettyPrintTo(
[
'Array [',
' <dd>',
' to talk in a playful manner',
' </dd>,',
' <dd',
' style="color: #99424F"',
' >',
' painless JavaScript testing',
' </dd>,',
']',
].join('\n'),
);
});
it('supports maxDepth option', () => {
const dt = document.createElement('dt');
dt.innerHTML = 'jest';
const dd1 = document.createElement('dd');
dd1.innerHTML = 'to talk in a <em>playful</em> manner';
const dd2 = document.createElement('dd');
dd2.innerHTML = '<em>painless</em> JavaScript testing';
dd2.setAttribute('style', 'color: #99424F');
const dl = document.createElement('dl');
dl.appendChild(dt);
dl.appendChild(dd1);
dl.appendChild(dd2);
expect(dl).toPrettyPrintTo(
[
'<dl>',
' <dt>',
' jest',
' </dt>',
' <dd>',
' to talk in a ',
' <em … />',
' manner',
' </dd>',
' <dd',
' style="color: #99424F"',
' >',
' <em … />',
' JavaScript testing',
' </dd>',
'</dl>',
].join('\n'),
{maxDepth: 2},
);
});
});