Skip to content

Commit 9e22732

Browse files
Add tests for HTML to React
In the tests, use the parser to convert HTML string to React elements and then use ReactDOMServer to render it back to HTML string. Add `react-dom` to devDependencies.
1 parent a1c8f2d commit 9e22732

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
},
2121
"devDependencies": {
2222
"mocha": "^3.0.2",
23-
"react": "^15.3.0"
23+
"react": "^15.3.0",
24+
"react-dom": "^15.3.0"
2425
},
2526
"peerDependencies": {
2627
"react": "^0.13 || ^15.0"

test/html-to-react.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var assert = require('assert');
7+
var React = require('react');
8+
var ReactDOMServer = require('react-dom/server');
9+
var Parser = require('../');
10+
var data = require('./data');
11+
12+
/**
13+
* Render a React element to static HTML markup.
14+
*
15+
* @param {ReactElement} reactElement - The React element.
16+
* @return {String} - The static HTML markup.
17+
*/
18+
function render(reactElement) {
19+
return ReactDOMServer.renderToStaticMarkup(reactElement);
20+
}
21+
22+
/**
23+
* Tests for `htmlToReact`.
24+
*/
25+
describe('html-to-react parser', function() {
26+
27+
it('converts single HTML element to React', function() {
28+
var html = data.html.single;
29+
var reactElement = Parser(html);
30+
assert.equal(render(reactElement), html);
31+
});
32+
33+
it('converts multiple HTML elements to React', function() {
34+
var html = data.html.multiple;
35+
var reactElements = Parser(html);
36+
assert.equal(
37+
render(React.createElement('div', {}, reactElements)),
38+
'<div>' + html + '</div>'
39+
);
40+
});
41+
42+
it('converts complex HTML to React', function() {
43+
var html = data.html.complex;
44+
var reactElement = Parser(html);
45+
assert.equal(render(reactElement), html);
46+
});
47+
48+
});

0 commit comments

Comments
 (0)