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

useFileDownload.test.ts

50 lines1.8 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3​
4import { useFileDownload } from "./useFileDownload";
5​
6const state = vi.hoisted(() => ({
7 canRead: true,
8 createUrl: vi.fn(async () => ({
9 _tag: "Success",
10 value: { relativeUrl: "/api/assets/t/a.zip" },
11 })),
12 download: vi.fn(async () => {}),
13}));
14​
15vi.mock("gt-react", async (importOriginal) => {
16 const gt = await importOriginal<typeof import("gt-react")>();
17 // GT's testing entry renders the English source with no provider and no React.
18 const sourceGT = await (await import("gt-react/testing")).getGT();
19 return { ...gt, useGT: () => sourceGT };
20});
21vi.mock(import("~/state/session"), async (original) => ({
22 ...(await original()),
23 useEnvironmentScope: () => state.canRead,
24 readPreparedConnection: () => ({ httpBaseUrl: "https://host.example/" }) as never,
25}));
26vi.mock("~/state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => state.createUrl }));
27vi.mock("../media/mediaContent", () => ({ downloadMedia: state.download }));
28​
29const environmentId = EnvironmentId.make("env");
30​
31it("saves the host file through an attachment asset URL, and only with file access", async () => {
32 const downloaded = Promise.withResolvers<void>();
33 state.download.mockImplementationOnce(async () => downloaded.resolve());
34 useFileDownload(environmentId)!("/repo", Promise.resolve("/repo/dist/a.zip"));
35 await downloaded.promise;
36 expect(state.download).toHaveBeenCalledWith("https://host.example/api/assets/t/a.zip", "a.zip");
37 expect(state.createUrl).toHaveBeenCalledWith({
38 environmentId,
39 input: {
40 resource: {
41 _tag: "draft-workspace-file",
42 cwd: "/repo",
43 path: "/repo/dist/a.zip",
44 disposition: "attachment",
45 },
46 },
47 });
48 state.canRead = false;
49 expect(useFileDownload(environmentId)).toBeNull();
50});