-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathindex.test.ts
More file actions
60 lines (48 loc) · 1.96 KB
/
Copy pathindex.test.ts
File metadata and controls
60 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { describe, expect, it, vi, beforeEach } from "vitest";
import type { ClientApi } from "../client/index.js";
const mockMutation = vi.fn();
const mockClient = { mutation: mockMutation };
vi.mock("convex-svelte", () => ({
useConvexClient: vi.fn(() => mockClient),
}));
vi.mock("../client/upload.js", () => ({
uploadWithProgress: vi.fn(),
}));
import { useUploadFile } from "./index.js";
import { uploadWithProgress } from "../client/upload.js";
const mockApi = {
generateUploadUrl: "generateUploadUrl" as unknown,
syncMetadata: "syncMetadata" as unknown,
} as Pick<ClientApi, "generateUploadUrl" | "syncMetadata">;
describe("svelte useUploadFile", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("calls generateUploadUrl, uploads, and syncs metadata", async () => {
const url = "https://upload.example.com";
const key = "file-key-123";
mockMutation.mockResolvedValueOnce({ url, key });
mockMutation.mockResolvedValueOnce(undefined);
const upload = useUploadFile(mockApi);
const file = new File(["content"], "test.txt", { type: "text/plain" });
const result = await upload(file);
expect(mockMutation).toHaveBeenCalledWith(mockApi.generateUploadUrl, {
fileSize: file.size,
contentType: "text/plain",
});
expect(uploadWithProgress).toHaveBeenCalledWith(url, file, undefined);
expect(mockMutation).toHaveBeenCalledWith(mockApi.syncMetadata, { key });
expect(result).toBe(key);
});
it("forwards progress callback to uploadWithProgress", async () => {
const url = "https://upload.example.com";
const key = "file-key-456";
mockMutation.mockResolvedValueOnce({ url, key });
mockMutation.mockResolvedValueOnce(undefined);
const upload = useUploadFile(mockApi);
const file = new File(["content"], "test.txt", { type: "text/plain" });
const onProgress = vi.fn();
await upload(file, { onProgress });
expect(uploadWithProgress).toHaveBeenCalledWith(url, file, onProgress);
});
});