Skip to content

Commit

Permalink
test(helpers): move helper that tests for errors to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Nov 2, 2019
1 parent 6d6125e commit f2e6312
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
34 changes: 34 additions & 0 deletions test/helpers/throws-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var values = [
undefined,
null,
0,
1,
true,
false,
{},
[],
function() {},
new Date()
];

/**
* Calls parser with invalid arguments.
*
* @param {Function} parser - The parser.
* @param {Function} [assert] - The assertion module.
*/
function throwsError(parser, assert) {
if (typeof assert !== 'function') {
assert = require('assert');
}

values.forEach(function(value) {
it('throws error for argument: ' + value, function() {
assert.throws(function() {
parser(value);
}, TypeError);
});
});
}

module.exports = throwsError;
38 changes: 9 additions & 29 deletions test/html-to-dom-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { assert } = require('chai');
const cases = require('./cases');
const htmlparser = require('htmlparser2');
const cases = require('./cases');
const throwsError = require('./helpers/throws-error');
const { CASE_SENSITIVE_TAG_NAMES } = require('../lib/constants');

/**
Expand All @@ -18,22 +19,6 @@ function runTests(parser, cases) {
});
}

/**
* Throws tests (a helper that runs tests and verifies that error is thrown).
*
* @param {Function} parser - The parser.
*/
function throwTests(parser) {
const values = [undefined, null, 1, true, {}, ['Array'], Function, Date];
values.forEach(value => {
it(`throws when argument is ${value}`, () => {
assert.throws(() => {
parser(value);
}, TypeError);
});
});
}

/**
* Tests case-sensitive tags (SVG) to make sure their case is preserved.
*
Expand All @@ -48,33 +33,28 @@ function testCaseSensitiveTags(parser) {
});
}

/**
* Tests for parser.
*/
describe('server parser', () => {
// before
const parser = require('../');

// check if invalid parameter type throws error
throwTests(parser);

// should be equivalent to `htmlparser2.parseDOM()`
// tests
throwsError(parser);
runTests(parser, cases.html);
runTests(parser, cases.svg);
});

describe('client parser in jsdom', () => {
// before
const jsdomify = require('jsdomify').default;
jsdomify.create();
const parser = require('../lib/html-to-dom-client');

// check if invalid parameter type throws error
throwTests(parser);

// should return the same output as `htmlparser2.parseDOM()`
// tests
throwsError(parser);
runTests(parser, cases.html);
runTests(parser, cases.svg);

testCaseSensitiveTags(parser);

// after
jsdomify.destroy();
});

0 comments on commit f2e6312

Please sign in to comment.