183 lines
5.9 KiB
JavaScript
183 lines
5.9 KiB
JavaScript
|
"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 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
|
||
|
|
||
|
describe("2.2.4: `onFulfilled` or `onRejected` must not be called until the execution context stack contains only " +
|
||
|
"platform code.", function () {
|
||
|
describe("`then` returns before the promise becomes fulfilled or rejected", function () {
|
||
|
testFulfilled(dummy, function (promise, done) {
|
||
|
var thenHasReturned = false;
|
||
|
|
||
|
promise.then(function onFulfilled() {
|
||
|
assert.strictEqual(thenHasReturned, true);
|
||
|
done();
|
||
|
});
|
||
|
|
||
|
thenHasReturned = true;
|
||
|
});
|
||
|
testRejected(dummy, function (promise, done) {
|
||
|
var thenHasReturned = false;
|
||
|
|
||
|
promise.then(null, function onRejected() {
|
||
|
assert.strictEqual(thenHasReturned, true);
|
||
|
done();
|
||
|
});
|
||
|
|
||
|
thenHasReturned = true;
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe("Clean-stack execution ordering tests (fulfillment case)", function () {
|
||
|
specify("when `onFulfilled` is added immediately before the promise is fulfilled",
|
||
|
function () {
|
||
|
var tuple = pending();
|
||
|
var onFulfilledCalled = false;
|
||
|
|
||
|
tuple.promise.then(function onFulfilled() {
|
||
|
onFulfilledCalled = true;
|
||
|
});
|
||
|
|
||
|
tuple.fulfill(dummy);
|
||
|
|
||
|
assert.strictEqual(onFulfilledCalled, false);
|
||
|
});
|
||
|
|
||
|
specify("when `onFulfilled` is added immediately after the promise is fulfilled",
|
||
|
function () {
|
||
|
var tuple = pending();
|
||
|
var onFulfilledCalled = false;
|
||
|
|
||
|
tuple.fulfill(dummy);
|
||
|
|
||
|
tuple.promise.then(function onFulfilled() {
|
||
|
onFulfilledCalled = true;
|
||
|
});
|
||
|
|
||
|
assert.strictEqual(onFulfilledCalled, false);
|
||
|
});
|
||
|
|
||
|
specify("when one `onFulfilled` is added inside another `onFulfilled`", function (done) {
|
||
|
var promise = fulfilled();
|
||
|
var firstOnFulfilledFinished = false;
|
||
|
|
||
|
promise.then(function () {
|
||
|
promise.then(function () {
|
||
|
assert.strictEqual(firstOnFulfilledFinished, true);
|
||
|
done();
|
||
|
});
|
||
|
firstOnFulfilledFinished = true;
|
||
|
});
|
||
|
});
|
||
|
|
||
|
specify("when `onFulfilled` is added inside an `onRejected`", function (done) {
|
||
|
var promise = rejected();
|
||
|
var promise2 = fulfilled();
|
||
|
var firstOnRejectedFinished = false;
|
||
|
|
||
|
promise.then(null, function () {
|
||
|
promise2.then(function () {
|
||
|
assert.strictEqual(firstOnRejectedFinished, true);
|
||
|
done();
|
||
|
});
|
||
|
firstOnRejectedFinished = true;
|
||
|
});
|
||
|
});
|
||
|
|
||
|
specify("when the promise is fulfilled asynchronously", function (done) {
|
||
|
var tuple = pending();
|
||
|
var firstStackFinished = false;
|
||
|
|
||
|
setTimeout(function () {
|
||
|
tuple.fulfill(dummy);
|
||
|
firstStackFinished = true;
|
||
|
}, 0);
|
||
|
|
||
|
tuple.promise.then(function () {
|
||
|
assert.strictEqual(firstStackFinished, true);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe("Clean-stack execution ordering tests (rejection case)", function () {
|
||
|
specify("when `onRejected` is added immediately before the promise is rejected",
|
||
|
function () {
|
||
|
var tuple = pending();
|
||
|
var onRejectedCalled = false;
|
||
|
|
||
|
tuple.promise.then(null, function onRejected() {
|
||
|
onRejectedCalled = true;
|
||
|
});
|
||
|
|
||
|
tuple.reject(dummy);
|
||
|
|
||
|
assert.strictEqual(onRejectedCalled, false);
|
||
|
});
|
||
|
|
||
|
specify("when `onRejected` is added immediately after the promise is rejected",
|
||
|
function () {
|
||
|
var tuple = pending();
|
||
|
var onRejectedCalled = false;
|
||
|
|
||
|
tuple.reject(dummy);
|
||
|
|
||
|
tuple.promise.then(null, function onRejected() {
|
||
|
onRejectedCalled = true;
|
||
|
});
|
||
|
|
||
|
assert.strictEqual(onRejectedCalled, false);
|
||
|
});
|
||
|
|
||
|
specify("when `onRejected` is added inside an `onFulfilled`", function (done) {
|
||
|
var promise = fulfilled();
|
||
|
var promise2 = rejected();
|
||
|
var firstOnFulfilledFinished = false;
|
||
|
|
||
|
promise.then(function () {
|
||
|
promise2.then(null, function () {
|
||
|
assert.strictEqual(firstOnFulfilledFinished, true);
|
||
|
done();
|
||
|
});
|
||
|
firstOnFulfilledFinished = true;
|
||
|
});
|
||
|
});
|
||
|
|
||
|
specify("when one `onRejected` is added inside another `onRejected`", function (done) {
|
||
|
var promise = rejected();
|
||
|
var firstOnRejectedFinished = false;
|
||
|
|
||
|
promise.then(null, function () {
|
||
|
promise.then(null, function () {
|
||
|
assert.strictEqual(firstOnRejectedFinished, true);
|
||
|
done();
|
||
|
});
|
||
|
firstOnRejectedFinished = true;
|
||
|
});
|
||
|
});
|
||
|
|
||
|
specify("when the promise is rejected asynchronously", function (done) {
|
||
|
var tuple = pending();
|
||
|
var firstStackFinished = false;
|
||
|
|
||
|
setTimeout(function () {
|
||
|
tuple.reject(dummy);
|
||
|
firstStackFinished = true;
|
||
|
}, 0);
|
||
|
|
||
|
tuple.promise.then(null, function () {
|
||
|
assert.strictEqual(firstStackFinished, true);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|