-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathindex.test.ts
More file actions
72 lines (59 loc) · 2.27 KB
/
Copy pathindex.test.ts
File metadata and controls
72 lines (59 loc) · 2.27 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
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, expect, it, vi, beforeEach } from "vitest";
import type { ClientApi } from "../client/index.js";
const mockGenerateUploadUrl = vi.fn();
const mockSyncMetadata = vi.fn();
vi.mock("convex/react", () => ({
useMutation: vi.fn((ref: unknown) => {
if (ref === "generateUploadUrl") return mockGenerateUploadUrl;
if (ref === "syncMetadata") return mockSyncMetadata;
return vi.fn();
}),
}));
vi.mock("react", async () => {
const actual = await vi.importActual<typeof import("react")>("react");
return {
...actual,
useCallback: vi.fn((fn: unknown) => fn),
};
});
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("react useUploadFile", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("calls generateUploadUrl, uploads, and syncs metadata", async () => {
const url = "https://upload.example.com";
const key = "file-key-123";
mockGenerateUploadUrl.mockResolvedValueOnce({ url, key });
mockSyncMetadata.mockResolvedValueOnce(undefined);
const upload = useUploadFile(mockApi);
const file = new File(["content"], "test.txt", { type: "text/plain" });
const result = await upload(file);
expect(mockGenerateUploadUrl).toHaveBeenCalledWith({
fileSize: file.size,
contentType: "text/plain",
});
expect(uploadWithProgress).toHaveBeenCalledWith(url, file, undefined);
expect(mockSyncMetadata).toHaveBeenCalledWith({ key });
expect(result).toBe(key);
});
it("forwards progress callback to uploadWithProgress", async () => {
const url = "https://upload.example.com";
const key = "file-key-456";
mockGenerateUploadUrl.mockResolvedValueOnce({ url, key });
mockSyncMetadata.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);
});
});