-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathintegration.spec.js
More file actions
202 lines (170 loc) · 5.5 KB
/
Copy pathintegration.spec.js
File metadata and controls
202 lines (170 loc) · 5.5 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
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import HtmlParser, { convertNodeToElement, htmlparser2 } from 'index';
const reactVersion = parseInt(require('react/package.json').version.match(/^(\d+)\./)[1], 10);
class HtmlParserComponent extends React.Component {
render() {
return <div>{HtmlParser(this.props.html, this.props.options)}</div>;
}
}
const test = function(html, override=null, options={}) {
const actual = ReactDOMServer.renderToStaticMarkup(<HtmlParserComponent html={html} options={options} />);
const expected = `<div>${override === null && html || override}</div>`;
expect(actual).toBe(expected);
};
describe('Integration tests: ', () => {
it('should render a simple element', () => {
test('<div>test</div>');
});
it('should render multiple sibling elements', () => {
test('<div>test1</div><span>test2</span><footer>test3</footer>');
});
it('should render nested elements', () => {
test('<div><span>test1</span><div><ul><li>test2</li><li>test3</li></ul></div></div>');
});
it('should handle bad html', () => {
test(
'<div class=test>test<ul><li>test1<li>test2</ul><span>test</span></div>',
'<div class="test">test<ul><li>test1</li><li>test2</li></ul><span>test</span></div>'
);
});
it('should ignore doctypes', () => {
test(
'<!doctype html><div>test</div>',
'<div>test</div>'
);
});
it('should ignore comments', () => {
test('<div>test1</div><!-- comment --><div>test2</div>', '<div>test1</div><div>test2</div>');
});
it('should ignore script tags', () => {
test('<script>alert(1)</script>', '');
});
it('should ignore event handlers', () => {
test('<a href="#" onclick="alert(1)">test</a>', '<a href="#">test</a>');
});
it('should handle attributes', () => {
test('<div class="test" id="test" aria-test="test" data-test="test">test</div>');
});
it('should handle inline styles', () => {
// react 16 drops trailing semi commas from inline styles so we have to test for both
const trailingSemiComma = reactVersion === 15 ? ';' : '';
test(`<div style="border-radius:1px;background:red${trailingSemiComma}">test</div>`);
});
it('should ignore inline styles that are empty strings', () => {
test('<div style="">test</div>', '<div>test</div>');
});
it('should not allow nesting of void elements', () => {
test('<img><p>test</p></img>', '<img/><p>test</p>');
});
it('should convert boolean attribute values', () => {
test('<input disabled>', '<input disabled=""/>');
test('<input disabled="">', '<input disabled=""/>');
test('<input disabled="disabled">', '<input disabled=""/>');
});
it('should decode html entities by default', () => {
test('<span>!</span>', '<span>!</span>');
});
it('should not decode html entities when the option is disabled', () => {
test(
'<span>!</span>',
'<span>&excl;</span>',
{
decodeEntities: false
}
);
});
describe('transform function', () => {
it('should use the response when it is not undefined', () => {
test(
'<span>test</span><div>another</div>',
'<p>transformed</p><p>transformed</p>',
{
transform(node, index) {
return <p key={index}>transformed</p>;
}
}
);
});
it('should not render elements and children when returning null', () => {
test(
'<p>test<span>inner test<b>bold child</b></span></p>',
'<p>test</p>',
{
transform(node) {
if (node.type === 'tag' && node.name === 'span') {
return null;
}
}
}
);
});
it('should allow modifying nodes', () => {
test(
'<a href="/test">test link</a>',
'<a href="/changed">test link</a>',
{
transform(node, index) {
node.attribs.href = '/changed';
return convertNodeToElement(node, index);
}
}
);
});
it('should allow passing the transform function down to children', () => {
const transform = function(node, index) {
if (node.type === 'tag') {
if (node.name === 'ul') {
node.attribs.class = 'test';
return convertNodeToElement(node, index, transform);
}
}
if (node.type === 'text') {
return node.data.replace(/list/, 'changed');
}
};
test(
'<ul><li>list 1</li><li>list 2</li></ul>',
'<ul class="test"><li>changed 1</li><li>changed 2</li></ul>',
{
transform
}
);
});
});
it('should not render invalid tags', () => {
test(
'<div>test<test</div>',
'<div>test</div>'
);
});
it('should not render invalid attributes', () => {
test(
'<div test<="test" class="test">content</div>',
'<div class="test">content</div>'
);
});
it('should preprocess nodes correctly', () => {
test(
'<div>preprocess test</div>',
'<div>preprocess test</div><div>preprocess test</div>',
{
preprocessNodes(nodes) {
return [
...nodes,
...nodes
];
}
}
);
});
it('should expose htmlparser2', () => {
expect(htmlparser2).toBeDefined();
});
it('should preserve whitespace with new lines between nodes', () => {
test(
'<strong>there should be</strong>\n <strong>a space</strong>',
'<strong>there should be</strong>\n <strong>a space</strong>'
);
});
});