diff options
author | Johannes Ewald <johannes.ewald@peerigon.com> | 2014-07-08 04:09:28 +0400 |
---|---|---|
committer | Johannes Ewald <johannes.ewald@peerigon.com> | 2014-07-08 04:09:28 +0400 |
commit | 70841d90b664082e32bebae1dbf3b8eeef49eb6e (patch) | |
tree | b5a83dc849ed21ce42d056e1037e173761124515 /test | |
parent | d044f04e4c6a5e9bff37c384a2256d771aa0e5bc (diff) |
Add promise feature to __with__
Diffstat (limited to 'test')
-rw-r--r-- | test/__with__.test.js | 67 |
1 files changed, 67 insertions, 0 deletions
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({}); + }); + + }); + }); |