Files
crowd-funder-for-time-pwa/src/test/services/sharedImageHandoff.test.ts
T

65 lines
2.4 KiB
TypeScript

import { createSharedImageHandoff } from "../../services/sharedImageHandoff";
describe("createSharedImageHandoff", () => {
it("shares one native read and navigation across concurrent callers", async () => {
let releaseRead: (image: { base64: string; fileName: string }) => void;
const getSharedImage = jest.fn(
() =>
new Promise<{ base64: string; fileName: string }>((resolve) => {
releaseRead = resolve;
}),
);
const storeSharedImage = jest.fn().mockResolvedValue(undefined);
const navigateToSharedPhoto = jest.fn().mockResolvedValue(undefined);
const handoff = createSharedImageHandoff({
getSharedImage,
storeSharedImage,
navigateToSharedPhoto,
});
const first = handoff.process();
const second = handoff.process();
releaseRead!({ base64: "image-data", fileName: "photo.jpg" });
await expect(Promise.all([first, second])).resolves.toEqual([true, true]);
expect(getSharedImage).toHaveBeenCalledTimes(1);
expect(storeSharedImage).toHaveBeenCalledWith("image-data", "photo.jpg");
expect(navigateToSharedPhoto).toHaveBeenCalledWith("photo.jpg");
});
it("does not navigate when native storage has no image", async () => {
const storeSharedImage = jest.fn();
const navigateToSharedPhoto = jest.fn();
const handoff = createSharedImageHandoff({
getSharedImage: async () => null,
storeSharedImage,
navigateToSharedPhoto,
});
await expect(handoff.process()).resolves.toBe(false);
expect(storeSharedImage).not.toHaveBeenCalled();
expect(navigateToSharedPhoto).not.toHaveBeenCalled();
});
it("allows a later share after the first operation completes", async () => {
const getSharedImage = jest
.fn()
.mockResolvedValueOnce({ base64: "first", fileName: "first.jpg" })
.mockResolvedValueOnce({ base64: "second", fileName: "second.jpg" });
const storeSharedImage = jest.fn().mockResolvedValue(undefined);
const navigateToSharedPhoto = jest.fn().mockResolvedValue(undefined);
const handoff = createSharedImageHandoff({
getSharedImage,
storeSharedImage,
navigateToSharedPhoto,
});
await handoff.process();
await handoff.process();
expect(getSharedImage).toHaveBeenCalledTimes(2);
expect(navigateToSharedPhoto).toHaveBeenNthCalledWith(1, "first.jpg");
expect(navigateToSharedPhoto).toHaveBeenNthCalledWith(2, "second.jpg");
});
});