maybeTest.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict"
  2. var maybe = require("../")
  3. var assert = require("assert")
  4. var Promise = global.Promise || require("promise")
  5. describe("maybe", function () {
  6. it("should call the callback with result the promise is resolved to", function (done) {
  7. var f = function f (cb) {
  8. return maybe(cb, new Promise(function (resolve, reject) {
  9. process.nextTick(function () {
  10. return resolve("hi")
  11. })
  12. }))
  13. }
  14. f(function (err, result) {
  15. assert.ifError(err, "no error")
  16. assert.strictEqual(result, "hi")
  17. return done()
  18. })
  19. })
  20. it("should call the callback with the error the promise is rejected with", function (done) {
  21. var f = function f (cb) {
  22. return maybe(cb, new Promise(function (resolve, reject) {
  23. process.nextTick(function () {
  24. return reject(new Error("boom"))
  25. })
  26. }))
  27. }
  28. f(function (err, result) {
  29. assert(err, "we got an error")
  30. assert.strictEqual(result, undefined, "we got undefined result")
  31. assert(err instanceof Error, "error is an Error")
  32. assert.strictEqual(err.message, "boom", "error message is boom")
  33. return done()
  34. })
  35. })
  36. it("should return undefined when called with a callback", function () {
  37. var f = function f (cb) {
  38. return maybe(cb, new Promise(function (resolve, reject) {
  39. //...
  40. }))
  41. }
  42. var returnVal = f(function (err, result) {})
  43. assert.strictEqual(returnVal, undefined, "returned val is undefined")
  44. })
  45. it("should return the same promise when no callback is provided", function () {
  46. var p
  47. var f = function f (cb) {
  48. p = new Promise(function (resolve, reject) {
  49. process.nextTick(function () {
  50. return resolve("hi")
  51. })
  52. })
  53. return maybe(cb, p)
  54. }
  55. var returnVal = f()
  56. assert(p instanceof Promise, "returned val is a Promise")
  57. assert.strictEqual(returnVal, p, "returned val is same obj (not a new Promise)")
  58. })
  59. it("should allow errors thrown in the callback to be uncaught", function (done) {
  60. var mochaHandler
  61. // Temporarily remove Mocha's global error handling so we can
  62. // verify error is indeed uncaught by installing our own
  63. // global error handler.
  64. if (process.browser) {
  65. mochaHandler = global.onerror
  66. global.onerror = handleUncaughtException
  67. }
  68. else {
  69. mochaHandler = process.listeners("uncaughtException").pop()
  70. process.removeListener("uncaughtException", mochaHandler)
  71. process.once("uncaughtException", handleUncaughtException)
  72. }
  73. var f = function f (cb) {
  74. return maybe(cb, new Promise(function (resolve, reject) {
  75. process.nextTick(function () {
  76. return resolve("hi")
  77. })
  78. }))
  79. }
  80. f(function (err, result) {
  81. throw new Error("yep")
  82. })
  83. function handleUncaughtException (err) {
  84. // `err` is either an Error when running under Node, or a
  85. // string if running under a browser.
  86. var msg = err.message || err
  87. assert(msg.match(/\byep\b/), "got expected error")
  88. // Restore Mocha's global error handler.
  89. if (process.browser) {
  90. global.onerror = mochaHandler
  91. }
  92. else {
  93. process.on("uncaughtException", mochaHandler)
  94. }
  95. done()
  96. // Don't leak error to browser console
  97. return true
  98. }
  99. })
  100. it("should not let the callback be called more than once", function (done) {
  101. var f = function f (cb) {
  102. return maybe(cb, new Promise(function (resolve, reject) {
  103. process.nextTick(function () {
  104. resolve("foo")
  105. })
  106. }))
  107. }
  108. var called = 0
  109. f(function (err, result) {
  110. called++
  111. assert(called <= 1, "called only once")
  112. setTimeout(function () { done() }, 100)
  113. return Promise.reject(new Error("bah"))
  114. })
  115. })
  116. })