called-in-order.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. var assert = require("@sinonjs/referee-sinon").assert;
  3. var calledInOrder = require("./called-in-order");
  4. var sinon = require("@sinonjs/referee-sinon").sinon;
  5. var testObject1 = {
  6. someFunction: function() {
  7. return;
  8. }
  9. };
  10. var testObject2 = {
  11. otherFunction: function() {
  12. return;
  13. }
  14. };
  15. var testObject3 = {
  16. thirdFunction: function() {
  17. return;
  18. }
  19. };
  20. function testMethod() {
  21. testObject1.someFunction();
  22. testObject2.otherFunction();
  23. testObject2.otherFunction();
  24. testObject2.otherFunction();
  25. testObject3.thirdFunction();
  26. }
  27. describe("calledInOrder", function() {
  28. beforeEach(function() {
  29. sinon.stub(testObject1, "someFunction");
  30. sinon.stub(testObject2, "otherFunction");
  31. sinon.stub(testObject3, "thirdFunction");
  32. testMethod();
  33. });
  34. afterEach(function() {
  35. testObject1.someFunction.restore();
  36. testObject2.otherFunction.restore();
  37. testObject3.thirdFunction.restore();
  38. });
  39. describe("given single array argument", function() {
  40. describe("when stubs were called in expected order", function() {
  41. it("returns true", function() {
  42. assert.isTrue(
  43. calledInOrder([
  44. testObject1.someFunction,
  45. testObject2.otherFunction
  46. ])
  47. );
  48. assert.isTrue(
  49. calledInOrder([
  50. testObject1.someFunction,
  51. testObject2.otherFunction,
  52. testObject2.otherFunction,
  53. testObject3.thirdFunction
  54. ])
  55. );
  56. });
  57. });
  58. describe("when stubs were called in unexpected order", function() {
  59. it("returns false", function() {
  60. assert.isFalse(
  61. calledInOrder([
  62. testObject2.otherFunction,
  63. testObject1.someFunction
  64. ])
  65. );
  66. assert.isFalse(
  67. calledInOrder([
  68. testObject2.otherFunction,
  69. testObject1.someFunction,
  70. testObject1.someFunction,
  71. testObject3.thirdFunction
  72. ])
  73. );
  74. });
  75. });
  76. });
  77. describe("given multiple arguments", function() {
  78. describe("when stubs were called in expected order", function() {
  79. it("returns true", function() {
  80. assert.isTrue(
  81. calledInOrder(
  82. testObject1.someFunction,
  83. testObject2.otherFunction
  84. )
  85. );
  86. assert.isTrue(
  87. calledInOrder(
  88. testObject1.someFunction,
  89. testObject2.otherFunction,
  90. testObject3.thirdFunction
  91. )
  92. );
  93. });
  94. });
  95. describe("when stubs were called in unexpected order", function() {
  96. it("returns false", function() {
  97. assert.isFalse(
  98. calledInOrder(
  99. testObject2.otherFunction,
  100. testObject1.someFunction
  101. )
  102. );
  103. assert.isFalse(
  104. calledInOrder(
  105. testObject2.otherFunction,
  106. testObject1.someFunction,
  107. testObject3.thirdFunction
  108. )
  109. );
  110. });
  111. });
  112. });
  113. });