node-bluebird/test/mocha/3.2.3.js

188 lines
6.1 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 testFulfilled = require("./helpers/testThreeCases").testFulfilled;
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("3.2.3: If `onRejected` is a function,", function () {
describe("3.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("3.2.3.2: 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();
});
});
});
describe("3.2.3.3: it must not be called if `onFulfilled` has been called.", function () {
testFulfilled(dummy, function (promise, done) {
var onFulfilledCalled = false;
promise.then(function onFulfilled() {
onFulfilledCalled = true;
}, function onRejected() {
assert.strictEqual(onFulfilledCalled, false);
done();
});
setTimeout(function(){done();}, 100);
});
specify("trying to fulfill then immediately reject", function (done) {
var tuple = pending();
var onFulfilledCalled = false;
tuple.promise.then(function onFulfilled() {
onFulfilledCalled = true;
}, function onRejected() {
assert.strictEqual(onFulfilledCalled, false);
done();
});
tuple.fulfill(dummy);
tuple.reject(dummy);
setTimeout(function(){done();}, 100);
});
specify("trying to fulfill then reject, delayed", function (done) {
var tuple = pending();
var onFulfilledCalled = false;
tuple.promise.then(function onFulfilled() {
onFulfilledCalled = true;
}, function onRejected() {
assert.strictEqual(onFulfilledCalled, false);
done();
});
setTimeout(function () {
tuple.fulfill(dummy);
tuple.reject(dummy);
}, 50);
setTimeout(function(){done();}, 100);
});
specify("trying to fulfill immediately then reject delayed", function (done) {
var tuple = pending();
var onFulfilledCalled = false;
tuple.promise.then(function onFulfilled() {
onFulfilledCalled = true;
}, function onRejected() {
assert.strictEqual(onFulfilledCalled, false);
done();
});
tuple.fulfill(dummy);
setTimeout(function () {
tuple.reject(dummy);
}, 50);
setTimeout(function(){done();}, 100);
});
});
});