"use strict"; var assert = require("assert"); var adapter = global.adapter; var resolved = adapter.resolved; var rejected = adapter.rejected; var deferred = adapter.deferred; 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 function testPromiseResolution(xFactory, test) { specify("via return from a fulfilled promise", function (done) { var promise = resolved(dummy).then(function onBasePromiseFulfilled() { return xFactory(); }); test(promise, done); }); specify("via return from a rejected promise", function (done) { var promise = rejected(dummy).then(null, function onBasePromiseRejected() { return xFactory(); }); test(promise, done); }); } describe("2.3.2: If `x` is a promise, adopt its state", function () { describe("2.3.2.1: If `x` is pending, `promise` must remain pending until `x` is fulfilled or rejected.", function () { function xFactory() { return deferred().promise; } testPromiseResolution(xFactory, function (promise, done) { var wasFulfilled = false; var wasRejected = false; promise.then( function onPromiseFulfilled() { wasFulfilled = true; }, function onPromiseRejected() { wasRejected = true; } ); setTimeout(function () { assert.strictEqual(wasFulfilled, false); assert.strictEqual(wasRejected, false); done(); }, 100); }); }); describe("2.3.2.2: If/when `x` is fulfilled, fulfill `promise` with the same value.", function () { describe("`x` is already-fulfilled", function () { function xFactory() { return resolved(sentinel); } testPromiseResolution(xFactory, function (promise, done) { promise.then(function onPromiseFulfilled(value) { assert.strictEqual(value, sentinel); done(); }); }); }); describe("`x` is eventually-fulfilled", function () { var d = null; function xFactory() { d = deferred(); setTimeout(function () { d.resolve(sentinel); }, 50); return d.promise; } testPromiseResolution(xFactory, function (promise, done) { promise.then(function onPromiseFulfilled(value) { assert.strictEqual(value, sentinel); done(); }); }); }); }); describe("2.3.2.3: If/when `x` is rejected, reject `promise` with the same reason.", function () { describe("`x` is already-rejected", function () { function xFactory() { return rejected(sentinel); } testPromiseResolution(xFactory, function (promise, done) { promise.then(null, function onPromiseRejected(reason) { assert.strictEqual(reason, sentinel); done(); }); }); }); describe("`x` is eventually-rejected", function () { var d = null; function xFactory() { d = deferred(); setTimeout(function () { d.reject(sentinel); }, 50); return d.promise; } testPromiseResolution(xFactory, function (promise, done) { promise.then(null, function onPromiseRejected(reason) { assert.strictEqual(reason, sentinel); done(); }); }); }); }); });