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

localAttachmentPreview.test.ts

95 lines3.3 KB

1import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3const mocks = vi.hoisted(() => ({
4 retain: vi.fn(),
5 share: vi.fn(),
6 exists: vi.fn(),
7}));
8​
9vi.mock("./composerAttachmentPreviewRetention", () => ({
10 retainComposerAttachmentFileForPreview: mocks.retain,
11}));
12vi.mock("./attachmentDownload", () => ({ shareLocalAttachment: mocks.share }));
13vi.mock("expo-file-system", () => ({
14 File: class {
15 constructor(readonly uri: string) {}
16 get exists(): boolean {
17 return mocks.exists(this.uri);
18 }
19 },
20 Paths: {
21 document: {
22 uri: "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents/",
23 },
24 },
25}));
26​
27import { loadLocalAttachmentPreview } from "./localAttachmentPreview";
28​
29const attachment = {
30 type: "file" as const,
31 id: "draft-video",
32 name: "clip.mov",
33 mimeType: "video/quicktime",
34 sizeBytes: 12,
35 fileUri:
36 "file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/33333333-3333-4333-8333-333333333333-clip.mov",
37};
38​
39beforeEach(() => {
40 mocks.retain.mockReset();
41 mocks.share.mockReset();
42 mocks.exists.mockReset();
43 mocks.retain.mockImplementation(() => vi.fn());
44 mocks.exists.mockReturnValue(true);
45 mocks.share.mockResolvedValue(undefined);
46});
47​
48describe("loadLocalAttachmentPreview", () => {
49 it("keeps an image MIME type when its filename has a video extension", async () => {
50 const image = {
51 ...attachment,
52 type: "image" as const,
53 name: "photo.mov",
54 mimeType: "image/png",
55 previewUri: attachment.fileUri,
56 };
57 const preview = await loadLocalAttachmentPreview(image, new AbortController().signal);
58 await preview!.share(new AbortController().signal);
59 expect(mocks.share).toHaveBeenCalledWith(
60 expect.objectContaining({
61 attachment: { name: "photo.mov", mimeType: "image/png" },
62 }),
63 );
64 preview!.dispose();
65 });
66 it("resolves the current iOS container and releases its playback lease once", async () => {
67 const preview = await loadLocalAttachmentPreview(attachment, new AbortController().signal);
68 expect(preview?.uri).toContain("/22222222-2222-4222-8222-222222222222/Documents/");
69 expect(mocks.retain).toHaveBeenCalledWith(attachment);
70 const release = mocks.retain.mock.results[0]!.value;
71 expect(release).not.toHaveBeenCalled();
72 preview?.dispose();
73 preview?.dispose();
74 expect(release).toHaveBeenCalledTimes(1);
75 });
76​
77 it.each([undefined, "share-button"])(
78 "keeps a separate share lease after playback closes (source: %s)",
79 async (sourceIdentifier) => {
80 const shared = Promise.withResolvers<void>();
81 mocks.share.mockReturnValue(shared.promise);
82 const preview = await loadLocalAttachmentPreview(attachment, new AbortController().signal);
83 const share = preview!.share(new AbortController().signal, sourceIdentifier);
84 expect(mocks.retain).toHaveBeenCalledTimes(2);
85 const releasePlayback = mocks.retain.mock.results[0]!.value;
86 const releaseShare = mocks.retain.mock.results[1]!.value;
87 preview!.dispose();
88 expect(releasePlayback).toHaveBeenCalledTimes(1);
89 expect(releaseShare).not.toHaveBeenCalled();
90 shared.resolve();
91 await share;
92 expect(releaseShare).toHaveBeenCalledTimes(1);
93 },
94 );
95});