aboutsummaryrefslogtreecommitdiff
path: root/node_modules/call-me-maybe/test
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2019-10-06 21:37:45 +0200
committerDimitri Staessens <dimitri@ouroboros.rocks>2019-10-06 21:37:45 +0200
commit3c51c3be85bb0d1bdb87ea0d6632f1c256912f27 (patch)
treec7ccc8279b12c4f7bdbbb4270d617e48f51722e4 /node_modules/call-me-maybe/test
parent412c104bebc507bea9c94fd53b5bdc4b64cbfe31 (diff)
downloadwebsite-3c51c3be85bb0d1bdb87ea0d6632f1c256912f27.tar.gz
website-3c51c3be85bb0d1bdb87ea0d6632f1c256912f27.zip
build: Add some required modules for node
Diffstat (limited to 'node_modules/call-me-maybe/test')
-rw-r--r--node_modules/call-me-maybe/test/maybeTest.js137
1 files changed, 137 insertions, 0 deletions
diff --git a/node_modules/call-me-maybe/test/maybeTest.js b/node_modules/call-me-maybe/test/maybeTest.js
new file mode 100644
index 0000000..5ce3017
--- /dev/null
+++ b/node_modules/call-me-maybe/test/maybeTest.js
@@ -0,0 +1,137 @@
+"use strict"
+
+var maybe = require("../")
+var assert = require("assert")
+var Promise = global.Promise || require("promise")
+
+describe("maybe", function () {
+ it("should call the callback with result the promise is resolved to", function (done) {
+ var f = function f (cb) {
+ return maybe(cb, new Promise(function (resolve, reject) {
+ process.nextTick(function () {
+ return resolve("hi")
+ })
+ }))
+ }
+
+ f(function (err, result) {
+ assert.ifError(err, "no error")
+ assert.strictEqual(result, "hi")
+ return done()
+ })
+ })
+
+ it("should call the callback with the error the promise is rejected with", function (done) {
+ var f = function f (cb) {
+ return maybe(cb, new Promise(function (resolve, reject) {
+ process.nextTick(function () {
+ return reject(new Error("boom"))
+ })
+ }))
+ }
+
+ f(function (err, result) {
+ assert(err, "we got an error")
+ assert.strictEqual(result, undefined, "we got undefined result")
+ assert(err instanceof Error, "error is an Error")
+ assert.strictEqual(err.message, "boom", "error message is boom")
+ return done()
+ })
+ })
+
+ it("should return undefined when called with a callback", function () {
+ var f = function f (cb) {
+ return maybe(cb, new Promise(function (resolve, reject) {
+ //...
+ }))
+ }
+
+ var returnVal = f(function (err, result) {})
+ assert.strictEqual(returnVal, undefined, "returned val is undefined")
+ })
+
+ it("should return the same promise when no callback is provided", function () {
+ var p
+
+ var f = function f (cb) {
+ p = new Promise(function (resolve, reject) {
+ process.nextTick(function () {
+ return resolve("hi")
+ })
+ })
+ return maybe(cb, p)
+ }
+
+ var returnVal = f()
+ assert(p instanceof Promise, "returned val is a Promise")
+ assert.strictEqual(returnVal, p, "returned val is same obj (not a new Promise)")
+ })
+
+ it("should allow errors thrown in the callback to be uncaught", function (done) {
+ var mochaHandler
+
+ // Temporarily remove Mocha's global error handling so we can
+ // verify error is indeed uncaught by installing our own
+ // global error handler.
+ if (process.browser) {
+ mochaHandler = global.onerror
+ global.onerror = handleUncaughtException
+ }
+ else {
+ mochaHandler = process.listeners("uncaughtException").pop()
+ process.removeListener("uncaughtException", mochaHandler)
+ process.once("uncaughtException", handleUncaughtException)
+ }
+
+ var f = function f (cb) {
+ return maybe(cb, new Promise(function (resolve, reject) {
+ process.nextTick(function () {
+ return resolve("hi")
+ })
+ }))
+ }
+
+ f(function (err, result) {
+ throw new Error("yep")
+ })
+
+ function handleUncaughtException (err) {
+ // `err` is either an Error when running under Node, or a
+ // string if running under a browser.
+ var msg = err.message || err
+
+ assert(msg.match(/\byep\b/), "got expected error")
+
+ // Restore Mocha's global error handler.
+ if (process.browser) {
+ global.onerror = mochaHandler
+ }
+ else {
+ process.on("uncaughtException", mochaHandler)
+ }
+
+ done()
+
+ // Don't leak error to browser console
+ return true
+ }
+ })
+
+ it("should not let the callback be called more than once", function (done) {
+ var f = function f (cb) {
+ return maybe(cb, new Promise(function (resolve, reject) {
+ process.nextTick(function () {
+ resolve("foo")
+ })
+ }))
+ }
+
+ var called = 0
+ f(function (err, result) {
+ called++
+ assert(called <= 1, "called only once")
+ setTimeout(function () { done() }, 100)
+ return Promise.reject(new Error("bah"))
+ })
+ })
+})