summaryrefslogtreecommitdiffstats
path: root/testing/setup.js
blob: 11050027766fab24f67ab5dd57882e408ace6d1f (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
var is = require('is');
var path = require('path');
var fs = require('fs');
var expect = require('expect');
var cheerio = require('cheerio');

expect.extend({
    /**
     * Check that a file is created in a directory:
     * expect('myFolder').toHaveFile('hello.md');
     */
    toHaveFile: function(fileName) {
        var filePath = path.join(this.actual, fileName);
        var exists = fs.existsSync(filePath);

        expect.assert(
            exists,
            'expected %s to have file %s',
            this.actual,
            fileName
        );
        return this;
    },
    toNotHaveFile: function(fileName) {
        var filePath = path.join(this.actual, fileName);
        var exists = fs.existsSync(filePath);

        expect.assert(
            !exists,
            'expected %s to not have file %s',
            this.actual,
            fileName
        );
        return this;
    },

    /**
     * Check that a value is defined (not null nor undefined)
     */
    toBeDefined: function() {
        expect.assert(
            !(is.undefined(this.actual) || is.null(this.actual)),
            'expected to be defined'
        );
        return this;
    },

    /**
     * Check that a value is defined (not null nor undefined)
     */
    toNotBeDefined: function() {
        expect.assert(
            (is.undefined(this.actual) || is.null(this.actual)),
            'expected %s to be not defined',
            this.actual
        );
        return this;
    },

    /**
     * Check that a dom element exists in HTML
     * @param {String} selector
     */
    toHaveDOMElement: function(selector) {
        var $ = cheerio.load(this.actual);
        var $el = $(selector);

        expect.assert($el.length > 0, 'expected HTML to contains %s', selector);
    }
});

global.expect = expect;