index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. require('mocha-as-promised')();
  2. var assert = require('assert');
  3. var Promise = require('promise');
  4. var nodeify = require('../');
  5. var A = {};
  6. var B = {};
  7. describe('nodeify(promise, callback)', function () {
  8. describe('when callback is a function', function () {
  9. it('still returns a promise which is always fulfilled with undefined', function () {
  10. var pA = new Promise(function (res) { res.fulfill(A); });
  11. var pB = new Promise(function (res) { res.reject(B); });
  12. return nodeify(pA, function (err, res) {})
  13. .then(function (res) {
  14. assert(typeof res === 'undefined');
  15. return nodeify(pB, function (err, res) {});
  16. })
  17. .then(function (res) {
  18. assert(typeof res === 'undefined');
  19. });
  20. });
  21. describe('when the promise is resolved', function () {
  22. it('calls the callback with (null, result)', function (done) {
  23. var p = new Promise(function (res) { res.fulfill(A); });
  24. nodeify(p, function (err, res) {
  25. if (err) return done(err);
  26. assert(res === A);
  27. done();
  28. });
  29. });
  30. });
  31. describe('when the promise is rejected', function () {
  32. it('calls the callback with (error)', function (done) {
  33. var p = new Promise(function (res) { res.reject(A); });
  34. nodeify(p, function (err, res) {
  35. assert(err === A);
  36. assert(arguments.length === 1);
  37. done();
  38. });
  39. });
  40. });
  41. });
  42. describe('when callback is not a function', function () {
  43. it('returns the original promise', function () {
  44. assert(nodeify(A) === A);
  45. assert(nodeify(A, null) === A);
  46. assert(nodeify(A, B) === A);
  47. });
  48. });
  49. });
  50. describe('(new nodeify.Promise(fn)).nodeify(callback)', function () {
  51. describe('when callback is a function', function () {
  52. it('still returns a promise which is always fulfilled with undefined', function () {
  53. var pA = new nodeify.Promise(function (res) { res.fulfill(A); });
  54. var pB = new nodeify.Promise(function (res) { res.reject(B); });
  55. return pA.nodeify(function (err, res) {})
  56. .then(function (res) {
  57. assert(typeof res === 'undefined');
  58. return pB.nodeify(function (err, res) {});
  59. })
  60. .then(function (res) {
  61. assert(typeof res === 'undefined');
  62. });
  63. });
  64. describe('when the promise is resolved', function () {
  65. it('calls the callback with (null, result)', function (done) {
  66. var p = new nodeify.Promise(function (res) { res.fulfill(A); });
  67. p.nodeify(function (err, res) {
  68. if (err) return done(err);
  69. assert(res === A);
  70. done();
  71. });
  72. });
  73. });
  74. describe('when the promise is rejected', function () {
  75. it('calls the callback with (error)', function (done) {
  76. var p = new nodeify.Promise(function (res) { res.reject(A); });
  77. p.nodeify(function (err, res) {
  78. assert(err === A);
  79. assert(arguments.length === 1);
  80. done();
  81. });
  82. });
  83. });
  84. });
  85. describe('when callback is not a function', function () {
  86. it('returns the original promise', function () {
  87. var p = new nodeify.Promise(function (res) { res.fulfill(A); });
  88. assert(p.nodeify() === p);
  89. assert(p.nodeify(null) === p);
  90. assert(p.nodeify(B) === p);
  91. });
  92. });
  93. describe('calls to then', function () {
  94. it('maintain the nodeify method', function (done) {
  95. (new nodeify.Promise(function (res) { res.fulfill(A); }))
  96. .then(function () { return B; })
  97. .then(function (res) {
  98. assert(res === B);
  99. return A;
  100. })
  101. .nodeify(function (err, res) {
  102. if (err) return done(err);
  103. assert(res === A);
  104. done();
  105. });
  106. });
  107. });
  108. });
  109. describe('nodeify.extend(promise)', function () {
  110. it('adds the nodeify method to promise (including promises resulting from calling promise.then)', function (done) {
  111. nodeify.extend(new Promise(function (res) { res.fulfill(A); }))
  112. .then(function () { return B; })
  113. .then(function (res) {
  114. assert(res === B);
  115. return A;
  116. })
  117. .nodeify(function (err, res) {
  118. if (err) return done(err);
  119. assert(res === A);
  120. done();
  121. });
  122. });
  123. });
  124. describe('nodeify.extend(PromiseConstructor)', function () {
  125. function PromiseConstructor() {};
  126. it('adds the nodeify method to PromiseConstructor.prototype', function () {
  127. nodeify.extend(PromiseConstructor)
  128. assert(typeof PromiseConstructor.prototype.nodeify === 'function');
  129. });
  130. });
  131. describe('nodeify.extend()', function () {
  132. it('adds the nodeify method on to all promises inheriting from Promise', function () {
  133. nodeify.extend()
  134. assert(typeof Promise.prototype.nodeify === 'function');
  135. });
  136. });