called-in-order.js 817 B

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. var every = require("./prototypes/array").every;
  3. function hasCallsLeft(callMap, spy) {
  4. if (callMap[spy.id] === undefined) {
  5. callMap[spy.id] = 0;
  6. }
  7. return callMap[spy.id] < spy.callCount;
  8. }
  9. function checkAdjacentCalls(callMap, spy, index, spies) {
  10. var calledBeforeNext = true;
  11. if (index !== spies.length - 1) {
  12. calledBeforeNext = spy.calledBefore(spies[index + 1]);
  13. }
  14. if (hasCallsLeft(callMap, spy) && calledBeforeNext) {
  15. callMap[spy.id] += 1;
  16. return true;
  17. }
  18. return false;
  19. }
  20. module.exports = function calledInOrder(spies) {
  21. var callMap = {};
  22. // eslint-disable-next-line no-underscore-dangle
  23. var _spies = arguments.length > 1 ? arguments : spies;
  24. return every(_spies, checkAdjacentCalls.bind(null, callMap));
  25. };