|
| 1 | +import "@testing-library/jest-dom/vitest" |
| 2 | +import { render, screen, waitFor } from "@testing-library/react" |
| 3 | +import React from "react" |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 5 | +import { ImageKitEditor } from "./ImageKitEditor" |
| 6 | +import { |
| 7 | + EDITOR_SESSION_STORAGE_KEY, |
| 8 | + EDITOR_SESSION_STORAGE_VERSION, |
| 9 | +} from "./persistence/editorSessionStorage" |
| 10 | +import type { TemplateStorageProvider } from "./storage" |
| 11 | +import { useEditorStore } from "./store" |
| 12 | + |
| 13 | +const RESUME_HEADING = "Resume previous session?" |
| 14 | + |
| 15 | +function stubTemplateStorage(): TemplateStorageProvider { |
| 16 | + return { |
| 17 | + getProviderName: () => "test", |
| 18 | + getCurrentUserSession: () => ({}), |
| 19 | + listTemplates: async () => [], |
| 20 | + getTemplate: async () => null, |
| 21 | + saveTemplate: async (record) => ({ |
| 22 | + id: record.id ?? "t-new", |
| 23 | + clientNumber: "c1", |
| 24 | + isPrivate: record.isPrivate ?? false, |
| 25 | + name: record.name, |
| 26 | + transformations: record.transformations ?? [], |
| 27 | + isPinned: false, |
| 28 | + createdBy: { userId: "u1", name: "U", email: "u@example.com" }, |
| 29 | + updatedBy: { userId: "u1", name: "U", email: "u@example.com" }, |
| 30 | + createdAt: Date.now(), |
| 31 | + updatedAt: Date.now(), |
| 32 | + }), |
| 33 | + setTemplatePinned: async () => { |
| 34 | + throw new Error("not used") |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function writeLastSessionToLocalStorage(args: { |
| 40 | + localChangeVersion: number |
| 41 | + lastSyncedVersion: number |
| 42 | + isPristine: boolean |
| 43 | +}) { |
| 44 | + const session = { |
| 45 | + v: EDITOR_SESSION_STORAGE_VERSION, |
| 46 | + savedAt: Date.now(), |
| 47 | + state: { |
| 48 | + transformations: [], |
| 49 | + visibleTransformations: {}, |
| 50 | + templateName: "Untitled Template", |
| 51 | + templateId: null, |
| 52 | + templateIsPrivate: null, |
| 53 | + syncStatus: "saved" as const, |
| 54 | + isPristine: args.isPristine, |
| 55 | + localChangeVersion: args.localChangeVersion, |
| 56 | + lastSyncedVersion: args.lastSyncedVersion, |
| 57 | + lastSavedAt: Date.now(), |
| 58 | + }, |
| 59 | + } |
| 60 | + window.localStorage.setItem( |
| 61 | + EDITOR_SESSION_STORAGE_KEY, |
| 62 | + JSON.stringify(session), |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +describe("ImageKitEditor resume session modal", () => { |
| 67 | + beforeEach(() => { |
| 68 | + useEditorStore.getState().destroy() |
| 69 | + window.localStorage.removeItem(EDITOR_SESSION_STORAGE_KEY) |
| 70 | + }) |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + useEditorStore.getState().destroy() |
| 74 | + window.localStorage.removeItem(EDITOR_SESSION_STORAGE_KEY) |
| 75 | + vi.restoreAllMocks() |
| 76 | + }) |
| 77 | + |
| 78 | + it("does not show resume modal when localStorage is empty", async () => { |
| 79 | + render( |
| 80 | + <ImageKitEditor |
| 81 | + onClose={() => {}} |
| 82 | + templateStorage={stubTemplateStorage()} |
| 83 | + />, |
| 84 | + ) |
| 85 | + |
| 86 | + await waitFor(() => { |
| 87 | + expect(screen.queryByText(RESUME_HEADING)).not.toBeInTheDocument() |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it("with template storage: does not show resume modal when versions are in sync", async () => { |
| 92 | + writeLastSessionToLocalStorage({ |
| 93 | + localChangeVersion: 3, |
| 94 | + lastSyncedVersion: 3, |
| 95 | + isPristine: false, |
| 96 | + }) |
| 97 | + |
| 98 | + render( |
| 99 | + <ImageKitEditor |
| 100 | + onClose={() => {}} |
| 101 | + templateStorage={stubTemplateStorage()} |
| 102 | + />, |
| 103 | + ) |
| 104 | + |
| 105 | + await waitFor(() => { |
| 106 | + expect(screen.queryByText(RESUME_HEADING)).not.toBeInTheDocument() |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + it("with template storage: shows resume modal when local changes are ahead of last sync", async () => { |
| 111 | + writeLastSessionToLocalStorage({ |
| 112 | + localChangeVersion: 4, |
| 113 | + lastSyncedVersion: 2, |
| 114 | + isPristine: true, |
| 115 | + }) |
| 116 | + |
| 117 | + render( |
| 118 | + <ImageKitEditor |
| 119 | + onClose={() => {}} |
| 120 | + templateStorage={stubTemplateStorage()} |
| 121 | + />, |
| 122 | + ) |
| 123 | + |
| 124 | + await waitFor(() => { |
| 125 | + expect(screen.getByText(RESUME_HEADING)).toBeInTheDocument() |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + it("without template storage: does not show resume modal when session is pristine", async () => { |
| 130 | + writeLastSessionToLocalStorage({ |
| 131 | + localChangeVersion: 0, |
| 132 | + lastSyncedVersion: 0, |
| 133 | + isPristine: true, |
| 134 | + }) |
| 135 | + |
| 136 | + render(<ImageKitEditor onClose={() => {}} />) |
| 137 | + |
| 138 | + await waitFor(() => { |
| 139 | + expect(screen.queryByText(RESUME_HEADING)).not.toBeInTheDocument() |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + it("without template storage: shows resume modal when session is not pristine", async () => { |
| 144 | + writeLastSessionToLocalStorage({ |
| 145 | + localChangeVersion: 1, |
| 146 | + lastSyncedVersion: 1, |
| 147 | + isPristine: false, |
| 148 | + }) |
| 149 | + |
| 150 | + render(<ImageKitEditor onClose={() => {}} />) |
| 151 | + |
| 152 | + await waitFor(() => { |
| 153 | + expect(screen.getByText(RESUME_HEADING)).toBeInTheDocument() |
| 154 | + }) |
| 155 | + }) |
| 156 | +}) |
0 commit comments