Skip to content

Commit

Permalink
test(server): add tests that spy and mock htmlparser2 and domhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Nov 3, 2019
1 parent 3684dac commit 61075a1
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { expect } = require('chai');
const sinon = require('sinon');
const mock = require('mock-require');

let parser;
let ParserSpy;
let parserEndSpy;
let DomHandlerSpy;

const html = '<html>';
const dom = [
{
type: 'tag',
name: 'html',
attribs: {},
children: [],
next: null,
prev: null,
parent: null
}
];

function clearRequireCache() {
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key];
});
}

describe('server parser', () => {
// before
parserEndSpy = sinon.spy();
ParserSpy = sinon.spy(function() {
this.end = parserEndSpy;
});
DomHandlerSpy = sinon.spy(function() {
this.dom = dom;
});

// tests
mock('htmlparser2/lib/Parser', ParserSpy);
mock('domhandler', DomHandlerSpy);
parser = require('../..');

it('calls `domhandler` and `htmlparser2/lib/Parser`', () => {
parser(html);
expect(DomHandlerSpy.called).to.equal(true);
expect(ParserSpy.called).to.equal(true);
expect(parserEndSpy.called).to.equal(true);
});

it.skip('uses the cached instance of `domhandler` if options is undefined', () => {
expect(DomHandlerSpy.callCount).to.equal(1);
expect(ParserSpy.callCount).to.equal(1);
parser(html);
expect(DomHandlerSpy.callCount).to.equal(1);
expect(ParserSpy.callCount).to.equal(2);
});

it('passes options to `domhandler` and arguments to `htmlparser2/lib/Parser`', () => {
const options = { decodeEntities: true };
parser(html, options);
expect(DomHandlerSpy.calledWith(options)).to.equal(true);
expect(ParserSpy.calledWith(DomHandlerSpy, options));
});

it('passes html to `htmlparser2/lib/Parser` end', () => {
parser(html);
expect(parserEndSpy.calledWith(html)).to.equal(true);
});

it('returns `domhandler` dom', () => {
expect(parser(html)).to.equal(DomHandlerSpy.lastCall.returnValue.dom);
});

// after
mock.stopAll();
clearRequireCache();
});

0 comments on commit 61075a1

Please sign in to comment.