Skip to content

Commit 2f941e2

Browse files
authored
Merge pull request #1406 from lucasfcosta/called-in-order-considers-callcount
Called in order considers callCount
2 parents 3af985a + 54b5837 commit 2f941e2

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
"use strict";
22

3+
var every = Array.prototype.every;
4+
35
module.exports = function calledInOrder(spies) {
6+
var callMap = {};
7+
8+
function hasCallsLeft(spy) {
9+
if (callMap[spy.id] === undefined) {
10+
callMap[spy.id] = 0;
11+
}
12+
13+
return callMap[spy.id] < spy.callCount;
14+
}
15+
416
if (arguments.length > 1) {
517
spies = arguments;
618
}
719

8-
for (var i = 1, l = spies.length; i < l; i++) {
9-
if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
10-
return false;
20+
return every.call(spies, function checkAdjacentCalls(spy, i) {
21+
var calledBeforeNext = true;
22+
23+
if (i !== spies.length - 1) {
24+
calledBeforeNext = spy.calledBefore(spies[i + 1]);
25+
}
26+
27+
if (hasCallsLeft(spy) && calledBeforeNext) {
28+
callMap[spy.id] += 1;
29+
return true;
1130
}
12-
}
1331

14-
return true;
32+
return false;
33+
});
1534
};

test/issues/issues-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,19 @@ describe("issues", function () {
186186
assert(firstFake !== secondFake);
187187
});
188188
});
189+
190+
describe("#1398", function () {
191+
it("Call order takes into account both calledBefore and callCount", function () {
192+
var s1 = sinon.spy();
193+
var s2 = sinon.spy();
194+
195+
s1();
196+
s2();
197+
s1();
198+
199+
assert.exception(function () {
200+
sinon.assert.callOrder(s2, s1, s2);
201+
});
202+
});
203+
});
189204
});

0 commit comments

Comments
 (0)