2017-08-24 13:33:26 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../../common');
|
|
|
|
const test_promise = require(`./build/${common.buildType}/test_promise`);
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
// A resolution
|
2017-10-06 15:23:58 -04:00
|
|
|
{
|
|
|
|
const expected_result = 42;
|
|
|
|
const promise = test_promise.createPromise();
|
|
|
|
promise.then(
|
|
|
|
common.mustCall(function(result) {
|
|
|
|
assert.strictEqual(result, expected_result,
|
|
|
|
`promise resolved as expected, received ${result}`);
|
|
|
|
}),
|
|
|
|
common.mustNotCall());
|
|
|
|
test_promise.concludeCurrentPromise(expected_result, true);
|
|
|
|
}
|
2017-08-24 13:33:26 +03:00
|
|
|
|
|
|
|
// A rejection
|
2017-10-06 15:23:58 -04:00
|
|
|
{
|
|
|
|
const expected_result = 'It\'s not you, it\'s me.';
|
|
|
|
const promise = test_promise.createPromise();
|
|
|
|
promise.then(
|
|
|
|
common.mustNotCall(),
|
|
|
|
common.mustCall(function(result) {
|
|
|
|
assert.strictEqual(result, expected_result,
|
|
|
|
`promise rejected as expected, received ${result}`);
|
|
|
|
}));
|
|
|
|
test_promise.concludeCurrentPromise(expected_result, false);
|
|
|
|
}
|
2017-08-24 13:33:26 +03:00
|
|
|
|
|
|
|
// Chaining
|
2017-10-06 15:23:58 -04:00
|
|
|
const promise = test_promise.createPromise();
|
2017-08-24 13:33:26 +03:00
|
|
|
promise.then(
|
|
|
|
common.mustCall(function(result) {
|
|
|
|
assert.strictEqual(result, 'chained answer',
|
|
|
|
'resolving with a promise chains properly');
|
|
|
|
}),
|
|
|
|
common.mustNotCall());
|
|
|
|
test_promise.concludeCurrentPromise(Promise.resolve('chained answer'), true);
|
|
|
|
|
2017-10-06 15:23:58 -04:00
|
|
|
assert.strictEqual(test_promise.isPromise(promise), true);
|
|
|
|
assert.strictEqual(test_promise.isPromise(Promise.reject(-1)), true);
|
|
|
|
assert.strictEqual(test_promise.isPromise(2.4), false);
|
|
|
|
assert.strictEqual(test_promise.isPromise('I promise!'), false);
|
|
|
|
assert.strictEqual(test_promise.isPromise(undefined), false);
|
|
|
|
assert.strictEqual(test_promise.isPromise(null), false);
|
|
|
|
assert.strictEqual(test_promise.isPromise({}), false);
|