summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-08-21 13:41:32 -0400
committerLinus Groh <mail@linusgroh.de>2021-08-21 23:08:49 +0100
commitdee3b7b8c9e62e405f856ea7422c5e64949d7b44 (patch)
tree4e9dc1e67792f50d8bee3d0c0a53d96e9f85b0de /Userland/Libraries/LibJS/Tests
parentcdf2854fdf58092ffbb1c706a4f2ace20d660c6c (diff)
downloadserenity-dee3b7b8c9e62e405f856ea7422c5e64949d7b44.zip
LibJS: Implement Promise.all on the Promise constructor
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.all.js116
1 files changed, 116 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.all.js b/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.all.js
new file mode 100644
index 0000000000..ff18a8dbfa
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Promise/Promise.all.js
@@ -0,0 +1,116 @@
+test("length is 1", () => {
+ expect(Promise.all).toHaveLength(1);
+});
+
+describe("normal behavior", () => {
+ test("returns a Promise", () => {
+ const promise = Promise.all();
+ expect(promise).toBeInstanceOf(Promise);
+ });
+
+ test("resolve", () => {
+ const promise1 = Promise.resolve(3);
+ const promise2 = 42;
+ const promise3 = new Promise((resolve, reject) => {
+ resolve("foo");
+ });
+
+ let resolvedValues = null;
+ let wasRejected = false;
+
+ Promise.all([promise1, promise2, promise3]).then(
+ values => {
+ resolvedValues = values;
+ },
+ () => {
+ wasRejected = true;
+ }
+ );
+
+ runQueuedPromiseJobs();
+ expect(resolvedValues).toEqual([3, 42, "foo"]);
+ expect(wasRejected).toBeFalse();
+ });
+
+ test("reject", () => {
+ const promise1 = Promise.resolve(3);
+ const promise2 = 42;
+ const promise3 = new Promise((resolve, reject) => {
+ reject("foo");
+ });
+
+ let rejectionReason = null;
+ let wasResolved = false;
+
+ Promise.all([promise1, promise2, promise3]).then(
+ () => {
+ wasResolved = true;
+ },
+ reason => {
+ rejectionReason = reason;
+ }
+ );
+
+ runQueuedPromiseJobs();
+ expect(rejectionReason).toBe("foo");
+ expect(wasResolved).toBeFalse();
+ });
+});
+
+describe("exceptional behavior", () => {
+ test("cannot invoke capabilities executor twice", () => {
+ function fn() {}
+
+ expect(() => {
+ function promise(executor) {
+ executor(fn, fn);
+ executor(fn, fn);
+ }
+
+ Promise.all.call(promise, []);
+ }).toThrow(TypeError);
+
+ expect(() => {
+ function promise(executor) {
+ executor(fn, undefined);
+ executor(fn, fn);
+ }
+
+ Promise.all.call(promise, []);
+ }).toThrow(TypeError);
+
+ expect(() => {
+ function promise(executor) {
+ executor(undefined, fn);
+ executor(fn, fn);
+ }
+
+ Promise.all.call(promise, []);
+ }).toThrow(TypeError);
+ });
+
+ test("promise without resolve method", () => {
+ expect(() => {
+ function promise(executor) {}
+ Promise.all.call(promise, []);
+ }).toThrow(TypeError);
+ });
+
+ test("no parameters", () => {
+ let rejectionReason = null;
+ Promise.all().catch(reason => {
+ rejectionReason = reason;
+ });
+ runQueuedPromiseJobs();
+ expect(rejectionReason).toBeInstanceOf(TypeError);
+ });
+
+ test("non-iterable", () => {
+ let rejectionReason = null;
+ Promise.all(1).catch(reason => {
+ rejectionReason = reason;
+ });
+ runQueuedPromiseJobs();
+ expect(rejectionReason).toBeInstanceOf(TypeError);
+ });
+});