Skip to content

Commit 26213ba

Browse files
Add tests for HTML to DOM parsing on the client
- Organize Mocha tests to differentiate between environments (node versus browser) - Add tests to confirm `formatDOM` (client) works as expected and returns the same output as `parseDOM` (server) - Update HTML mocks with more cases - Save `jsdomify` to devDependencies.
1 parent 954a964 commit 26213ba

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"htmlparser2": "^3.9.1"
2020
},
2121
"devDependencies": {
22+
"jsdomify": "^2.1.0",
2223
"mocha": "^3.0.2",
2324
"react": "^15.3.0",
2425
"react-dom": "^15.3.0"

test/data.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"html": {
33
"single": "<p>foo</p>",
44
"multiple": "<p>foo</p><p>bar</p>",
5-
"complex": "<html><head><title>Title</title></head><body><header id=\"header\">Header</header><h1>Heading</h1><p>Paragraph</p><div class=\"class1 class2\">Some <em>text</em>.</div><script>alert();</script></body></html>",
5+
"nested": "<div><p>foo <em>bar</em></p></div>",
6+
"attributes": "<hr id=\"foo\" class=\"bar baz\" style=\"background: #fff; text-align: center;\" data-foo=\"bar\" />",
7+
"complex": "<html><head><title>Title</title></head><body><header id=\"header\">Header</header><h1 style=\"color:#000;font-size:42px;\">Heading</h1><p>Paragraph</p><div class=\"class1 class2\">Some <em>text</em>.</div><script>alert();</script></body></html>",
68
"textarea": "<textarea>foo</textarea>",
79
"script": "<script>alert(1 < 2);</script>",
810
"comment": "<!-- comment -->"

test/html-to-dom.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,58 @@
55
*/
66
var assert = require('assert');
77
var util = require('util');
8-
var parseDOM = require('../lib/html-to-dom');
8+
var jsdomify = require('jsdomify').default;
99
var htmlparser = require('htmlparser2');
10+
var parseDOM = require('../lib/html-to-dom');
11+
var formatDOM = require('../lib/html-to-dom-client');
12+
var helpers = require('./helpers/');
1013
var data = require('./data');
1114

1215
/**
13-
* Tests for `parseDOM`.
16+
* Tests for HTML to DOM parser.
1417
*/
15-
describe('html-to-dom parser', function() {
16-
17-
it('works the same as `require("htmlparser2").parseDOM`', function() {
18-
var html = data.html.complex;
19-
// use `util.inspect` to resolve circular references
20-
assert.equal(
21-
util.inspect(parseDOM(html), { showHidden: true, depth: null }),
22-
util.inspect(htmlparser.parseDOM(html), { showHidden: true, depth: null })
23-
);
18+
describe('html-to-dom', function() {
19+
20+
/**
21+
* Node environment.
22+
*/
23+
describe('server parser', function() {
24+
25+
it('works the same as `require("htmlparser2").parseDOM`', function() {
26+
var html = data.html.complex;
27+
// use `util.inspect` to resolve circular references
28+
assert.equal(
29+
util.inspect(parseDOM(html), { showHidden: true, depth: null }),
30+
util.inspect(htmlparser.parseDOM(html), { showHidden: true, depth: null })
31+
);
32+
});
33+
34+
});
35+
36+
/**
37+
* Browser environment.
38+
*/
39+
describe('client parser', function() {
40+
41+
// setup mock browser environment
42+
before(function() {
43+
jsdomify.create();
44+
});
45+
46+
// teardown mock browser environment
47+
after(function() {
48+
jsdomify.destroy();
49+
});
50+
51+
it('formats the nodes like `htmlparser2.parseDOM`', function() {
52+
var html = data.html.attributes + data.html.nested + data.html.comment + data.html.script;
53+
var root = document.createElement('div');
54+
root.innerHTML = html;
55+
var clientNodes = formatDOM(root.childNodes);
56+
var serverNodes = parseDOM(html);
57+
helpers.deepEqualCircular(clientNodes, serverNodes);
58+
});
59+
2460
});
2561

2662
});

0 commit comments

Comments
 (0)