forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_element.test.js
173 lines (143 loc) Β· 4.69 KB
/
html_element.test.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
/**
* 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 element with text content', () => {
const parent = document.createElement('div');
parent.innerHTML = 'texty texty';
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 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('trims unnecessary whitespace', () => {
const parent = document.createElement('div');
parent.innerHTML = `
<span>
some
apple
pseudo-multilne text
</span>
<span>text</span>
`;
expect(parent).toPrettyPrintTo(
[
'<div>',
' <span>',
' some apple pseudo-multilne text',
' </span>',
' <span>',
' text',
' </span>',
'</div>',
].join('\n'),
);
});
it('supports text node', () => {
const parent = document.createElement('div');
parent.innerHTML = 'some <span>text</span>';
// prettier-ignore
expect(parent).toPrettyPrintTo([
'<div>',
' some ',
' <span>',
' text',
' </span>',
'</div>',
].join('\n'));
});
it('supports comment node', () => {
const parent = document.createElement('div');
parent.innerHTML = 'some <!-- comments -->';
// prettier-ignore
expect(parent).toPrettyPrintTo([
'<div>',
' some ',
' <!-- comments -->',
'</div>',
].join('\n'));
});
});