1
0

nthen_test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. var test = require("tap").test;
  2. var nThen = require("nthen");
  3. test("make sure nThen works", function (t) {
  4. var to = setTimeout(function() { throw Error("timeout"); }, 1000);
  5. var i = 0;
  6. var lastI = 0;
  7. var chk = function() { t.equal(i, ++lastI); };
  8. nThen(function(waitFor) {
  9. // Obvious path.
  10. setTimeout(waitFor(function() {
  11. i++;
  12. }), 1);
  13. }).nThen(function(waitFor) {
  14. // Traditional chained calls should work.
  15. chk();
  16. setTimeout(waitFor(function() {
  17. chk();
  18. setTimeout(waitFor(function() {
  19. chk();
  20. i++;
  21. }), 1);
  22. i++;
  23. }), 1);
  24. i++;
  25. }).nThen(function(waitFor) {
  26. chk();
  27. i++;
  28. // no callback should still work
  29. }).nThen(function(waitFor) {
  30. chk();
  31. i++;
  32. // This will timeout.
  33. setTimeout(waitFor(function() {
  34. // never run.
  35. i = 10000;
  36. }), 100);
  37. }).nThen(function(waitFor) {
  38. t.notOk(1);
  39. }).nThen(function(waitFor) {
  40. t.notOk(1);
  41. }).nThen(function(waitFor) {
  42. t.notOk(1);
  43. }).nThen(function(waitFor) {
  44. t.notOk(1);
  45. }).orTimeout(function(waitFor) {
  46. chk();
  47. setTimeout(waitFor(function() {
  48. i++;
  49. }), 1);
  50. }, 30).nThen(function(waitFor) {
  51. chk();
  52. setTimeout(waitFor(function() {
  53. i++;
  54. }), 1);
  55. }).nThen(function(waitFor) {
  56. chk();
  57. t.equals(lastI, 8);
  58. clearTimeout(to);
  59. t.end();
  60. });
  61. });
  62. test("no callback first function", function (t) {
  63. var to = setTimeout(function() {
  64. t.notOk(1);
  65. t.end();
  66. }, 100);
  67. var i = 0;
  68. nThen(function(waitFor) {
  69. i++;
  70. // waitFor is never called
  71. }).nThen(function(waitfor) {
  72. t.equals(i, 1);
  73. clearTimeout(to);
  74. t.end();
  75. });
  76. });
  77. test("abort synchronous", function (t) {
  78. nThen(function(waitFor) {
  79. waitFor.abort();
  80. }).nThen(function(waitFor) {
  81. t.notOk(1);
  82. });
  83. nThen(function(waitFor) {
  84. }).nThen(function(waitFor) {
  85. waitFor.abort();
  86. }).nThen(function(waitFor) {
  87. t.notOk(1);
  88. });
  89. var to;
  90. nThen(function(waitFor) {
  91. }).nThen(function(waitFor) {
  92. to = setTimeout(waitFor(function () {
  93. // this timeout is not cleared
  94. t.end();
  95. }), 10);
  96. waitFor.abort();
  97. }).orTimeout(function(waitFor) {
  98. clearTimeout(to);
  99. t.notOk(1);
  100. }, 1);
  101. });
  102. test("abort asynchronous", function (t) {
  103. nThen(function(waitFor) {
  104. setTimeout(waitFor(function() {
  105. waitFor.abort();
  106. }));
  107. }).nThen(function(waitFor) {
  108. t.notOk(1);
  109. });
  110. nThen(function(waitFor) {
  111. setTimeout(waitFor(function() {
  112. // do nothing
  113. }));
  114. }).nThen(function(waitFor) {
  115. setTimeout(waitFor(function() {
  116. waitFor.abort();
  117. }));
  118. }).nThen(function(waitFor) {
  119. t.notOk(1);
  120. });
  121. var to;
  122. nThen(function(waitFor) {
  123. setTimeout(waitFor(function() {
  124. // do nothing
  125. }));
  126. }).nThen(function(waitFor) {
  127. to = setTimeout(waitFor(function () {
  128. // this timeout is not cleared
  129. t.end();
  130. }), 10);
  131. waitFor.abort();
  132. }).orTimeout(function(waitFor) {
  133. clearTimeout(to);
  134. t.notOk(1);
  135. }, 1);
  136. });