forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_element.test.js
More file actions
368 lines (319 loc) · 9.82 KB
/
html_element.test.js
File metadata and controls
368 lines (319 loc) · 9.82 KB
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
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @jest-environment jsdom
* @flow
*/
/* eslint-env browser*/
'use strict';
const prettyFormat = require('../');
const {HTMLElement} = prettyFormat.plugins;
const toPrettyPrintTo = require('./expect_util').getPrettyPrint([HTMLElement]);
const expect: any = global.expect;
expect.extend({toPrettyPrintTo});
describe('HTMLElement 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 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},
);
});
});