Skip to content

Commit

Permalink
Implemented usingPromise on sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
blacksun1 committed Mar 28, 2017
1 parent 8cc8092 commit 8e0e191
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/sinon/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions lib/sinon/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
Expand Down
65 changes: 65 additions & 0 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 8e0e191

Please sign in to comment.