diff --git a/web/__test__/test-setup.ts b/web/__test__/test-setup.ts new file mode 100644 index 000000000..d0de870dc --- /dev/null +++ b/web/__test__/test-setup.ts @@ -0,0 +1 @@ +import "@testing-library/jest-dom"; diff --git a/web/__test__/testing-library.js b/web/__test__/testing-library.js new file mode 100644 index 000000000..9c7f86fb4 --- /dev/null +++ b/web/__test__/testing-library.js @@ -0,0 +1 @@ +export * from "@testing-library/react"; diff --git a/web/src/utils/snapshotUtil.test.ts b/web/src/utils/snapshotUtil.test.ts new file mode 100644 index 000000000..5c2dced45 --- /dev/null +++ b/web/src/utils/snapshotUtil.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it, vi } from "vitest"; +import { + downloadSnapshot, + generateSnapshotFilename, +} from "@/utils/snapshotUtil"; + +describe("generateSnapshotFilename", () => { + it("uses the provided playback timestamp in the filename", () => { + expect(generateSnapshotFilename("driveway", 1712592245, "UTC")).toBe( + "driveway_snapshot_2024-04-08T16-04-05.jpg", + ); + }); + + it("uses the provided timezone so filename matches timeline-local time", () => { + expect( + generateSnapshotFilename("driveway", 1712592245, "America/Los_Angeles"), + ).toBe("driveway_snapshot_2024-04-08T09-04-05.jpg"); + }); + + it("falls back to current time when no playback timestamp is provided", () => { + vi.useFakeTimers(); + vi.setSystemTime(new Date("2024-08-09T10:11:12Z")); + + expect(generateSnapshotFilename("front_yard", undefined, "UTC")).toBe( + "front_yard_snapshot_2024-08-09T10-11-12.jpg", + ); + + vi.useRealTimers(); + }); +}); + +describe("downloadSnapshot", () => { + it("creates and clicks a temporary anchor for download", () => { + const appendSpy = vi.spyOn(document.body, "appendChild"); + const removeSpy = vi.spyOn(document.body, "removeChild"); + const clickSpy = vi + .spyOn(HTMLAnchorElement.prototype, "click") + .mockImplementation(() => {}); + + downloadSnapshot("data:image/jpeg;base64,abc123", "snapshot.jpg"); + + expect(appendSpy).toHaveBeenCalledTimes(1); + expect(clickSpy).toHaveBeenCalledTimes(1); + expect(removeSpy).toHaveBeenCalledTimes(1); + + const link = appendSpy.mock.calls[0]?.[0] as HTMLAnchorElement; + expect(link.download).toBe("snapshot.jpg"); + expect(link.href).toContain("data:image/jpeg;base64,abc123"); + }); +});