summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/modules/tests/xpcshell/test_PromiseUtils.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/modules/tests/xpcshell/test_PromiseUtils.js')
-rw-r--r--toolkit/modules/tests/xpcshell/test_PromiseUtils.js105
1 files changed, 105 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 000000000..c3ab839e4
--- /dev/null
+++ b/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
@@ -0,0 +1,105 @@
+ /* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+Components.utils.import("resource://gre/modules/PromiseUtils.jsm");
+Components.utils.import("resource://gre/modules/Timer.jsm");
+Components.utils.import("resource://testing-common/PromiseTestUtils.jsm");
+
+// Tests for PromiseUtils.jsm
+function run_test() {
+ run_next_test();
+}
+
+// Tests for PromiseUtils.defer()
+
+/* Tests for checking the resolve method of the Deferred object
+ * returned by PromiseUtils.defer() */
+add_task(function* test_resolve_string() {
+ let def = PromiseUtils.defer();
+ let expected = "The promise is resolved " + Math.random();
+ def.resolve(expected);
+ let result = yield 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(function* test_resolve_undefined() {
+ let def = PromiseUtils.defer();
+ def.resolve();
+ let result = yield 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(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 = yield 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(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 = yield 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(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);
+ yield 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(function* test_reject_Error() {
+ let def = PromiseUtils.defer();
+ def.reject(new Error("This one rejects"));
+ yield 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(function* test_reject_pending_promise() {
+ let def = PromiseUtils.defer();
+ let p = new Promise((resolve, reject) => {
+ setTimeout(() => resolve(100), 100);
+ });
+ def.reject(p);
+ yield 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(function* test_reject_resolved_promise() {
+ let def = PromiseUtils.defer();
+ let p = new Promise((resolve, reject) => resolve("This resolved"));
+ def.reject(p);
+ yield 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(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);
+ yield Assert.rejects(def.promise, Promise, "Rejection with a rejected promise uses the passed promise itself as the reason of rejection");
+});