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