blob: 5ce3017e23856f8f8b261a59d919307ab6a74456 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001"use strict"
2
3var maybe = require("../")
4var assert = require("assert")
5var Promise = global.Promise || require("promise")
6
7describe("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 () {
12 return resolve("hi")
13 })
14 }))
15 }
16
17 f(function (err, result) {
18 assert.ifError(err, "no error")
19 assert.strictEqual(result, "hi")
20 return done()
21 })
22 })
23
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"))
29 })
30 }))
31 }
32
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")
38 return done()
39 })
40 })
41
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) {
45 //...
46 }))
47 }
48
49 var returnVal = f(function (err, result) {})
50 assert.strictEqual(returnVal, undefined, "returned val is undefined")
51 })
52
53 it("should return the same promise when no callback is provided", function () {
54 var p
55
56 var f = function f (cb) {
57 p = new Promise(function (resolve, reject) {
58 process.nextTick(function () {
59 return resolve("hi")
60 })
61 })
62 return maybe(cb, p)
63 }
64
65 var returnVal = f()
66 assert(p instanceof Promise, "returned val is a Promise")
67 assert.strictEqual(returnVal, p, "returned val is same obj (not a new Promise)")
68 })
69
70 it("should allow errors thrown in the callback to be uncaught", function (done) {
71 var mochaHandler
72
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
79 }
80 else {
81 mochaHandler = process.listeners("uncaughtException").pop()
82 process.removeListener("uncaughtException", mochaHandler)
83 process.once("uncaughtException", handleUncaughtException)
84 }
85
86 var f = function f (cb) {
87 return maybe(cb, new Promise(function (resolve, reject) {
88 process.nextTick(function () {
89 return resolve("hi")
90 })
91 }))
92 }
93
94 f(function (err, result) {
95 throw new Error("yep")
96 })
97
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
102
103 assert(msg.match(/\byep\b/), "got expected error")
104
105 // Restore Mocha's global error handler.
106 if (process.browser) {
107 global.onerror = mochaHandler
108 }
109 else {
110 process.on("uncaughtException", mochaHandler)
111 }
112
113 done()
114
115 // Don't leak error to browser console
116 return true
117 }
118 })
119
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 () {
124 resolve("foo")
125 })
126 }))
127 }
128
129 var called = 0
130 f(function (err, result) {
131 called++
132 assert(called <= 1, "called only once")
133 setTimeout(function () { done() }, 100)
134 return Promise.reject(new Error("bah"))
135 })
136 })
137})