Skip to content

Commit 457f258

Browse files
Create test for HTML to DOM parser
- Verify that recreated helper works exactly like htmlparser2's `parseDOM` - Create mock data (HTML) - Use `util.inspect` in tests to handle circular references: https://nodejs.org/api/util.html#util_util_inspect_object_options - Add npm test script - Add `mocha` to devDependencies.
1 parent e6adbf5 commit 457f258

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "0.0.1",
44
"description": "Convert HTML string to React elements.",
55
"author": "Mark <mark@remarkablemark.org>",
6+
"scripts": {
7+
"test": "mocha"
8+
},
69
"keywords": [
710
"html",
811
"react",
@@ -14,5 +17,8 @@
1417
"domhandler": "^2.3.0",
1518
"htmlparser2": "^3.9.1"
1619
},
20+
"devDependencies": {
21+
"mocha": "^3.0.2"
22+
},
1723
"license": "MIT"
1824
}

test/data.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"html": {
3+
"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>"
4+
}
5+
}

test/html-to-dom.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var assert = require('assert');
7+
var util = require('util');
8+
var parseDOM = require('../lib/html-to-dom');
9+
var htmlparser = require('htmlparser2');
10+
var data = require('./data');
11+
12+
/**
13+
* Tests for `parseDOM`.
14+
*/
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+
);
24+
});
25+
26+
});

0 commit comments

Comments
 (0)