3 var maybe = require("../")
4 var assert = require("assert")
5 var Promise = global.Promise || require("promise")
7 describe("maybe", function () {
8 it("should call the callback with result the promise is resolved to", function (done) {
9 var f = function f (cb) {
10 return maybe(cb, new Promise(function (resolve, reject) {
11 process.nextTick(function () {
17 f(function (err, result) {
18 assert.ifError(err, "no error")
19 assert.strictEqual(result, "hi")
24 it("should call the callback with the error the promise is rejected with", function (done) {
25 var f = function f (cb) {
26 return maybe(cb, new Promise(function (resolve, reject) {
27 process.nextTick(function () {
28 return reject(new Error("boom"))
33 f(function (err, result) {
34 assert(err, "we got an error")
35 assert.strictEqual(result, undefined, "we got undefined result")
36 assert(err instanceof Error, "error is an Error")
37 assert.strictEqual(err.message, "boom", "error message is boom")
42 it("should return undefined when called with a callback", function () {
43 var f = function f (cb) {
44 return maybe(cb, new Promise(function (resolve, reject) {
49 var returnVal = f(function (err, result) {})
50 assert.strictEqual(returnVal, undefined, "returned val is undefined")
53 it("should return the same promise when no callback is provided", function () {
56 var f = function f (cb) {
57 p = new Promise(function (resolve, reject) {
58 process.nextTick(function () {
66 assert(p instanceof Promise, "returned val is a Promise")
67 assert.strictEqual(returnVal, p, "returned val is same obj (not a new Promise)")
70 it("should allow errors thrown in the callback to be uncaught", function (done) {
73 // Temporarily remove Mocha's global error handling so we can
74 // verify error is indeed uncaught by installing our own
75 // global error handler.
76 if (process.browser) {
77 mochaHandler = global.onerror
78 global.onerror = handleUncaughtException
81 mochaHandler = process.listeners("uncaughtException").pop()
82 process.removeListener("uncaughtException", mochaHandler)
83 process.once("uncaughtException", handleUncaughtException)
86 var f = function f (cb) {
87 return maybe(cb, new Promise(function (resolve, reject) {
88 process.nextTick(function () {
94 f(function (err, result) {
95 throw new Error("yep")
98 function handleUncaughtException (err) {
99 // `err` is either an Error when running under Node, or a
100 // string if running under a browser.
101 var msg = err.message || err
103 assert(msg.match(/\byep\b/), "got expected error")
105 // Restore Mocha's global error handler.
106 if (process.browser) {
107 global.onerror = mochaHandler
110 process.on("uncaughtException", mochaHandler)
115 // Don't leak error to browser console
120 it("should not let the callback be called more than once", function (done) {
121 var f = function f (cb) {
122 return maybe(cb, new Promise(function (resolve, reject) {
123 process.nextTick(function () {
130 f(function (err, result) {
132 assert(called <= 1, "called only once")
133 setTimeout(function () { done() }, 100)
134 return Promise.reject(new Error("bah"))