Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitHubStarPrompt.test.ts

91 lines3.1 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import type { toastManager } from "./ui/toast";
5​
6const { add, starred } = vi.hoisted(() => ({
7 add: vi.fn<typeof toastManager.add>(),
8 starred: { value: false, duringCheck: () => {} },
9}));
10vi.mock("./ui/toast", () => ({ toastManager: { add, close: vi.fn() } }));
11vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} }));
12vi.mock("../state/sourceControl", () => ({ sourceControlEnvironment: { checkStarred: {} } }));
13vi.mock("@t3tools/client-runtime/state/runtime", () => ({
14 runAtomCommand: async () => {
15 starred.duringCheck();
16 return { _tag: "Success", value: starred.value };
17 },
18}));
19​
20const environmentId = EnvironmentId.make("environment-1");
21​
22function setup({ sent = 0, writable = true, alreadyStarred = false } = {}) {
23 add.mockReset();
24 starred.value = alreadyStarred;
25 starred.duringCheck = () => {};
26 const store = new Map([["t3code:github-star-prompt:sent", String(sent)]]);
27 vi.stubGlobal("window", {
28 localStorage: {
29 getItem: (key: string) => store.get(key) ?? null,
30 setItem: (key: string, value: string) => {
31 if (!writable) throw new Error("QuotaExceededError");
32 store.set(key, value);
33 },
34 },
35 });
36 return store;
38​
39// A fresh module import stands in for an app reload; storage survives it.
40async function startSession() {
41 vi.resetModules();
42 const session = await import("./GitHubStarPrompt");
43 return { send: () => session.recordPromptSent(environmentId) };
45​
46describe("recordPromptSent", () => {
47 it("asks after five prompts, once per session, and never again once closed", async () => {
48 setup();
49 let session = await startSession();
50 for (let sent = 0; sent < 5; sent++) await session.send();
51 expect(add).not.toHaveBeenCalled();
52​
53 await session.send();
54 await session.send();
55 expect(add).toHaveBeenCalledOnce();
56​
57 session = await startSession();
58 await session.send();
59 expect(add).toHaveBeenCalledTimes(2);
60​
61 add.mock.lastCall?.[0].onClose?.();
62 session = await startSession();
63 for (let sent = 0; sent < 10; sent++) await session.send();
64 expect(add).toHaveBeenCalledTimes(2);
65 vi.unstubAllGlobals();
66 });
67​
68 it("never asks when a dismissal could not be saved", async () => {
69 setup({ sent: 5, writable: false });
70 await (await startSession()).send();
71 expect(add).not.toHaveBeenCalled();
72 vi.unstubAllGlobals();
73 });
74​
75 it("never asks once the environment's GitHub account starred the repo", async () => {
76 setup({ sent: 5, alreadyStarred: true });
77 await (await startSession()).send();
78 starred.value = false;
79 await (await startSession()).send();
80 expect(add).not.toHaveBeenCalled();
81 vi.unstubAllGlobals();
82 });
83​
84 it("never asks when another tab dismisses it during the star check", async () => {
85 const store = setup({ sent: 5 });
86 starred.duringCheck = () => store.set("t3code:github-star-prompt:dismissed", "true");
87 await (await startSession()).send();
88 expect(add).not.toHaveBeenCalled();
89 vi.unstubAllGlobals();
90 });
91});