2017-08-24 13:33:26 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../../common');
|
2017-11-06 15:14:51 +00:00
|
|
|
|
|
|
|
// This tests the promise-related n-api calls
|
|
|
|
|
2017-08-24 13:33:26 +03:00
|
|
|
const assert = require('assert');
|
2018-01-23 21:49:25 +11:00
|
|
|
const test_promise = require(`./build/${common.buildType}/test_promise`);
|
2017-08-24 13:33:26 +03:00
|
|
|
|
|
|
|
// 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) {
|
2017-11-06 15:14:51 +00:00
|
|
|
assert.strictEqual(result, expected_result);
|
2017-10-06 15:23:58 -04:00
|
|
|
}),
|
|
|
|
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) {
|
2017-11-06 15:14:51 +00:00
|
|
|
assert.strictEqual(result, expected_result);
|
2017-10-06 15:23:58 -04:00
|
|
|
}));
|
|
|
|
test_promise.concludeCurrentPromise(expected_result, false);
|
|
|
|
}
|
2017-08-24 13:33:26 +03:00
|
|
|
|
|
|
|
// Chaining
|
2017-11-06 15:14:51 +00:00
|
|
|
{
|
|
|
|
const expected_result = 'chained answer';
|
|
|
|
const promise = test_promise.createPromise();
|
|
|
|
promise.then(
|
|
|
|
common.mustCall(function(result) {
|
|
|
|
assert.strictEqual(result, expected_result);
|
|
|
|
}),
|
|
|
|
common.mustNotCall());
|
|
|
|
test_promise.concludeCurrentPromise(Promise.resolve('chained answer'), true);
|
|
|
|
}
|
2017-08-24 13:33:26 +03:00
|
|
|
|
2018-03-08 18:17:46 -05:00
|
|
|
const promiseTypeTestPromise = test_promise.createPromise();
|
|
|
|
assert.strictEqual(test_promise.isPromise(promiseTypeTestPromise), true);
|
|
|
|
test_promise.concludeCurrentPromise(undefined, true);
|
2017-11-23 19:44:20 +08:00
|
|
|
|
|
|
|
const rejectPromise = Promise.reject(-1);
|
|
|
|
const expected_reason = -1;
|
|
|
|
assert.strictEqual(test_promise.isPromise(rejectPromise), true);
|
|
|
|
rejectPromise.catch((reason) => {
|
|
|
|
assert.strictEqual(reason, expected_reason);
|
|
|
|
});
|
|
|
|
|
2017-10-06 15:23:58 -04:00
|
|
|
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);
|