Skip to content

Commit 8e30784

Browse files
test(helpers): refactor and move runTests to its own file
1 parent f2e6312 commit 8e30784

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

test/helpers/run-tests.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Runs tests.
3+
*
4+
* @param {Object} testCases - The test cases.
5+
* @param {Function} expectedParser - The expected parser.
6+
* @param {Function} actualParser - The actual parser.
7+
* @param {Function} [assert] - The assertion module.
8+
*/
9+
function runTests(testCases, expectedParser, actualParser, assert) {
10+
if (typeof assert !== 'function') {
11+
assert = require('assert');
12+
}
13+
14+
if (typeof expectedParser !== 'function') {
15+
throw new TypeError('Missing or invalid expected parser');
16+
}
17+
18+
if (typeof actualParser !== 'function') {
19+
throw new TypeError('Missing or invalid actual parser');
20+
}
21+
22+
Object.keys(testCases).forEach(function(type) {
23+
var testCase = testCases[type];
24+
25+
it(type, function() {
26+
assert.deepEqual(expectedParser(testCase), actualParser(testCase));
27+
});
28+
});
29+
}
30+
31+
module.exports = runTests;

test/html-to-dom-server.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
const { assert } = require('chai');
22
const htmlparser = require('htmlparser2');
33
const cases = require('./cases');
4+
const runTests = require('./helpers/run-tests');
45
const throwsError = require('./helpers/throws-error');
56
const { CASE_SENSITIVE_TAG_NAMES } = require('../lib/constants');
67

7-
/**
8-
* Runs test cases.
9-
*
10-
* @param {Function} parser - The parser.
11-
* @param {Object} cases - The test cases.
12-
*/
13-
function runTests(parser, cases) {
14-
Object.keys(cases).forEach(type => {
15-
it(type, () => {
16-
const data = cases[type];
17-
assert.deepEqual(parser(data), htmlparser.parseDOM(data));
18-
});
19-
});
20-
}
21-
228
/**
239
* Tests case-sensitive tags (SVG) to make sure their case is preserved.
2410
*
@@ -39,8 +25,8 @@ describe('server parser', () => {
3925

4026
// tests
4127
throwsError(parser);
42-
runTests(parser, cases.html);
43-
runTests(parser, cases.svg);
28+
runTests(cases.html, parser, htmlparser.parseDOM);
29+
runTests(cases.svg, parser, htmlparser.parseDOM);
4430
});
4531

4632
describe('client parser in jsdom', () => {
@@ -51,8 +37,8 @@ describe('client parser in jsdom', () => {
5137

5238
// tests
5339
throwsError(parser);
54-
runTests(parser, cases.html);
55-
runTests(parser, cases.svg);
40+
runTests(cases.html, parser, htmlparser.parseDOM);
41+
runTests(cases.svg, parser, htmlparser.parseDOM);
5642
testCaseSensitiveTags(parser);
5743

5844
// after

0 commit comments

Comments
 (0)