From 8e0e1916ddfdfbff59b78dbb7ca9fdced6c0a475 Mon Sep 17 00:00:00 2001 From: blacksun1 Date: Wed, 29 Mar 2017 03:22:30 +1030 Subject: [PATCH] Implemented usingPromise on sandbox --- lib/sinon/collection.js | 14 ++++++++- lib/sinon/sandbox.js | 7 +++++ test/sandbox-test.js | 65 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/lib/sinon/collection.js b/lib/sinon/collection.js index bf0c46de1..a41575a3d 100644 --- a/lib/sinon/collection.js +++ b/lib/sinon/collection.js @@ -71,6 +71,11 @@ var collection = { return fake; }, + addUsingPromise: function (fake) { + fake.usingPromise(this.promiseLibrary); + return fake; + }, + spy: function spy() { return this.add(sinonSpy.apply(sinonSpy, arguments)); }, @@ -85,9 +90,16 @@ var collection = { sinonStub.apply(null, arguments); if (isStubbingEntireObject) { - collectOwnMethods(stubbed).forEach(this.add.bind(this)); + var ownMethods = collectOwnMethods(stubbed); + ownMethods.forEach(this.add.bind(this)); + if (this.promiseLibrary) { + ownMethods.forEach(this.addUsingPromise.bind(this)); + } } else { this.add(stubbed); + if (this.promiseLibrary) { + stubbed.usingPromise(this.promiseLibrary); + } } return stubbed; diff --git a/lib/sinon/sandbox.js b/lib/sinon/sandbox.js index 71cb434cb..4a3d7ce30 100644 --- a/lib/sinon/sandbox.js +++ b/lib/sinon/sandbox.js @@ -90,6 +90,13 @@ extend(sinonSandbox, { return obj; }, + usingPromise: function (promiseLibrary) { + + this.promiseLibrary = promiseLibrary; + + return this; + }, + restore: function () { if (arguments.length) { throw new Error("sandbox.restore() does not take any parameters. Perhaps you meant stub.restore()"); diff --git a/test/sandbox-test.js b/test/sandbox-test.js index 89d0ad1f7..d512bd51d 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -109,6 +109,71 @@ describe("sinonSandbox", function () { }); }); + describe(".usingPromise", function () { + beforeEach(function () { + this.sandbox = Object.create(sinonSandbox); + }); + + afterEach(function () { + this.sandbox.restore(); + }); + + it("must be a function", function () { + + assert.isFunction(this.sandbox.usingPromise); + }); + + it("must return the sandbox", function () { + var mockPromise = {}; + + var actual = this.sandbox.usingPromise(mockPromise); + + assert.same(actual, this.sandbox); + }); + + it("must set all stubs created from sandbox with mockPromise", function () { + + var resolveValue = {}; + var mockPromise = { + resolve: sinonStub.create().resolves(resolveValue) + }; + + this.sandbox.usingPromise(mockPromise); + var stub = this.sandbox.stub().resolves(); + + return stub() + .then(function (action) { + + assert.same(resolveValue, action); + assert(mockPromise.resolve.calledOnce); + }); + }); + + it("must set all stubs created from sandbox with mockPromise", function () { + + var resolveValue = {}; + var mockPromise = { + resolve: sinonStub.create().resolves(resolveValue) + }; + var stubbedObject = { + stubbedMethod: function () { + return; + } + }; + + this.sandbox.usingPromise(mockPromise); + this.sandbox.stub(stubbedObject); + stubbedObject.stubbedMethod.resolves({}); + + return stubbedObject.stubbedMethod() + .then(function (action) { + + assert.same(resolveValue, action); + assert(mockPromise.resolve.calledOnce); + }); + }); + }); + // These were not run in browsers before, as we were only testing in node if (typeof window !== "undefined") { describe("fake XHR/server", function () {