summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/modules/tests/xpcshell/test_PromiseUtils.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/tests/xpcshell/test_PromiseUtils.js')
-rw-r--r--toolkit/modules/tests/xpcshell/test_PromiseUtils.js141
1 files changed, 141 insertions, 0 deletions
diff --git a/toolkit/modules/tests/xpcshell/test_PromiseUtils.js b/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
new file mode 100644
index 0000000000..cc80248f00
--- /dev/null
+++ b/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
@@ -0,0 +1,141 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+/* eslint-disable mozilla/no-arbitrary-setTimeout */
+
+"use strict";
+
+const { PromiseUtils } = ChromeUtils.importESModule(
+ "resource://gre/modules/PromiseUtils.sys.mjs"
+);
+const { setTimeout } = ChromeUtils.importESModule(
+ "resource://gre/modules/Timer.sys.mjs"
+);
+const { PromiseTestUtils } = ChromeUtils.importESModule(
+ "resource://testing-common/PromiseTestUtils.sys.mjs"
+);
+
+// Tests for PromiseUtils.sys.mjs
+
+// Tests for PromiseUtils.defer()
+
+/* Tests for checking the resolve method of the Deferred object
+ * returned by PromiseUtils.defer() */
+add_task(async function test_resolve_string() {
+ let def = PromiseUtils.defer();
+ let expected = "The promise is resolved " + Math.random();
+ def.resolve(expected);
+ let result = await def.promise;
+ Assert.equal(result, expected, "def.resolve() resolves the promise");
+});
+
+/* Test for the case when undefined is passed to the resolve method
+ * of the Deferred object */
+add_task(async function test_resolve_undefined() {
+ let def = PromiseUtils.defer();
+ def.resolve();
+ let result = await def.promise;
+ Assert.equal(result, undefined, "resolve works with undefined as well");
+});
+
+/* Test when a pending promise is passed to the resolve method
+ * of the Deferred object */
+add_task(async function test_resolve_pending_promise() {
+ let def = PromiseUtils.defer();
+ let expected = 100 + Math.random();
+ let p = new Promise((resolve, reject) => {
+ setTimeout(() => resolve(expected), 100);
+ });
+ def.resolve(p);
+ let result = await def.promise;
+ Assert.equal(
+ result,
+ expected,
+ "def.promise assumed the state of the passed promise"
+ );
+});
+
+/* Test when a resovled promise is passed
+ * to the resolve method of the Deferred object */
+add_task(async function test_resolve_resolved_promise() {
+ let def = PromiseUtils.defer();
+ let expected = "Yeah resolved" + Math.random();
+ let p = new Promise((resolve, reject) => resolve(expected));
+ def.resolve(p);
+ let result = await def.promise;
+ Assert.equal(
+ result,
+ expected,
+ "Resolved promise is passed to the resolve method"
+ );
+});
+
+/* Test for the case when a rejected promise is
+ * passed to the resolve method */
+add_task(async function test_resolve_rejected_promise() {
+ let def = PromiseUtils.defer();
+ let p = new Promise((resolve, reject) =>
+ reject(new Error("There its an rejection"))
+ );
+ def.resolve(p);
+ await Assert.rejects(
+ def.promise,
+ /There its an rejection/,
+ "Settled rejection promise passed to the resolve method"
+ );
+});
+
+/* Test for the checking the reject method of
+ * the Deferred object returned by PromiseUtils.defer() */
+add_task(async function test_reject_Error() {
+ let def = PromiseUtils.defer();
+ def.reject(new Error("This one rejects"));
+ await Assert.rejects(
+ def.promise,
+ /This one rejects/,
+ "reject method with Error for rejection"
+ );
+});
+
+/* Test for the case when a pending Promise is passed to
+ * the reject method of Deferred object */
+add_task(async function test_reject_pending_promise() {
+ let def = PromiseUtils.defer();
+ let p = new Promise((resolve, reject) => {
+ setTimeout(() => resolve(100), 100);
+ });
+ def.reject(p);
+ await Assert.rejects(
+ def.promise,
+ Promise,
+ "Rejection with a pending promise uses the passed promise itself as the reason of rejection"
+ );
+});
+
+/* Test for the case when a resolved Promise
+ * is passed to the reject method */
+add_task(async function test_reject_resolved_promise() {
+ let def = PromiseUtils.defer();
+ let p = new Promise((resolve, reject) => resolve("This resolved"));
+ def.reject(p);
+ await Assert.rejects(
+ def.promise,
+ Promise,
+ "Rejection with a resolved promise uses the passed promise itself as the reason of rejection"
+ );
+});
+
+/* Test for the case when a rejected Promise is
+ * passed to the reject method */
+add_task(async function test_reject_resolved_promise() {
+ PromiseTestUtils.expectUncaughtRejection(/This one rejects/);
+ let def = PromiseUtils.defer();
+ let p = new Promise((resolve, reject) =>
+ reject(new Error("This one rejects"))
+ );
+ def.reject(p);
+ await Assert.rejects(
+ def.promise,
+ Promise,
+ "Rejection with a rejected promise uses the passed promise itself as the reason of rejection"
+ );
+});