import { beforeEach, describe, expect, it, vi } from "vitest"; const clearPendingCommunityAvatarFile = vi.fn(); const forgetFlushedCommunityAvatarUrl = vi.fn(); vi.mock("../../lib/create/pendingCommunityAvatarUpload", () => ({ clearPendingCommunityAvatarFile: () => clearPendingCommunityAvatarFile(), })); vi.mock("../../lib/create/flushPendingCommunityAvatar", () => ({ forgetFlushedCommunityAvatarUrl: () => forgetFlushedCommunityAvatarUrl(), })); import { CREATE_FLOW_ANONYMOUS_KEY, CREATE_FLOW_TRANSFER_PENDING_KEY, } from "../../app/(app)/create/utils/anonymousDraftStorage"; import { CORE_VALUE_DETAILS_STORAGE_KEY } from "../../app/(app)/create/utils/coreValueDetailsLocalStorage"; import { seedContinuedDraftLocalStorage } from "../../app/(app)/create/utils/seedContinuedDraftLocalStorage"; import type { CreateFlowState } from "../../app/(app)/create/types"; describe("seedContinuedDraftLocalStorage", () => { beforeEach(() => { localStorage.clear(); clearPendingCommunityAvatarFile.mockReset(); forgetFlushedCommunityAvatarUrl.mockReset(); localStorage.setItem( CREATE_FLOW_ANONYMOUS_KEY, JSON.stringify({ title: "Stale" }), ); localStorage.setItem( CORE_VALUE_DETAILS_STORAGE_KEY, JSON.stringify({ old: { meaning: "old", signals: "" } }), ); localStorage.setItem(CREATE_FLOW_TRANSFER_PENDING_KEY, "1"); }); it("writes the server draft over the browser mirror", () => { const state: CreateFlowState = { title: "Garden", currentStep: "core-values", coreValueDetailsByChipId: { solidarity: { meaning: "We show up.", signals: "" }, }, }; seedContinuedDraftLocalStorage(state); expect( JSON.parse(localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY) ?? "{}"), ).toEqual(state); expect( JSON.parse(localStorage.getItem(CORE_VALUE_DETAILS_STORAGE_KEY) ?? "{}"), ).toEqual(state.coreValueDetailsByChipId); expect(localStorage.getItem(CREATE_FLOW_TRANSFER_PENDING_KEY)).toBeNull(); expect(clearPendingCommunityAvatarFile).toHaveBeenCalledTimes(1); expect(forgetFlushedCommunityAvatarUrl).toHaveBeenCalledTimes(1); }); it("clears core-value details when the continued draft has none", () => { seedContinuedDraftLocalStorage({ title: "Garden" }); expect(localStorage.getItem(CORE_VALUE_DETAILS_STORAGE_KEY)).toBeNull(); expect( JSON.parse(localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY) ?? "{}"), ).toEqual({ title: "Garden", }); }); });