Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2
3import type { ComposerFileAttachment } from "../../composerDraftStore";
4import {
5 attachVideoThumbnail,
6 buildExpandedImagePreview,
7 downloadVideoPreview,
8} from "./ExpandedImagePreview";
9
10describe("buildExpandedImagePreview", () => {
11 it("builds a video preview for a local video attachment", () => {
12 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
13 const attachment: ComposerFileAttachment = {
14 type: "file",
15 id: "video-1",
16 name: file.name,
17 mimeType: file.type,
18 sizeBytes: file.size,
19 file,
20 };
21
22 const preview = buildExpandedImagePreview([attachment], attachment.id);
23
24 expect(preview).toMatchObject({
25 images: [{ name: "demo.mp4", type: "video" }],
26 index: 0,
27 });
28 expect(preview?.images[0]?.src).toMatch(/^blob:/);
29 URL.revokeObjectURL(preview?.images[0]?.src ?? "");
30 });
31
32 it("releases a video thumbnail URL when detached", async () => {
33 const video = { src: "" } as HTMLVideoElement;
34 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
35
36 const detach = attachVideoThumbnail(video, file);
37 const url = video.src;
38
39 expect((await fetch(url)).ok).toBe(true);
40 detach();
41 await expect(fetch(url)).rejects.toThrow();
42 });
43
44 it("downloads a video through a local blob URL", async () => {
45 vi.useFakeTimers();
46 const source = URL.createObjectURL(new Blob([new Uint8Array([1, 2, 3])]));
47 const click = vi.fn();
48 const anchor = { href: "", download: "", click };
49 vi.stubGlobal("document", { createElement: () => anchor });
50
51 try {
52 await downloadVideoPreview(source, "demo.mp4");
53
54 expect(anchor.download).toBe("demo.mp4");
55 expect(anchor.href).toMatch(/^blob:/);
56 expect(anchor.href).not.toBe(source);
57 expect(click).toHaveBeenCalledOnce();
58 expect((await fetch(anchor.href)).ok).toBe(true);
59
60 await vi.runAllTimersAsync();
61 await expect(fetch(anchor.href)).rejects.toThrow();
62 } finally {
63 URL.revokeObjectURL(source);
64 vi.useRealTimers();
65 vi.unstubAllGlobals();
66 }
67 });
68});