Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2
3import { createClerkOAuthCancel } from "./clerkOAuthCancel.ts";
4
5const deferredOpen = () => {
6 const resolvers: Array<(value: { callbackUrl: string }) => void> = [];
7 const opened: Array<Promise<{ callbackUrl: string }>> = [];
8 const open = (_url: string) => {
9 const promise = new Promise<{ callbackUrl: string }>((resolve) => {
10 resolvers.push(resolve);
11 });
12 opened.push(promise);
13 return promise;
14 };
15 return { open, resolvers, opened };
16};
17
18const outcome = (promise: Promise<{ callbackUrl: string }>) =>
19 promise.then(
20 ({ callbackUrl }) => `resolved ${callbackUrl}`,
21 (error: Error) => `rejected ${error.message}`,
22 );
23
24describe("createClerkOAuthCancel", () => {
25 it("cancel rejects the pending flow and reports it is no longer pending", async () => {
26 const oauth = createClerkOAuthCancel();
27 const changes: boolean[] = [];
28 oauth.onPendingChange((pending) => changes.push(pending));
29 const { open } = deferredOpen();
30
31 const flow = outcome(oauth.wrapOpen(open)("https://accounts.example/a"));
32 assert.isTrue(oauth.isPending());
33 oauth.cancel();
34
35 assert.strictEqual(await flow, "rejected Clerk: OAuth flow was cancelled.");
36 assert.isFalse(oauth.isPending());
37 assert.deepStrictEqual(changes, [true, false]);
38 });
39
40 it("a new attempt replaces the pending one, and the old result cannot clear it", async () => {
41 const oauth = createClerkOAuthCancel();
42 const { open, resolvers, opened } = deferredOpen();
43 const wrapped = oauth.wrapOpen(open);
44
45 const first = outcome(wrapped("https://accounts.example/a"));
46 const second = outcome(wrapped("https://accounts.example/b"));
47 assert.strictEqual(await first, "rejected Clerk: OAuth flow was cancelled.");
48
49 resolvers[0]?.({ callbackUrl: "t3code://app/?a" });
50 // Two hops, so the wrapper's own `.then().finally()` on this result has run.
51 await opened[0]?.then(() => undefined).then(() => undefined);
52 assert.isTrue(oauth.isPending());
53
54 resolvers[1]?.({ callbackUrl: "t3code://app/?b" });
55 assert.strictEqual(await second, "resolved t3code://app/?b");
56 assert.isFalse(oauth.isPending());
57 });
58});