11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This tests the promise-related n-api calls
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst test_promise = require(`./build/${common.buildType}/test_promise`);
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// A resolution
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const expected_result = 42;
131cb0ef41Sopenharmony_ci  const promise = test_promise.createPromise();
141cb0ef41Sopenharmony_ci  promise.then(
151cb0ef41Sopenharmony_ci    common.mustCall(function(result) {
161cb0ef41Sopenharmony_ci      assert.strictEqual(result, expected_result);
171cb0ef41Sopenharmony_ci    }),
181cb0ef41Sopenharmony_ci    common.mustNotCall());
191cb0ef41Sopenharmony_ci  test_promise.concludeCurrentPromise(expected_result, true);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// A rejection
231cb0ef41Sopenharmony_ci{
241cb0ef41Sopenharmony_ci  const expected_result = 'It\'s not you, it\'s me.';
251cb0ef41Sopenharmony_ci  const promise = test_promise.createPromise();
261cb0ef41Sopenharmony_ci  promise.then(
271cb0ef41Sopenharmony_ci    common.mustNotCall(),
281cb0ef41Sopenharmony_ci    common.mustCall(function(result) {
291cb0ef41Sopenharmony_ci      assert.strictEqual(result, expected_result);
301cb0ef41Sopenharmony_ci    }));
311cb0ef41Sopenharmony_ci  test_promise.concludeCurrentPromise(expected_result, false);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// Chaining
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  const expected_result = 'chained answer';
371cb0ef41Sopenharmony_ci  const promise = test_promise.createPromise();
381cb0ef41Sopenharmony_ci  promise.then(
391cb0ef41Sopenharmony_ci    common.mustCall(function(result) {
401cb0ef41Sopenharmony_ci      assert.strictEqual(result, expected_result);
411cb0ef41Sopenharmony_ci    }),
421cb0ef41Sopenharmony_ci    common.mustNotCall());
431cb0ef41Sopenharmony_ci  test_promise.concludeCurrentPromise(Promise.resolve('chained answer'), true);
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciconst promiseTypeTestPromise = test_promise.createPromise();
471cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise(promiseTypeTestPromise), true);
481cb0ef41Sopenharmony_citest_promise.concludeCurrentPromise(undefined, true);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciconst rejectPromise = Promise.reject(-1);
511cb0ef41Sopenharmony_ciconst expected_reason = -1;
521cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise(rejectPromise), true);
531cb0ef41Sopenharmony_cirejectPromise.catch((reason) => {
541cb0ef41Sopenharmony_ci  assert.strictEqual(reason, expected_reason);
551cb0ef41Sopenharmony_ci});
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise(2.4), false);
581cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise('I promise!'), false);
591cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise(undefined), false);
601cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise(null), false);
611cb0ef41Sopenharmony_ciassert.strictEqual(test_promise.isPromise({}), false);
62