node-bluebird/test/mocha/2.2.3.js

152 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var assert = require("assert");
var testRejected = require("./helpers/testThreeCases").testRejected;
var adapter = global.adapter;
var rejected = adapter.rejected;
var pending = adapter.pending;
var dummy = { dummy: "dummy" }; // we fulfill or reject with this when we don't intend to test against it
var sentinel = { sentinel: "sentinel" }; // a sentinel fulfillment value to test for with strict equality
describe("2.2.3: If `onRejected` is a function,", function () {
describe("2.2.3.1: it must be called after `promise` is rejected, with `promise`s rejection reason as its " +
"first argument.", function () {
testRejected(sentinel, function (promise, done) {
promise.then(null, function onRejected(reason) {
assert.strictEqual(reason, sentinel);
done();
});
});
});
describe("2.2.3.2: it must not be called before `promise` is rejected", function () {
specify("rejected after a delay", function (done) {
var tuple = pending();
var isRejected = false;
tuple.promise.then(null, function onRejected() {
assert.strictEqual(isRejected, true);
done();
});
setTimeout(function () {
tuple.reject(dummy);
isRejected = true;
}, 50);
});
specify("never rejected", function (done) {
var tuple = pending();
var onRejectedCalled = false;
tuple.promise.then(null, function onRejected() {
onRejectedCalled = true;
done();
});
setTimeout(function () {
assert.strictEqual(onRejectedCalled, false);
done();
}, 150);
});
});
describe("2.2.3.3: it must not be called more than once.", function () {
specify("already-rejected", function (done) {
var timesCalled = 0;
rejected(dummy).then(null, function onRejected() {
assert.strictEqual(++timesCalled, 1);
done();
});
});
specify("trying to reject a pending promise more than once, immediately", function (done) {
var tuple = pending();
var timesCalled = 0;
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled, 1);
done();
});
tuple.reject(dummy);
tuple.reject(dummy);
});
specify("trying to reject a pending promise more than once, delayed", function (done) {
var tuple = pending();
var timesCalled = 0;
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled, 1);
done();
});
setTimeout(function () {
tuple.reject(dummy);
tuple.reject(dummy);
}, 50);
});
specify("trying to reject a pending promise more than once, immediately then delayed", function (done) {
var tuple = pending();
var timesCalled = 0;
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled, 1);
done();
});
tuple.reject(dummy);
setTimeout(function () {
tuple.reject(dummy);
}, 50);
});
specify("when multiple `then` calls are made, spaced apart in time", function (done) {
var tuple = pending();
var timesCalled = [0, 0, 0];
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled[0], 1);
});
setTimeout(function () {
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled[1], 1);
});
}, 50);
setTimeout(function () {
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled[2], 1);
done();
});
}, 100);
setTimeout(function () {
tuple.reject(dummy);
}, 150);
});
specify("when `then` is interleaved with rejection", function (done) {
var tuple = pending();
var timesCalled = [0, 0];
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled[0], 1);
});
tuple.reject(dummy);
tuple.promise.then(null, function onRejected() {
assert.strictEqual(++timesCalled[1], 1);
done();
});
});
});
});