'use strict';

/**
 * Module dependencies.
 */
var assert = require('chai').assert;
var mocks = require('./mocks/');
var htmlparser = require('htmlparser2');

/**
 * Tests for parser.
 */
describe('html-dom-parser', function() {

    describe('server parser', function() {
        var parser = require('../');

        it('is equivalent to `htmlparser2.parseDOM()`', function() {
            // html
            Object.keys(mocks.html).forEach(function(type) {
                var html = mocks.html[type];
                assert.deepEqual(parser(html), htmlparser.parseDOM(html));
            });

            // svg
            Object.keys(mocks.svg).forEach(function(type) {
                var svg = mocks.svg[type];
                assert.deepEqual(parser(svg), htmlparser.parseDOM(svg));
            });
        });
    });

});

'use strict';

/**
 * Module dependencies.
 */
var assert = require('chai').assert;
var mocks = require('./mocks/');
var htmlparser = require('htmlparser2');

/**
 * Helper that creates and runs tests based on mock data.
 *
 * @param {Function} parser  - The parser.
 * @param {Object}   mockObj - The mock object.
 */
function runTests(parser, mockObj) {
    Object.keys(mockObj).forEach(function(type) {
        it(type, function() {
            var data = mockObj[type];
            assert.deepEqual(parser(data), htmlparser.parseDOM(data));
        })
    });
}

/**
 * Tests for parser.
 */
describe('html-dom-parser', function() {

    // server
    describe('server parser', function() {
        var parser = require('../');

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

});

Refactor tests with `runTests()` helper
Refactor tests with `runTests()` helper

`runTests()` will create and run tests given parser and mock data.

Since the helper creates an `it()` block for each mock object key,
this allows the tests to be DRY and clear. Also, debugging will be
easier since the type of parser test being run is made explicit.
