| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | var every = require("./prototypes/array").every;
|
|---|
| 4 |
|
|---|
| 5 | /**
|
|---|
| 6 | * @private
|
|---|
| 7 | */
|
|---|
| 8 | function hasCallsLeft(callMap, spy) {
|
|---|
| 9 | if (callMap[spy.id] === undefined) {
|
|---|
| 10 | callMap[spy.id] = 0;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | return callMap[spy.id] < spy.callCount;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @private
|
|---|
| 18 | */
|
|---|
| 19 | function checkAdjacentCalls(callMap, spy, index, spies) {
|
|---|
| 20 | var calledBeforeNext = true;
|
|---|
| 21 |
|
|---|
| 22 | if (index !== spies.length - 1) {
|
|---|
| 23 | calledBeforeNext = spy.calledBefore(spies[index + 1]);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | if (hasCallsLeft(callMap, spy) && calledBeforeNext) {
|
|---|
| 27 | callMap[spy.id] += 1;
|
|---|
| 28 | return true;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | return false;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * A Sinon proxy object (fake, spy, stub)
|
|---|
| 36 | *
|
|---|
| 37 | * @typedef {object} SinonProxy
|
|---|
| 38 | * @property {Function} calledBefore - A method that determines if this proxy was called before another one
|
|---|
| 39 | * @property {string} id - Some id
|
|---|
| 40 | * @property {number} callCount - Number of times this proxy has been called
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Returns true when the spies have been called in the order they were supplied in
|
|---|
| 45 | *
|
|---|
| 46 | * @param {SinonProxy[] | SinonProxy} spies An array of proxies, or several proxies as arguments
|
|---|
| 47 | * @returns {boolean} true when spies are called in order, false otherwise
|
|---|
| 48 | */
|
|---|
| 49 | function calledInOrder(spies) {
|
|---|
| 50 | var callMap = {};
|
|---|
| 51 | // eslint-disable-next-line no-underscore-dangle
|
|---|
| 52 | var _spies = arguments.length > 1 ? arguments : spies;
|
|---|
| 53 |
|
|---|
| 54 | return every(_spies, checkAdjacentCalls.bind(null, callMap));
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | module.exports = calledInOrder;
|
|---|