order-by-first-call.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var assert = require("@sinonjs/referee-sinon").assert;
  3. var knuthShuffle = require("knuth-shuffle").knuthShuffle;
  4. var sinon = require("@sinonjs/referee-sinon").sinon;
  5. var orderByFirstCall = require("./order-by-first-call");
  6. describe("orderByFirstCall", function() {
  7. it("should order an Array of spies by the callId of the first call, ascending", function() {
  8. // create an array of spies
  9. var spies = [
  10. sinon.spy(),
  11. sinon.spy(),
  12. sinon.spy(),
  13. sinon.spy(),
  14. sinon.spy(),
  15. sinon.spy()
  16. ];
  17. // call all the spies
  18. spies.forEach(function(spy) {
  19. spy();
  20. });
  21. // add a few uncalled spies
  22. spies.push(sinon.spy());
  23. spies.push(sinon.spy());
  24. // randomise the order of the spies
  25. knuthShuffle(spies);
  26. var sortedSpies = orderByFirstCall(spies);
  27. assert.equals(sortedSpies.length, spies.length);
  28. var orderedByFirstCall = sortedSpies.every(function(spy, index) {
  29. if (index + 1 === sortedSpies.length) {
  30. return true;
  31. }
  32. var nextSpy = sortedSpies[index + 1];
  33. // uncalled spies should be ordered first
  34. if (!spy.called) {
  35. return true;
  36. }
  37. return spy.calledImmediatelyBefore(nextSpy);
  38. });
  39. assert.isTrue(orderedByFirstCall);
  40. });
  41. });