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.

137 lines
3.8 KiB

4 years ago
  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. })