From 70841d90b664082e32bebae1dbf3b8eeef49eb6e Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Tue, 8 Jul 2014 02:09:28 +0200 Subject: Add promise feature to __with__ --- test/__with__.test.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'test') diff --git a/test/__with__.test.js b/test/__with__.test.js index 532ef91..3cd4b57 100644 --- a/test/__with__.test.js +++ b/test/__with__.test.js @@ -111,4 +111,71 @@ describe("__with__", function() { expect(callWithFunction({})).to.throwError(expectTypeError); expect(callWithFunction(function(){})).to.not.throwError(expectTypeError); }); + + describe("using promises", function () { + var promiseFake; + + beforeEach(function () { + promiseFake = { + then: function (onResolve, onReject) { + promiseFake.onResolve = onResolve; + promiseFake.onReject = onReject; + } + }; + }); + + it("should pass the returned promise through", function () { + var fn = moduleFake.__with__({}); + + expect(fn(function () { + return promiseFake; + })).to.equal(promiseFake); + }); + + it("should not undo any changes until the promise has been resolved", function () { + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + + moduleFake.__with__({ + myValue: 2, + myReference: newObj + })(function () { + return promiseFake; + }); + + // the change should still be present at this point + expect(moduleFake.getValue()).to.be(2); + expect(moduleFake.getReference()).to.be(newObj); + + promiseFake.onResolve(); + + // now everything should be back to normal + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + }); + + it("should also undo any changes if the promise has been rejected", function () { + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + + moduleFake.__with__({ + myValue: 2, + myReference: newObj + })(function () { + return promiseFake; + }); + + // the change should still be present at this point + expect(moduleFake.getValue()).to.be(2); + expect(moduleFake.getReference()).to.be(newObj); + + promiseFake.onReject(); + + // now everything should be back to normal + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + }); + + }); + }); -- cgit v1.2.3