node-bluebird/test/mocha/3.2.2.js

188 lines
6.0 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 fulfilled = adapter.fulfilled;
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.2: If `onFulfilled` is a function,", function () {
describe("3.2.2.1: it must be called after `promise` is fulfilled, with `promise`s fulfillment value as its " +
"first argument.", function () {
testFulfilled(sentinel, function (promise, done) {
promise.then(function onFulfilled(value) {
assert.strictEqual(value, sentinel);
done();
});
});
});
describe("3.2.2.2: it must not be called more than once.", function () {
specify("already-fulfilled", function (done) {
var timesCalled = 0;
fulfilled(dummy).then(function onFulfilled() {
assert.strictEqual(++timesCalled, 1);
done();
});
});
specify("trying to fulfill a pending promise more than once, immediately", function (done) {
var tuple = pending();
var timesCalled = 0;
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled, 1);
done();
});
tuple.fulfill(dummy);
tuple.fulfill(dummy);
});
specify("trying to fulfill a pending promise more than once, delayed", function (done) {
var tuple = pending();
var timesCalled = 0;
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled, 1);
done();
});
setTimeout(function () {
tuple.fulfill(dummy);
tuple.fulfill(dummy);
}, 50);
});
specify("trying to fulfill a pending promise more than once, immediately then delayed", function (done) {
var tuple = pending();
var timesCalled = 0;
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled, 1);
done();
});
tuple.fulfill(dummy);
setTimeout(function () {
tuple.fulfill(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(function onFulfilled() {
assert.strictEqual(++timesCalled[0], 1);
});
setTimeout(function () {
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled[1], 1);
});
}, 50);
setTimeout(function () {
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled[2], 1);
done();
});
}, 100);
setTimeout(function () {
tuple.fulfill(dummy);
}, 150);
});
specify("when `then` is interleaved with fulfillment", function (done) {
var tuple = pending();
var timesCalled = [0, 0];
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled[0], 1);
});
tuple.fulfill(dummy);
tuple.promise.then(function onFulfilled() {
assert.strictEqual(++timesCalled[1], 1);
done();
});
});
});
describe("3.2.2.3: it must not be called if `onRejected` has been called.", function () {
testRejected(dummy, function (promise, done) {
var onRejectedCalled = false;
promise.then(function onFulfilled() {
assert.strictEqual(onRejectedCalled, false);
done();
}, function onRejected() {
onRejectedCalled = true;
});
setTimeout(function(){done();}, 100);
});
specify("trying to reject then immediately fulfill", function (done) {
var tuple = pending();
var onRejectedCalled = false;
tuple.promise.then(function onFulfilled() {
assert.strictEqual(onRejectedCalled, false);
done();
}, function onRejected() {
onRejectedCalled = true;
});
tuple.reject(dummy);
tuple.fulfill(dummy);
setTimeout(function(){done();}, 100);
});
specify("trying to reject then fulfill, delayed", function (done) {
var tuple = pending();
var onRejectedCalled = false;
tuple.promise.then(function onFulfilled() {
assert.strictEqual(onRejectedCalled, false);
done();
}, function onRejected() {
onRejectedCalled = true;
});
setTimeout(function () {
tuple.reject(dummy);
tuple.fulfill(dummy);
}, 50);
setTimeout(function(){done();}, 100);
});
specify("trying to reject immediately then fulfill delayed", function (done) {
var tuple = pending();
var onRejectedCalled = false;
tuple.promise.then(function onFulfilled() {
assert.strictEqual(onRejectedCalled, false);
done();
}, function onRejected() {
onRejectedCalled = true;
});
tuple.reject(dummy);
setTimeout(function () {
tuple.fulfill(dummy);
}, 50);
setTimeout(function(){done();}, 100);
});
});
});