Skip to content

Commit

Permalink
[feature] adds spy.calledImmediatelyBefore and spy.calledImmediatelyA…
Browse files Browse the repository at this point in the history
…fter
  • Loading branch information
Tim Costa committed Mar 17, 2017
1 parent e401337 commit 3aa9919
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/sinon/call.js
Expand Up @@ -88,6 +88,14 @@ var callProto = {
return this.callId > other.callId;
},

calledImmediatelyBefore: function (other) {
return this.callId === other.callId - 1;
},

calledImmediatelyAfter: function (other) {
return this.callId === other.callId + 1;
},

callArg: function (pos) {
this.args[pos]();
},
Expand Down
16 changes: 16 additions & 0 deletions lib/sinon/spy.js
Expand Up @@ -267,6 +267,22 @@ var spyApi = {
return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
},

calledImmediatelyBefore: function calledImmediatelyBefore(spyFn) {
if (!this.called || !spyFn.called) {
return false;
}

return this.callIds[this.callCount - 1] === spyFn.callIds[spyFn.callCount - 1] - 1;
},

calledImmediatelyAfter: function calledImmediatelyAfter(spyFn) {
if (!this.called || !spyFn.called) {
return false;
}

return this.callIds[this.callCount - 1] === spyFn.callIds[spyFn.callCount - 1] + 1;
},

withArgs: function () {
var args = slice.call(arguments);

Expand Down
108 changes: 108 additions & 0 deletions test/spy-test.js
Expand Up @@ -1650,6 +1650,114 @@ describe("spy", function () {
});
});

describe(".calledImmediatelyAfter", function () {
beforeEach(function () {
this.spy1 = createSpy();
this.spy2 = createSpy();
this.spy3 = createSpy();
});

it("is function", function () {
assert.isFunction(this.spy1.calledImmediatelyAfter);
});

it("returns true if first call to A was immediately after first to B", function () {
this.spy2();
this.spy1();

assert(this.spy1.calledImmediatelyAfter(this.spy2));
});

it("compares calls directly", function () {
this.spy2();
this.spy1();

assert(this.spy1.getCall(0).calledImmediatelyAfter(this.spy2.getCall(0)));
});

it("returns false if not called", function () {
this.spy2();

assert.isFalse(this.spy1.calledImmediatelyAfter(this.spy2));
});

it("returns false if other not called", function () {
this.spy1();

assert.isFalse(this.spy1.calledImmediatelyAfter(this.spy2));
});

it("returns false if other called last", function () {
this.spy2();
this.spy1();
this.spy2();

assert.isFalse(this.spy1.calledImmediatelyAfter(this.spy2));
});

it("returns false if another spy called between", function () {
this.spy1();
this.spy3();
this.spy2();

assert.isFalse(this.spy2.calledImmediatelyAfter(this.spy1));
});
});

describe(".calledImmediatelyBefore", function () {
beforeEach(function () {
this.spy1 = createSpy();
this.spy2 = createSpy();
this.spy3 = createSpy();
});

it("is function", function () {
assert.isFunction(this.spy1.calledImmediatelyBefore);
});

it("returns true if first call to A was immediately after first to B", function () {
this.spy2();
this.spy1();

assert(this.spy2.calledImmediatelyBefore(this.spy1));
});

it("compares calls directly", function () {
this.spy2();
this.spy1();

assert(this.spy2.getCall(0).calledImmediatelyBefore(this.spy1.getCall(0)));
});

it("returns false if not called", function () {
this.spy2();

assert.isFalse(this.spy1.calledImmediatelyBefore(this.spy2));
});

it("returns false if other not called", function () {
this.spy1();

assert.isFalse(this.spy1.calledImmediatelyBefore(this.spy2));
});

it("returns false if other called last", function () {
this.spy2();
this.spy1();
this.spy2();

assert.isFalse(this.spy2.calledImmediatelyBefore(this.spy1));
});

it("returns false if another spy called between", function () {
this.spy1();
this.spy3();
this.spy2();

assert.isFalse(this.spy1.calledImmediatelyBefore(this.spy2));
});
});

describe(".firstCall", function () {
it("is undefined by default", function () {
var spy = createSpy();
Expand Down

0 comments on commit 3aa9919

Please sign in to comment.