diff options
author | Johannes Ewald <johannes.ewald@peerigon.com> | 2014-07-08 03:30:48 +0400 |
---|---|---|
committer | Johannes Ewald <johannes.ewald@peerigon.com> | 2014-07-08 03:30:48 +0400 |
commit | 3f7b23aec53d97bed6489f3be9ad3954bc629b7d (patch) | |
tree | a5e3d68c8a595c03a14c406130f37bd6d892bb36 /test | |
parent | 71733c8dad414c08efd57821b896bfc1d6868e22 (diff) |
Refactor tests
Diffstat (limited to 'test')
-rw-r--r-- | test/__set__.test.js | 9 | ||||
-rw-r--r-- | test/__with__.test.js | 61 |
2 files changed, 38 insertions, 32 deletions
diff --git a/test/__set__.test.js b/test/__set__.test.js index c883c27..ef69a07 100644 --- a/test/__set__.test.js +++ b/test/__set__.test.js @@ -2,7 +2,6 @@ var expect = require("expect.js"), __set__ = require("../lib/__set__.js"), vm = require("vm"), - expectReferenceError = expectError(ReferenceError), expectTypeError = expectError(TypeError); function expectError(ErrConstructor) { @@ -12,7 +11,8 @@ function expectError(ErrConstructor) { } describe("__set__", function () { - var moduleFake; + var moduleFake, + undo; beforeEach(function () { moduleFake = { @@ -24,6 +24,7 @@ describe("__set__", function () { }; vm.runInNewContext( + //__set__ requires __set__ to be present on module.exports "__set__ = module.exports.__set__ = " + __set__.toString() + "; " + "getValue = function () { return myValue; }; " + "getReference = function () { return myReference; }; ", @@ -74,7 +75,7 @@ describe("__set__", function () { }); it("should return a function that when invoked reverts to the values before set was called", function () { undo = moduleFake.__set__("myValue", 4); - expect(typeof undo).to.be("function"); + expect(undo).to.be.a("function"); expect(moduleFake.getValue()).to.be(4); undo(); expect(moduleFake.getValue()).to.be(0); @@ -85,7 +86,7 @@ describe("__set__", function () { expect(moduleFake.getValue()).to.be(0); expect(moduleFake.getReference()).to.eql({}); - var undo = moduleFake.__set__({ + undo = moduleFake.__set__({ myValue: 2, myReference: newObj }); diff --git a/test/__with__.test.js b/test/__with__.test.js index 247838a..7ce86b5 100644 --- a/test/__with__.test.js +++ b/test/__with__.test.js @@ -1,9 +1,7 @@ var expect = require("expect.js"), __with__ = require("../lib/__with__.js"), __set__ = require("../lib/__set__.js"), - vm = require("vm"), - expectReferenceError = expectError(ReferenceError), - expectTypeError = expectError(TypeError); + vm = require("vm"); function expectError(ErrConstructor) { return function expectReferenceError(err) { @@ -12,7 +10,8 @@ function expectError(ErrConstructor) { } describe("__with__", function() { - var moduleFake; + var moduleFake, + newObj; beforeEach(function () { moduleFake = { @@ -23,8 +22,10 @@ describe("__with__", function() { myReference: {} // copy by reference }; - //__with__ requires __set__ to be in scope + newObj = { hello: "hello" }; + vm.runInNewContext( + //__with__ requires __set__ to be present on module.exports "module.exports.__set__ = " + __set__.toString() + "; " + "__with__ = " + __with__.toString() + "; " + "getValue = function () { return myValue; }; " + @@ -33,53 +34,57 @@ describe("__with__", function() { ); }); - 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 () { - var newObj = { hello: "hello" }; + it("should return a function", function () { + expect(moduleFake.__with__({ + myValue: 2, + myReference: newObj + })).to.be.a("function"); + }); + 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 () { expect(moduleFake.getValue()).to.be(0); expect(moduleFake.getReference()).to.eql({}); moduleFake.__with__({ myValue: 2, myReference: newObj - })(function() { - //changes will be visible from within this callback function - expect(moduleFake.getValue()).to.be(2); - expect(moduleFake.getReference()).to.be(newObj); + })(function () { + // changes will be visible from within this callback function + expect(moduleFake.getValue()).to.be(2); + expect(moduleFake.getReference()).to.be(newObj); }); - //undo will automatically get called for you after returning from your callback function + // undo will automatically get called for you after returning from your callback function expect(moduleFake.getValue()).to.be(0); expect(moduleFake.getReference()).to.eql({}); }); it("should still revert values if the callback throws an exception", function(){ - var newObj = { hello: "hello" }; - function withError(){ - moduleFake.__with__({ - myValue: 2, - myReference: newObj - })(function() { - throw new Error("something went wrong..."); - }); - } - expect(withError).to.throwError(); + expect(function withError() { + moduleFake.__with__({ + myValue: 2, + myReference: newObj + })(function () { + throw new Error("something went wrong..."); + }); + }).to.throwError(); expect(moduleFake.getValue()).to.be(0); expect(moduleFake.getReference()).to.eql({}); }); it("should throw an error if something other than a function is passed as the callback", function() { - var newObj = { hello: "hello" }, - withFunction = moduleFake.__with__({ + var withFunction = moduleFake.__with__({ myValue: 2, myReference: newObj }); - callWithFunction = function(){ - var args = arguments; - return function() { + + function callWithFunction() { + var args = arguments; + + return function () { withFunction.apply(null, args); - }; }; + } expect(callWithFunction(1)).to.throwError(); expect(callWithFunction("a string")).to.throwError(); |