|
| 1 | +import { act, type SyntheticEvent } from "react"; |
| 2 | +import { create, type ReactTestRenderer } from "react-test-renderer"; |
| 3 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 4 | +import { AttachmentImage } from "./AttachmentImage"; |
| 5 | +import { prepareImageForAttachment } from "../../lib/imageCompression"; |
| 6 | + |
| 7 | +vi.mock("../../lib/imageCompression", async (importOriginal) => ({ |
| 8 | + ...(await importOriginal<typeof import("../../lib/imageCompression")>()), |
| 9 | + prepareImageForAttachment: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +let renderer: ReactTestRenderer; |
| 13 | +const fetchImage = vi.fn(); |
| 14 | +const revokeUrl = vi.fn(); |
| 15 | +const decodeError = {} as SyntheticEvent<HTMLImageElement>; |
| 16 | +const jpeg = new File(["jpeg"], "photo.jpg", { type: "image/jpeg" }); |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 20 | + vi.stubGlobal("fetch", fetchImage); |
| 21 | + vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:jpeg"); |
| 22 | + vi.spyOn(URL, "revokeObjectURL").mockImplementation(revokeUrl); |
| 23 | + fetchImage.mockResolvedValue(new Response(new Blob(["heic"]))); |
| 24 | + vi.mocked(prepareImageForAttachment).mockResolvedValue({ |
| 25 | + ok: true, |
| 26 | + file: jpeg, |
| 27 | + recompressed: true, |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +afterEach(async () => { |
| 32 | + await act(() => renderer?.unmount()); |
| 33 | + vi.restoreAllMocks(); |
| 34 | + vi.unstubAllGlobals(); |
| 35 | + vi.clearAllMocks(); |
| 36 | +}); |
| 37 | + |
| 38 | +it.each([ |
| 39 | + { name: "photo.HEIC", mimeType: undefined }, |
| 40 | + { name: "photo", mimeType: "image/heif" }, |
| 41 | +])( |
| 42 | + "converts $name after native decoding fails and releases its preview", |
| 43 | + async ({ name, mimeType }) => { |
| 44 | + await act(() => { |
| 45 | + renderer = create( |
| 46 | + <AttachmentImage src="https://environment.test/photo" name={name} mimeType={mimeType} />, |
| 47 | + ); |
| 48 | + }); |
| 49 | + expect(fetchImage).not.toHaveBeenCalled(); |
| 50 | + await act(() => renderer.root.findByType("img").props.onError(decodeError)); |
| 51 | + expect(renderer.root.findByType("img").props.src).toBe("blob:jpeg"); |
| 52 | + expect(prepareImageForAttachment).toHaveBeenCalledWith( |
| 53 | + expect.objectContaining({ name, type: "image/heic" }), |
| 54 | + 50 * 1024 * 1024, |
| 55 | + ); |
| 56 | + await act(() => renderer.unmount()); |
| 57 | + expect(revokeUrl).toHaveBeenCalledWith("blob:jpeg"); |
| 58 | + }, |
| 59 | +); |
| 60 | + |
| 61 | +it("leaves PNG errors to the caller without fetching or converting", async () => { |
| 62 | + const onError = vi.fn(); |
| 63 | + await act(() => { |
| 64 | + renderer = create(<AttachmentImage src="blob:png" name="photo.png" onError={onError} />); |
| 65 | + }); |
| 66 | + await act(() => renderer.root.findByType("img").props.onError(decodeError)); |
| 67 | + expect(onError).toHaveBeenCalledWith(decodeError); |
| 68 | + expect(fetchImage).not.toHaveBeenCalled(); |
| 69 | +}); |
| 70 | + |
| 71 | +it("reports failed HEIC conversion through the existing error handler", async () => { |
| 72 | + const onError = vi.fn(); |
| 73 | + vi.mocked(prepareImageForAttachment).mockResolvedValue({ ok: false, reason: "unreadable" }); |
| 74 | + await act(() => { |
| 75 | + renderer = create(<AttachmentImage src="blob:heic" name="photo.heif" onError={onError} />); |
| 76 | + }); |
| 77 | + await act(() => renderer.root.findByType("img").props.onError(decodeError)); |
| 78 | + expect(onError).toHaveBeenCalledWith(decodeError); |
| 79 | + expect(URL.createObjectURL).not.toHaveBeenCalled(); |
| 80 | +}); |
| 81 | + |
| 82 | +it("discards an old decode when navigating to a different source", async () => { |
| 83 | + let finishDecode!: (result: Awaited<ReturnType<typeof prepareImageForAttachment>>) => void; |
| 84 | + vi.mocked(prepareImageForAttachment).mockReturnValue( |
| 85 | + new Promise((resolve) => { |
| 86 | + finishDecode = resolve; |
| 87 | + }), |
| 88 | + ); |
| 89 | + await act(() => { |
| 90 | + renderer = create(<AttachmentImage src="blob:first" name="photo.heic" />); |
| 91 | + }); |
| 92 | + await act(() => renderer.root.findByType("img").props.onError(decodeError)); |
| 93 | + await act(() => renderer.update(<AttachmentImage src="blob:second" name="photo.heic" />)); |
| 94 | + await act(() => finishDecode({ ok: true, file: jpeg, recompressed: true })); |
| 95 | + expect(renderer.root.findByType("img").props.src).toBe("blob:second"); |
| 96 | + expect(URL.createObjectURL).not.toHaveBeenCalled(); |
| 97 | + expect(fetchImage.mock.calls[0]?.[1].signal.aborted).toBe(true); |
| 98 | +}); |
0 commit comments