This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
html-to-react-tests.js
199 lines (153 loc) · 7.69 KB
/
html-to-react-tests.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
'use strict';
var assert = require("assert");
var React = require('react');
var ReactDOMServer = require('react-dom/server')
var Parser = require('../index').Parser;
var ProcessNodeDefinitions = require('../index').ProcessNodeDefinitions;
describe('Html2React', function() {
var parser = new Parser(React);
describe('parse valid HTML', function() {
it('should return a valid HTML string', function() {
var htmlInput = '<p>Does this work?</p>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
it('should return a valid HTML string with nested elements', function() {
var htmlInput = '<div><h1>Heading</h1></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
it('should return a valid HTML string with inline styles', function() {
var htmlInput = '<div style="background-color: red;color: white;"></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
it('should return a valid HTML string with empty inline styles', function() {
var htmlInput = '<div style=""></div>';
var htmlExpected = '<div></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlExpected);
});
it('should return a valid HTML string with data attributes', function() {
var htmlInput = '<div data-test-attribute="data attribute value"></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
it('should return a valid HTML string with aria attributes', function() {
var htmlInput = '<div aria-labelledby="label1"></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
it('should return a valid HTML string with a class attribute', function() {
var htmlInput = '<div class="class-one"></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
// FIXME: See lib/process-node-definitions.js -> processDefaultNode()
it.skip('should return a valid HTML string with comments', function() {
var htmlInput = '<div><!-- This is a comment --></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
});
// FIXME: If / when React implements HTML comments, this test can be removed
it('should return a valid HTML string without comments', function() {
var htmlInput = '<div><!-- This is a comment --></div>';
var htmlExpected = '<div></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlExpected);
});
});
describe('parse invalid HTML', function() {
it('should throw an error when trying parsing multiple root elements', function() {
var htmlInput = '<div></div><div></div>';
assert.throws(function() {
parser.parse(htmlInput);
}, Error);
});
it('should throw an error with a specific message when parsing multiple root elements', function() {
var htmlInput = '<div></div><div></div><div></div>';
assert.throws(function() {
parser.parse(htmlInput);
}, /contains 3 root elements/);
});
it('should fix missing closing tags', function() {
var htmlInput = '<div><p></div>';
var htmlExpected = '<div><p></p></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlExpected);
});
});
});
describe('Html2React with custom processing instructions', function() {
var parser = new Parser(React);
var processNodeDefinitions = new ProcessNodeDefinitions(React);
describe('parse valid HTML', function() {
it('should return nothing with only a single <p> element', function() {
var htmlInput = '<p>Does this work?</p>';
var isValidNode = function() {
return true;
};
var processingInstructions = [{
shouldProcessNode: function(node) {
return node.name && node.name !== 'p';
},
processNode: processNodeDefinitions.processDefaultNode
}];
var reactComponent = parser.parseWithInstructions(htmlInput, isValidNode, processingInstructions);
// With only 1 <p> element, nothing is rendered
assert.equal(reactComponent, false);
});
it('should return a single <h1> element within a div of <h1> and <p> as siblings', function() {
var htmlInput = '<div><h1>Title</h1><p>Paragraph</p></div>';
var htmlExpected = '<div><h1>Title</h1></div>';
var isValidNode = function() {
return true;
};
var processingInstructions = [{
shouldProcessNode: function(node) {
return node.type === 'text' || node.name !== 'p';
},
processNode: processNodeDefinitions.processDefaultNode
}];
var reactComponent = parser.parseWithInstructions(htmlInput, isValidNode, processingInstructions);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlExpected);
});
it('should return capitalized content for all <h1> elements', function() {
var htmlInput = '<div><h1>Title</h1><p>Paragraph</p><h1>Another title</h1></div>';
var htmlExpected = '<div><h1>TITLE</h1><p>Paragraph</p><h1>ANOTHER TITLE</h1></div>';
var isValidNode = function() {
return true;
};
var processingInstructions = [
{
// Custom <h1> processing
shouldProcessNode: function(node) {
return node.parent && node.parent.name && node.parent.name === 'h1';
},
processNode: function(node, children) {
return node.data.toUpperCase();
}
}, {
// Anything else
shouldProcessNode: function(node) {
return true;
},
processNode: processNodeDefinitions.processDefaultNode
}];
var reactComponent = parser.parseWithInstructions(htmlInput, isValidNode, processingInstructions);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlExpected);
});
});
});