Welcome to mirror list, hosted at ThFree Co, Russian Federation.

__set__.test.js « test - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 85a1d37b510ba6101a7022267ebc126ee26ce497 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var expect = require("expect.js"),
    __set__ = require("../lib/__set__.js"),
    vm = require("vm"),

    expectReferenceError = expectError(ReferenceError),
    expectTypeError = expectError(TypeError);

function expectError(ErrConstructor) {
    return function expectReferenceError(err) {
        expect(err.constructor.name === ErrConstructor.name).to.be(true);
    };
}

describe("__set__", function () {
    var moduleFake;

    beforeEach(function () {
        moduleFake = {
            myNumber: 0,    // copy by value
            myObj: {},       // copy by reference

            // these variables are used within the set method
            // because there is a eval() statement within the set method
            // these variables should not override same-named vars of the module
            key: "key",
            env: "env",
            src: "src"
        };

        vm.runInNewContext(
            "__set__ = " + __set__.toString() + "; " +
            "getNumber = function () { return myNumber; }; " +
            "getObj = function () { return myObj; }; ",
            moduleFake
        );
    });
    it("should set the new number when calling with varName, varValue", function () {
        expect(moduleFake.getNumber() === 0).to.be(true);
        moduleFake.__set__("myNumber", 2);
        expect(moduleFake.getNumber() === 2).to.be(true);
    });
    it("should set the new object when calling with varName, varValue", function () {
        var newObj = { hello: "hello" };

        expect(moduleFake.getObj()).to.eql({});
        moduleFake.__set__("myObj", newObj);
        expect(moduleFake.getObj() === newObj).to.be(true);
    });
    it("should set the new number and the new obj when calling with an env-obj", function () {
        var newObj = { hello: "hello" };

        expect(moduleFake.getNumber() === 0).to.be(true);
        expect(moduleFake.getObj()).to.eql({});
        moduleFake.__set__({
            myNumber: 2,
            myObj: newObj
        });
        expect(moduleFake.getNumber() === 2).to.be(true);
        expect(moduleFake.getObj() === newObj).to.be(true);
    });
    it("should return undefined", function () {
        expect(moduleFake.__set__("myNumber", 4) === undefined).to.be(true);
    });
    it("should throw a ReferenceError when trying to set non-existing vars", function () {
        expect(function () {
            moduleFake.__set__("notExisting", 3);
        }).to.throwException();
        expect(function () {
            moduleFake.__set__({
                notExisting: "bla",
                notExistingAsWell: "blabla"
            });
        }).to.throwException(expectReferenceError);
    });
    it("should throw a TypeError when passing misfitting params", function () {
        expect(function () {
            moduleFake.__set__();
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__(undefined);
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__(null);
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__(true);
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__(2);
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__("");
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__(function () {});
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__({}, true);   // misfitting number of params
        }).to.throwException(expectTypeError);
        expect(function () {
            moduleFake.__set__("someVar");  // misfitting number of params
        }).to.throwException(expectTypeError);
    });
});