|
3 | 3 | *
|
4 | 4 | * (c) 2024 Feedzai
|
5 | 5 | */
|
6 |
| -import { makeCancelable } from "src/functions"; |
| 6 | +import { AbortPromiseError, makeCancelable, wait } from "src/functions"; |
| 7 | + |
| 8 | +async function expectAbort(cancelable: ReturnType<typeof makeCancelable>) { |
| 9 | + try { |
| 10 | + await cancelable.promise; |
| 11 | + throw new Error("Promise should have been rejected"); |
| 12 | + } catch (error) { |
| 13 | + expect(error).to.be.instanceOf(AbortPromiseError); |
| 14 | + if (error instanceof AbortPromiseError) { |
| 15 | + expect(error.message).to.equal("Promise was aborted"); |
| 16 | + } |
| 17 | + } |
| 18 | +} |
7 | 19 |
|
8 | 20 | describe("makeCancelable", () => {
|
9 |
| - it("should return an object with a promise and a cancel function", () => { |
10 |
| - const promise = new Promise((resolve) => setTimeout(resolve, 100)); |
| 21 | + // Configure Cypress to not fail on unhandled promise rejections |
| 22 | + before(() => { |
| 23 | + cy.on("uncaught:exception", (err) => { |
| 24 | + if (err.name === "AbortError") { |
| 25 | + return false; |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should reject with AbortPromiseError if cancelled just before resolution", async () => { |
| 31 | + const promise = wait(10); |
| 32 | + const cancelable = makeCancelable(promise); |
| 33 | + |
| 34 | + setTimeout(() => cancelable.cancel(), 5); |
| 35 | + |
| 36 | + await expectAbort(cancelable); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should return an object with a promise, cancel function, and isCancelled function", () => { |
| 40 | + const promise = wait(25); |
11 | 41 | const cancelable = makeCancelable(promise);
|
12 | 42 | expect(cancelable).to.be.an("object");
|
13 | 43 | expect(cancelable.promise).to.be.a("promise");
|
14 | 44 | expect(cancelable.cancel).to.be.a("function");
|
| 45 | + expect(cancelable.isCancelled).to.be.a("function"); |
15 | 46 | });
|
16 | 47 |
|
17 | 48 | it("should resolve the promise if not cancelled", async () => {
|
18 |
| - const promise = new Promise((resolve) => setTimeout(resolve, 100)); |
| 49 | + const value = "test value"; |
| 50 | + const promise = new Promise((resolve) => setTimeout(() => resolve(value), 25)); |
19 | 51 | const cancelable = makeCancelable(promise);
|
20 | 52 | const result = await cancelable.promise;
|
21 |
| - expect(result).to.be.undefined; // Or any other expected resolved value |
| 53 | + expect(result).to.equal(value); |
22 | 54 | });
|
23 | 55 |
|
24 |
| - it("should reject the promise with { isCanceled: true } if cancelled", async () => { |
25 |
| - const promise = new Promise((resolve) => setTimeout(resolve, 100)); |
| 56 | + it("should reject with AbortPromiseError when cancelled", async () => { |
| 57 | + const promise = wait(25); |
26 | 58 | const cancelable = makeCancelable(promise);
|
27 | 59 | cancelable.cancel();
|
| 60 | + |
28 | 61 | try {
|
29 | 62 | await cancelable.promise;
|
30 |
| - } catch (error) { |
31 |
| - expect(error).to.have.property("isCanceled", true); |
| 63 | + throw new Error("Promise should have been rejected"); |
| 64 | + } catch (error: unknown) { |
| 65 | + expect(error).to.be.instanceOf(AbortPromiseError); |
| 66 | + if (error instanceof AbortPromiseError) { |
| 67 | + expect(error.message).to.equal("Promise was aborted"); |
| 68 | + } |
32 | 69 | }
|
33 | 70 | });
|
34 | 71 |
|
35 |
| - it("should not resolve or reject the promise after being cancelled", async () => { |
36 |
| - cy.window().then(async (win) => { |
37 |
| - const promise = new win.Promise((resolve) => win.setTimeout(resolve, 100)); |
38 |
| - const cancelable = makeCancelable(promise); |
39 |
| - cancelable.cancel(); |
40 |
| - const racePromise = win.Promise.race([ |
41 |
| - cancelable.promise.then(() => "resolved"), |
42 |
| - new win.Promise((resolve) => win.setTimeout(() => resolve("not-resolved"), 200)), |
43 |
| - ]); |
| 72 | + it("should handle rejection from the original promise", async () => { |
| 73 | + const error = new Error("Original promise error"); |
| 74 | + const promise = new Promise((_, reject) => setTimeout(() => reject(error), 25)); |
| 75 | + const cancelable = makeCancelable(promise); |
| 76 | + |
| 77 | + try { |
| 78 | + await cancelable.promise; |
| 79 | + throw new Error("Promise should have been rejected"); |
| 80 | + } catch (caughtError: unknown) { |
| 81 | + expect(caughtError).to.equal(error); |
| 82 | + } |
| 83 | + }); |
44 | 84 |
|
45 |
| - try { |
46 |
| - const result = await racePromise; |
| 85 | + it("should not reject with original error if cancelled", async () => { |
| 86 | + const error = new Error("Original promise error"); |
| 87 | + const promise = new Promise((_, reject) => setTimeout(() => reject(error), 25)); |
| 88 | + const cancelable = makeCancelable(promise); |
| 89 | + cancelable.cancel(); |
47 | 90 |
|
48 |
| - expect(result).to.equal("not-resolved"); |
49 |
| - } catch (error) { |
50 |
| - console.log(error); |
| 91 | + try { |
| 92 | + await cancelable.promise; |
| 93 | + throw new Error("Promise should have been rejected"); |
| 94 | + } catch (caughtError: unknown) { |
| 95 | + expect(caughtError).to.be.instanceOf(AbortPromiseError); |
| 96 | + if (caughtError instanceof AbortPromiseError) { |
| 97 | + expect(caughtError.message).to.equal("Promise was aborted"); |
51 | 98 | }
|
52 |
| - }); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle multiple cancel calls", async () => { |
| 103 | + const promise = wait(25); |
| 104 | + const cancelable = makeCancelable(promise); |
| 105 | + |
| 106 | + cancelable.cancel(); |
| 107 | + cancelable.cancel(); // Second call should be ignored |
| 108 | + |
| 109 | + try { |
| 110 | + await cancelable.promise; |
| 111 | + throw new Error("Promise should have been rejected"); |
| 112 | + } catch (error: unknown) { |
| 113 | + expect(error).to.be.instanceOf(AbortPromiseError); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it("should correctly report cancellation state", async () => { |
| 118 | + const promise = wait(25); |
| 119 | + const cancelable = makeCancelable(promise); |
| 120 | + |
| 121 | + expect(cancelable.isCancelled()).to.be.false; |
| 122 | + cancelable.cancel(); |
| 123 | + expect(cancelable.isCancelled()).to.be.true; |
| 124 | + }); |
| 125 | + |
| 126 | + it("should handle abort error message", async () => { |
| 127 | + const promise = wait(25); |
| 128 | + const cancelable = makeCancelable(promise); |
| 129 | + cancelable.cancel(); |
| 130 | + |
| 131 | + try { |
| 132 | + await cancelable.promise; |
| 133 | + throw new Error("Promise should have been rejected"); |
| 134 | + } catch (error: unknown) { |
| 135 | + expect(error).to.be.instanceOf(AbortPromiseError); |
| 136 | + if (error instanceof AbortPromiseError) { |
| 137 | + expect(error.message).to.equal("Promise was aborted"); |
| 138 | + } |
| 139 | + } |
53 | 140 | });
|
54 | 141 | });
|
0 commit comments