Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d044f04

Browse files
committed
Improve __with__ tests
1 parent 3f7b23a commit d044f04

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

test/__with__.test.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var expect = require("expect.js"),
22
__with__ = require("../lib/__with__.js"),
33
__set__ = require("../lib/__set__.js"),
4-
vm = require("vm");
4+
vm = require("vm"),
5+
6+
expectTypeError = expectError(TypeError);
57

68
function expectError(ErrConstructor) {
79
return function expectReferenceError(err) {
@@ -41,7 +43,7 @@ describe("__with__", function() {
4143
})).to.be.a("function");
4244
});
4345

44-
it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end", function () {
46+
it("should return a function that can be invoked with a callback which guarantees __set__'s undo function is called for you at the end", function () {
4547
expect(moduleFake.getValue()).to.be(0);
4648
expect(moduleFake.getReference()).to.eql({});
4749

@@ -59,6 +61,24 @@ describe("__with__", function() {
5961
expect(moduleFake.getReference()).to.eql({});
6062
});
6163

64+
it("should also accept a variable name and a variable value (just like __set__)", function () {
65+
expect(moduleFake.getValue()).to.be(0);
66+
67+
moduleFake.__with__("myValue", 2)(function () {
68+
expect(moduleFake.getValue()).to.be(2);
69+
});
70+
71+
expect(moduleFake.getValue()).to.be(0);
72+
73+
expect(moduleFake.getReference()).to.eql({});
74+
75+
moduleFake.__with__("myReference", newObj)(function () {
76+
expect(moduleFake.getReference()).to.be(newObj);
77+
});
78+
79+
expect(moduleFake.getReference()).to.eql({});
80+
});
81+
6282
it("should still revert values if the callback throws an exception", function(){
6383
expect(function withError() {
6484
moduleFake.__with__({
@@ -86,9 +106,9 @@ describe("__with__", function() {
86106
};
87107
}
88108

89-
expect(callWithFunction(1)).to.throwError();
90-
expect(callWithFunction("a string")).to.throwError();
91-
expect(callWithFunction({})).to.throwError();
92-
expect(callWithFunction(function(){})).to.not.throwError();
109+
expect(callWithFunction(1)).to.throwError(expectTypeError);
110+
expect(callWithFunction("a string")).to.throwError(expectTypeError);
111+
expect(callWithFunction({})).to.throwError(expectTypeError);
112+
expect(callWithFunction(function(){})).to.not.throwError(expectTypeError);
93113
});
94114
});

0 commit comments

Comments
 (0)