GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.2 KiB

2 months ago
  1. var tape = require("tape");
  2. var asPromise = require("..");
  3. tape.test("aspromise", function(test) {
  4. test.test(this.name + " - resolve", function(test) {
  5. function fn(arg1, arg2, callback) {
  6. test.equal(this, ctx, "function should be called with this = ctx");
  7. test.equal(arg1, 1, "function should be called with arg1 = 1");
  8. test.equal(arg2, 2, "function should be called with arg2 = 2");
  9. callback(null, arg2);
  10. }
  11. var ctx = {};
  12. var promise = asPromise(fn, ctx, 1, 2);
  13. promise.then(function(arg2) {
  14. test.equal(arg2, 2, "promise should be resolved with arg2 = 2");
  15. test.end();
  16. }).catch(function(err) {
  17. test.fail("promise should not be rejected (" + err + ")");
  18. });
  19. });
  20. test.test(this.name + " - reject", function(test) {
  21. function fn(arg1, arg2, callback) {
  22. test.equal(this, ctx, "function should be called with this = ctx");
  23. test.equal(arg1, 1, "function should be called with arg1 = 1");
  24. test.equal(arg2, 2, "function should be called with arg2 = 2");
  25. callback(arg1);
  26. }
  27. var ctx = {};
  28. var promise = asPromise(fn, ctx, 1, 2);
  29. promise.then(function() {
  30. test.fail("promise should not be resolved");
  31. }).catch(function(err) {
  32. test.equal(err, 1, "promise should be rejected with err = 1");
  33. test.end();
  34. });
  35. });
  36. test.test(this.name + " - resolve twice", function(test) {
  37. function fn(arg1, arg2, callback) {
  38. test.equal(this, ctx, "function should be called with this = ctx");
  39. test.equal(arg1, 1, "function should be called with arg1 = 1");
  40. test.equal(arg2, 2, "function should be called with arg2 = 2");
  41. callback(null, arg2);
  42. callback(null, arg1);
  43. }
  44. var ctx = {};
  45. var count = 0;
  46. var promise = asPromise(fn, ctx, 1, 2);
  47. promise.then(function(arg2) {
  48. test.equal(arg2, 2, "promise should be resolved with arg2 = 2");
  49. if (++count > 1)
  50. test.fail("promise should not be resolved twice");
  51. test.end();
  52. }).catch(function(err) {
  53. test.fail("promise should not be rejected (" + err + ")");
  54. });
  55. });
  56. test.test(this.name + " - reject twice", function(test) {
  57. function fn(arg1, arg2, callback) {
  58. test.equal(this, ctx, "function should be called with this = ctx");
  59. test.equal(arg1, 1, "function should be called with arg1 = 1");
  60. test.equal(arg2, 2, "function should be called with arg2 = 2");
  61. callback(arg1);
  62. callback(arg2);
  63. }
  64. var ctx = {};
  65. var count = 0;
  66. var promise = asPromise(fn, ctx, 1, 2);
  67. promise.then(function() {
  68. test.fail("promise should not be resolved");
  69. }).catch(function(err) {
  70. test.equal(err, 1, "promise should be rejected with err = 1");
  71. if (++count > 1)
  72. test.fail("promise should not be rejected twice");
  73. test.end();
  74. });
  75. });
  76. test.test(this.name + " - reject error", function(test) {
  77. function fn(callback) {
  78. test.ok(arguments.length === 1 && typeof callback === "function", "function should be called with just a callback");
  79. throw 3;
  80. }
  81. var promise = asPromise(fn, null);
  82. promise.then(function() {
  83. test.fail("promise should not be resolved");
  84. }).catch(function(err) {
  85. test.equal(err, 3, "promise should be rejected with err = 3");
  86. test.end();
  87. });
  88. });
  89. test.test(this.name + " - reject and error", function(test) {
  90. function fn(callback) {
  91. callback(3);
  92. throw 4;
  93. }
  94. var count = 0;
  95. var promise = asPromise(fn, null);
  96. promise.then(function() {
  97. test.fail("promise should not be resolved");
  98. }).catch(function(err) {
  99. test.equal(err, 3, "promise should be rejected with err = 3");
  100. if (++count > 1)
  101. test.fail("promise should not be rejected twice");
  102. test.end();
  103. });
  104. });
  105. });