nthen_test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var test = require("tap").test;
  2. var nThen = require("../index");
  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. });