summaryrefslogtreecommitdiffstats
path: root/test/assertions.js
blob: f96fdc1d694baa4c99b68bcf7e314b7ec78f2a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var cheerio = require('cheerio');
var should = require('should');

// Assertions to test if an Output has generated a file
should.Assertion.add('file', function(file, description) {
    var rootFolder;
    if (_.isFunction(this.obj.root)) {
        rootFolder = this.obj.root();
    } else {
        rootFolder = this.obj;
    }

    this.params = {
        actual: rootFolder,
        operator: 'have file ' + file,
        message: description
    };

    if (_.isFunction(this.obj.resolve)) {
        file = this.obj.resolve(file);
    } else {
        file = path.resolve(rootFolder, file);
    }

    this.assert(fs.existsSync(file));
});

should.Assertion.add('html', function(rules, description) {
    this.params = { actual: 'HTML string', operator: 'valid html', message: description };
    var $ = cheerio.load(this.obj);

    _.each(rules, function(validations, query) {
        validations = _.defaults(validations || {}, {
            // Select a specific element in the list of matched elements
            index: null,

            // Check that there is the correct count of elements
            count: 1,

            // Check attribute values
            attributes: {},

            // Trim inner text
            trim: false,

            // Check inner text
            text: undefined
        });

        var $el = $(query);

        // Select correct element
        if (_.isNumber(validations.index)) $el = $($el.get(validations.index));

        // Test number of elements
        $el.length.should.be.equal(validations.count);

        // Test text
        if (validations.text !== undefined) {
            var text = $el.text();
            if (validations.trim) text = text.trim();
            text.should.be.equal(validations.text);
        }

        // Test attributes
        _.each(validations.attributes, function(value, name) {
            var attr = $el.attr(name);
            should(attr).be.ok();
            attr.should.be.equal(value);
        });
    });
});