mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:07:45 +00:00
LibJS: Implement the Promise.withResolvers proposal
https://github.com/tc39/proposal-promise-with-resolvers
This commit is contained in:
parent
705e96568c
commit
36d156428b
4 changed files with 87 additions and 1 deletions
|
@ -0,0 +1,56 @@
|
|||
describe("errors", () => {
|
||||
test("this value must be a constructor", () => {
|
||||
expect(() => {
|
||||
Promise.withResolvers.call(Symbol.hasInstance);
|
||||
}).toThrowWithMessage(TypeError, "Symbol(Symbol.hasInstance) is not a constructor");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Promise.withResolvers).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("returned promise is a Promise", () => {
|
||||
const { promise, resolve, reject } = Promise.withResolvers();
|
||||
expect(promise).toBeInstanceOf(Promise);
|
||||
});
|
||||
|
||||
test("returned resolve/reject are unary functions", () => {
|
||||
const { promise, resolve, reject } = Promise.withResolvers();
|
||||
|
||||
expect(resolve).toBeInstanceOf(Function);
|
||||
expect(resolve).toHaveLength(1);
|
||||
|
||||
expect(reject).toBeInstanceOf(Function);
|
||||
expect(reject).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("returned promise can be resolved", () => {
|
||||
const { promise, resolve, reject } = Promise.withResolvers();
|
||||
|
||||
let fulfillmentValue = null;
|
||||
promise.then(value => {
|
||||
fulfillmentValue = value;
|
||||
});
|
||||
|
||||
resolve("Some value");
|
||||
runQueuedPromiseJobs();
|
||||
|
||||
expect(fulfillmentValue).toBe("Some value");
|
||||
});
|
||||
|
||||
test("returned promise can be rejected", () => {
|
||||
const { promise, resolve, reject } = Promise.withResolvers();
|
||||
|
||||
let rejectionReason = null;
|
||||
promise.catch(value => {
|
||||
rejectionReason = value;
|
||||
});
|
||||
|
||||
reject("Some value");
|
||||
runQueuedPromiseJobs();
|
||||
|
||||
expect(rejectionReason).toBe("Some value");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue