@olenbetong/synergi-react 5.0.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/styles/styles.css +1 -1
- package/dist/styles/styles.css.map +1 -1
- package/es/components/Checkbox/index.css +1 -1
- package/es/components/Checkbox/index.js +1 -9
- package/es/components/CodeEditor/CodeEditor.css +6 -6
- package/es/components/ColorCard/index.css +17 -28
- package/es/components/DateNavigator/index.css +5 -7
- package/es/components/FilePond.js +3 -12
- package/es/components/ImageEditor/ImageEditor.css +10 -10
- package/es/components/ImageEditor/ImageEditor.js +4 -4
- package/es/components/ImageEditor/ResponsiveButton.js +2 -2
- package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
- package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
- package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
- package/es/components/ImageEditor/useImageHistory.js +13 -1
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +3 -3
- package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
- package/es/components/PageBanner/index.css +1 -1
- package/es/components/PdfViewer/PdfViewer.js +1 -1
- package/es/components/Portal/index.js +20 -20
- package/es/components/Spinner/index.css +1 -1
- package/es/components/SplitContainer/index.css +1 -1
- package/es/components/SplitContainer/index.js +3 -1
- package/es/components/Toolbar/Toolbar.css +137 -137
- package/es/components/Toolbar/index.js +6 -1
- package/es/components/ValueToggleGroup/index.css +2 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
- package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
- package/es/elements-ds/DescriptionInput.d.ts +16 -0
- package/es/elements-ds/DescriptionInput.js +41 -0
- package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
- package/es/elements-ds/DuplicateElementDamage.js +57 -0
- package/es/elements-ds/ElementLookup.d.ts +23 -0
- package/es/elements-ds/ElementLookup.js +35 -0
- package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
- package/es/elements-ds/ElementLookupDialog.js +84 -0
- package/es/elements-ds/FormFields.d.ts +43 -0
- package/es/elements-ds/FormFields.js +67 -0
- package/es/elements-ds/HIDScanner.d.ts +7 -0
- package/es/elements-ds/HIDScanner.js +36 -0
- package/es/elements-ds/ManualForm.d.ts +7 -0
- package/es/elements-ds/ManualForm.js +70 -0
- package/es/elements-ds/ProjectSelect.d.ts +28 -0
- package/es/elements-ds/ProjectSelect.js +45 -0
- package/es/elements-ds/elements-ds.css +157 -0
- package/es/elements-ds/index.d.ts +12 -0
- package/es/elements-ds/index.js +14 -0
- package/es/index.css +1 -1
- package/es/useTranslation/index.js +5 -3
- package/package.json +16 -4
- package/src/chrome/AppShell/AppShell.test.tsx +48 -0
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.test.ts +50 -0
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.test.ts +188 -0
- package/src/colorScheme.ts +125 -125
- package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
- package/src/components/Checkbox/index.scss +2 -2
- package/src/components/Checkbox/index.tsx +1 -10
- package/src/components/CodeEditor/CodeEditor.css +6 -6
- package/src/components/CodeEditor/CodeEditor.tsx +173 -173
- package/src/components/CodeEditor/index.ts +1 -1
- package/src/components/ColorCard/ColorCard.test.tsx +34 -0
- package/src/components/ColorCard/index.css +17 -28
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/DateNavigator/index.css +5 -7
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.browser.test.tsx +389 -0
- package/src/components/FilePond.tsx +303 -312
- package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
- package/src/components/ImageEditor/ImageEditor.css +10 -10
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/ImageEditor.tsx +348 -348
- package/src/components/ImageEditor/ResponsiveButton.tsx +2 -2
- package/src/components/ImageEditor/index.ts +9 -9
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +42 -48
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
- package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
- package/src/components/ImageEditor/useActiveTool.ts +21 -21
- package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
- package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
- package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
- package/src/components/ImageEditor/useImageHistory.ts +206 -197
- package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +4 -3
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
- package/src/components/PageBanner/index.scss +10 -10
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
- package/src/components/PdfViewer/PdfViewer.tsx +158 -158
- package/src/components/Portal/Portal.test.tsx +106 -0
- package/src/components/Portal/index.tsx +22 -25
- package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
- package/src/components/Sidebar/Sidebar.test.tsx +123 -0
- package/src/components/Spinner/index.css +1 -1
- package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
- package/src/components/SplitContainer/index.scss +5 -1
- package/src/components/SplitContainer/index.tsx +335 -333
- package/src/components/Toolbar/Toolbar.css +137 -137
- package/src/components/Toolbar/Toolbar.test.tsx +216 -0
- package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
- package/src/components/Toolbar/index.tsx +270 -265
- package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
- package/src/components/ValueToggleGroup/index.css +2 -2
- package/src/designsystemet-web-types.d.ts +24 -24
- package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
- package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
- package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
- package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
- package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
- package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
- package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
- package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
- package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
- package/src/elements/ElementLookup.tsx +123 -123
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
- package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
- package/src/elements/ProjectSelect.tsx +130 -130
- package/src/elements/index.ts +15 -15
- package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
- package/src/elements-ds/DescriptionInput.tsx +114 -0
- package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
- package/src/elements-ds/ElementLookup.tsx +114 -0
- package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
- package/src/elements-ds/ElementLookupDialog.tsx +194 -0
- package/src/elements-ds/FormFields.tsx +176 -0
- package/src/elements-ds/HIDScanner.tsx +53 -0
- package/src/elements-ds/ManualForm.tsx +129 -0
- package/src/elements-ds/ProjectSelect.tsx +120 -0
- package/src/elements-ds/elements-ds.css +157 -0
- package/src/elements-ds/index.ts +16 -0
- package/src/filter/index.ts +34 -34
- package/src/i18n.test.ts +31 -0
- package/src/i18n.ts +13 -13
- package/src/mui/MuiThemeProvider.tsx +135 -135
- package/src/test/fakeCamera.ts +98 -0
- package/src/test/fakeDataApi.ts +90 -0
- package/src/test/setup.browser.ts +21 -0
- package/src/test/setup.ts +23 -23
- package/src/useTranslation/index.ts +5 -3
- package/src/useTranslation/useTranslation.test.ts +128 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
- package/es/test/setup.d.ts +0 -6
- package/es/test/setup.js +0 -6
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import type { ActionRegistry, EditAction } from "./types";
|
|
5
|
+
import { useImageHistory } from "./useImageHistory";
|
|
6
|
+
|
|
7
|
+
const SOURCE = "blob:source";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Object URLs are faked as `blob:<n>` so the tests can follow which image is current and
|
|
11
|
+
* which URLs were released. Each fake blob's text is the chain of edits that produced it.
|
|
12
|
+
*/
|
|
13
|
+
let created: string[] = [];
|
|
14
|
+
let revoked: string[] = [];
|
|
15
|
+
let blobsByUrl = new Map<string, string>();
|
|
16
|
+
let blobText = new WeakMap<Blob, string>();
|
|
17
|
+
|
|
18
|
+
function blob(text: string) {
|
|
19
|
+
let b = new Blob([text], { type: "text/plain" });
|
|
20
|
+
blobText.set(b, text);
|
|
21
|
+
return b;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
created = [];
|
|
26
|
+
revoked = [];
|
|
27
|
+
blobsByUrl = new Map([[SOURCE, "source"]]);
|
|
28
|
+
let counter = 0;
|
|
29
|
+
vi.spyOn(URL, "createObjectURL").mockImplementation((b) => {
|
|
30
|
+
let url = `blob:${++counter}`;
|
|
31
|
+
created.push(url);
|
|
32
|
+
blobsByUrl.set(url, blobText.get(b as Blob) ?? "?");
|
|
33
|
+
return url;
|
|
34
|
+
});
|
|
35
|
+
vi.spyOn(URL, "revokeObjectURL").mockImplementation((url) => {
|
|
36
|
+
revoked.push(url);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
vi.restoreAllMocks();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** A replay handler that appends its action type to the text of the image it is given. */
|
|
45
|
+
function appendingRegistry(...types: string[]) {
|
|
46
|
+
let calls: { url: string; type: string; params: Record<string, unknown> }[] = [];
|
|
47
|
+
let registry: ActionRegistry = new Map();
|
|
48
|
+
for (let type of types) {
|
|
49
|
+
registry.set(type, async (url, params) => {
|
|
50
|
+
calls.push({ url, type, params });
|
|
51
|
+
return blob(`${blobsByUrl.get(url) ?? url}+${type}`);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return { registry, calls };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function action(type: string, params: Record<string, unknown> = {}): EditAction[] {
|
|
58
|
+
return [{ type, params }];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("useImageHistory", () => {
|
|
62
|
+
it("starts on the source image with nothing to undo or redo", () => {
|
|
63
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
64
|
+
|
|
65
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
66
|
+
expect(result.current.currentBlob).toBeNull();
|
|
67
|
+
expect(result.current.canUndo).toBe(false);
|
|
68
|
+
expect(result.current.canRedo).toBe(false);
|
|
69
|
+
expect(result.current.replaying).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("makes an applied image current at once, before React re-renders", () => {
|
|
73
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
74
|
+
let rotated = blob("rotated");
|
|
75
|
+
|
|
76
|
+
let url = "";
|
|
77
|
+
act(() => {
|
|
78
|
+
url = result.current.applyActions(rotated, action("rotate-cw"));
|
|
79
|
+
// The imperative getters are what ImageEditorRef.getCurrentBlob() reads.
|
|
80
|
+
expect(result.current.getCurrentImageUrl()).toBe(url);
|
|
81
|
+
expect(result.current.getCurrentBlob()).toBe(rotated);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(result.current.currentImageUrl).toBe(url);
|
|
85
|
+
expect(result.current.currentBlob).toBe(rotated);
|
|
86
|
+
expect(result.current.canUndo).toBe(true);
|
|
87
|
+
expect(result.current.canRedo).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("releases the previous edited image when a new one is applied, but never the source", () => {
|
|
91
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
92
|
+
|
|
93
|
+
let first = "";
|
|
94
|
+
act(() => {
|
|
95
|
+
first = result.current.applyActions(blob("a"), action("a"));
|
|
96
|
+
});
|
|
97
|
+
expect(revoked).toEqual([]);
|
|
98
|
+
|
|
99
|
+
act(() => {
|
|
100
|
+
result.current.applyActions(blob("b"), action("b"));
|
|
101
|
+
});
|
|
102
|
+
expect(revoked).toEqual([first]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("undoes by replaying the remaining edits from the source image", async () => {
|
|
106
|
+
let { registry, calls } = appendingRegistry("a", "b");
|
|
107
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
108
|
+
|
|
109
|
+
act(() => {
|
|
110
|
+
result.current.applyActions(blob("source+a"), action("a", { n: 1 }));
|
|
111
|
+
});
|
|
112
|
+
act(() => {
|
|
113
|
+
result.current.applyActions(blob("source+a+b"), action("b"));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
act(() => result.current.undo());
|
|
117
|
+
expect(result.current.replaying).toBe(true);
|
|
118
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
119
|
+
|
|
120
|
+
// Only "a" is replayed, from the original source and with its recorded params.
|
|
121
|
+
expect(calls).toEqual([{ url: SOURCE, type: "a", params: { n: 1 } }]);
|
|
122
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a");
|
|
123
|
+
expect(result.current.canUndo).toBe(true);
|
|
124
|
+
expect(result.current.canRedo).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("goes back to the untouched source when the last edit is undone", async () => {
|
|
128
|
+
let { registry, calls } = appendingRegistry("a");
|
|
129
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
130
|
+
|
|
131
|
+
act(() => {
|
|
132
|
+
result.current.applyActions(blob("source+a"), action("a"));
|
|
133
|
+
});
|
|
134
|
+
act(() => result.current.undo());
|
|
135
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
136
|
+
|
|
137
|
+
expect(calls).toEqual([]);
|
|
138
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
139
|
+
expect(result.current.currentBlob).toBeNull();
|
|
140
|
+
expect(result.current.canUndo).toBe(false);
|
|
141
|
+
expect(result.current.canRedo).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("redoes an undone edit by replaying it on top of the others", async () => {
|
|
145
|
+
let { registry, calls } = appendingRegistry("a", "b");
|
|
146
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
147
|
+
|
|
148
|
+
act(() => {
|
|
149
|
+
result.current.applyActions(blob("source+a"), action("a"));
|
|
150
|
+
});
|
|
151
|
+
act(() => {
|
|
152
|
+
result.current.applyActions(blob("source+a+b"), action("b"));
|
|
153
|
+
});
|
|
154
|
+
act(() => result.current.undo());
|
|
155
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
156
|
+
calls.length = 0;
|
|
157
|
+
|
|
158
|
+
act(() => result.current.redo());
|
|
159
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
160
|
+
|
|
161
|
+
expect(calls.map((c) => c.type)).toEqual(["a", "b"]);
|
|
162
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a+b");
|
|
163
|
+
expect(result.current.canRedo).toBe(false);
|
|
164
|
+
expect(result.current.canUndo).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("replays every action of a multi-action edit (several redactions in one apply)", async () => {
|
|
168
|
+
let { registry, calls } = appendingRegistry("a", "r");
|
|
169
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
170
|
+
|
|
171
|
+
act(() => {
|
|
172
|
+
result.current.applyActions(blob("x"), [
|
|
173
|
+
{ type: "r", params: { x: 1 } },
|
|
174
|
+
{ type: "r", params: { x: 2 } },
|
|
175
|
+
]);
|
|
176
|
+
});
|
|
177
|
+
act(() => {
|
|
178
|
+
result.current.applyActions(blob("y"), action("a"));
|
|
179
|
+
});
|
|
180
|
+
act(() => result.current.undo());
|
|
181
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
182
|
+
|
|
183
|
+
expect(calls.map((c) => c.params)).toEqual([{ x: 1 }, { x: 2 }]);
|
|
184
|
+
expect(await result.current.currentBlob?.text()).toBe("source+r+r");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("releases the intermediate images it creates while replaying", async () => {
|
|
188
|
+
let { registry } = appendingRegistry("a", "b", "c");
|
|
189
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
190
|
+
|
|
191
|
+
for (let type of ["a", "b", "c"]) {
|
|
192
|
+
act(() => {
|
|
193
|
+
result.current.applyActions(blob(type), action(type));
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
revoked = [];
|
|
197
|
+
let before = created.length;
|
|
198
|
+
|
|
199
|
+
act(() => result.current.undo());
|
|
200
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
201
|
+
|
|
202
|
+
// Replaying a then b creates two URLs; the one for "a" is an intermediate.
|
|
203
|
+
let replayUrls = created.slice(before);
|
|
204
|
+
expect(replayUrls).toHaveLength(2);
|
|
205
|
+
expect(revoked).toContain(replayUrls[0]);
|
|
206
|
+
expect(revoked).not.toContain(replayUrls[1]);
|
|
207
|
+
expect(result.current.currentImageUrl).toBe(replayUrls[1]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("drops the redo stack when a new edit is applied", async () => {
|
|
211
|
+
let { registry } = appendingRegistry("a");
|
|
212
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
213
|
+
|
|
214
|
+
act(() => {
|
|
215
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
216
|
+
});
|
|
217
|
+
act(() => result.current.undo());
|
|
218
|
+
await waitFor(() => expect(result.current.canRedo).toBe(true));
|
|
219
|
+
|
|
220
|
+
act(() => {
|
|
221
|
+
result.current.applyActions(blob("b"), action("b"));
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
expect(result.current.canRedo).toBe(false);
|
|
225
|
+
expect(result.current.canUndo).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("ignores undo and redo when there is nothing to undo or redo", () => {
|
|
229
|
+
let { registry, calls } = appendingRegistry("a");
|
|
230
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
231
|
+
|
|
232
|
+
act(() => result.current.undo());
|
|
233
|
+
act(() => result.current.redo());
|
|
234
|
+
|
|
235
|
+
expect(result.current.replaying).toBe(false);
|
|
236
|
+
expect(calls).toEqual([]);
|
|
237
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("ignores a second undo while the first is still replaying", async () => {
|
|
241
|
+
let release: () => void = () => {};
|
|
242
|
+
let registry: ActionRegistry = new Map([
|
|
243
|
+
[
|
|
244
|
+
"a",
|
|
245
|
+
() =>
|
|
246
|
+
new Promise<Blob>((resolve) => {
|
|
247
|
+
release = () => resolve(blob("source+a"));
|
|
248
|
+
}),
|
|
249
|
+
],
|
|
250
|
+
]);
|
|
251
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
252
|
+
|
|
253
|
+
act(() => {
|
|
254
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
255
|
+
});
|
|
256
|
+
act(() => {
|
|
257
|
+
result.current.applyActions(blob("ab"), action("a"));
|
|
258
|
+
});
|
|
259
|
+
act(() => result.current.undo());
|
|
260
|
+
act(() => result.current.undo());
|
|
261
|
+
|
|
262
|
+
await act(async () => release());
|
|
263
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
264
|
+
|
|
265
|
+
// One undo: one edit left to undo, one to redo.
|
|
266
|
+
expect(result.current.canUndo).toBe(true);
|
|
267
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("lets a new edit win over an undo that is still replaying", async () => {
|
|
271
|
+
let release: () => void = () => {};
|
|
272
|
+
let registry: ActionRegistry = new Map([
|
|
273
|
+
[
|
|
274
|
+
"a",
|
|
275
|
+
() =>
|
|
276
|
+
new Promise<Blob>((resolve) => {
|
|
277
|
+
release = () => resolve(blob("stale"));
|
|
278
|
+
}),
|
|
279
|
+
],
|
|
280
|
+
]);
|
|
281
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
282
|
+
|
|
283
|
+
act(() => {
|
|
284
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
285
|
+
});
|
|
286
|
+
act(() => {
|
|
287
|
+
result.current.applyActions(blob("aa"), action("a"));
|
|
288
|
+
});
|
|
289
|
+
act(() => result.current.undo());
|
|
290
|
+
|
|
291
|
+
let fresh = blob("fresh");
|
|
292
|
+
act(() => {
|
|
293
|
+
result.current.applyActions(fresh, action("b"));
|
|
294
|
+
});
|
|
295
|
+
expect(result.current.replaying).toBe(false);
|
|
296
|
+
|
|
297
|
+
await act(async () => release());
|
|
298
|
+
|
|
299
|
+
expect(result.current.currentBlob).toBe(fresh);
|
|
300
|
+
expect(result.current.canRedo).toBe(false);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("skips actions it has no replay handler for", async () => {
|
|
304
|
+
let warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
305
|
+
let { registry, calls } = appendingRegistry("a");
|
|
306
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
307
|
+
|
|
308
|
+
act(() => {
|
|
309
|
+
result.current.applyActions(blob("x"), action("unknown"));
|
|
310
|
+
});
|
|
311
|
+
act(() => {
|
|
312
|
+
result.current.applyActions(blob("y"), action("a"));
|
|
313
|
+
});
|
|
314
|
+
act(() => {
|
|
315
|
+
result.current.applyActions(blob("z"), action("a"));
|
|
316
|
+
});
|
|
317
|
+
act(() => result.current.undo());
|
|
318
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
319
|
+
|
|
320
|
+
expect(calls.map((c) => c.type)).toEqual(["a"]);
|
|
321
|
+
expect(await result.current.currentBlob?.text()).toBe("source+a");
|
|
322
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('"unknown"'));
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("stays on the current image when an edit cannot be replayed", async () => {
|
|
326
|
+
let warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
327
|
+
let registry: ActionRegistry = new Map([["a", () => Promise.reject(new Error("Failed to load image"))]]);
|
|
328
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
329
|
+
|
|
330
|
+
act(() => {
|
|
331
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
332
|
+
});
|
|
333
|
+
let current = "";
|
|
334
|
+
act(() => {
|
|
335
|
+
current = result.current.applyActions(blob("b"), action("b"));
|
|
336
|
+
});
|
|
337
|
+
act(() => result.current.undo());
|
|
338
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
339
|
+
|
|
340
|
+
// The undo did not happen, so the user can try again; the failure is logged, not thrown.
|
|
341
|
+
expect(result.current.currentImageUrl).toBe(current);
|
|
342
|
+
expect(result.current.canUndo).toBe(true);
|
|
343
|
+
expect(result.current.canRedo).toBe(false);
|
|
344
|
+
expect(warn).toHaveBeenCalled();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("releases the images of a replay that failed half-way", async () => {
|
|
348
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
349
|
+
let { registry } = appendingRegistry("a");
|
|
350
|
+
registry.set("b", () => Promise.reject(new Error("Failed to load image")));
|
|
351
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, registry));
|
|
352
|
+
|
|
353
|
+
for (let type of ["a", "b", "c"]) {
|
|
354
|
+
act(() => {
|
|
355
|
+
result.current.applyActions(blob(type), action(type));
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
let before = created.length;
|
|
359
|
+
act(() => result.current.undo());
|
|
360
|
+
await waitFor(() => expect(result.current.replaying).toBe(false));
|
|
361
|
+
|
|
362
|
+
let replayUrls = created.slice(before);
|
|
363
|
+
expect(replayUrls).toHaveLength(1);
|
|
364
|
+
expect(revoked).toContain(replayUrls[0]);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("reset returns to the source and clears the history", () => {
|
|
368
|
+
let { result } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
369
|
+
|
|
370
|
+
let url = "";
|
|
371
|
+
act(() => {
|
|
372
|
+
url = result.current.applyActions(blob("a"), action("a"));
|
|
373
|
+
});
|
|
374
|
+
act(() => result.current.reset());
|
|
375
|
+
|
|
376
|
+
expect(result.current.currentImageUrl).toBe(SOURCE);
|
|
377
|
+
expect(result.current.getCurrentBlob()).toBeNull();
|
|
378
|
+
expect(result.current.canUndo).toBe(false);
|
|
379
|
+
expect(result.current.canRedo).toBe(false);
|
|
380
|
+
expect(revoked).toContain(url);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("starts over when the app passes a new source image", () => {
|
|
384
|
+
let { result, rerender } = renderHook(({ src }) => useImageHistory(src, new Map()), {
|
|
385
|
+
initialProps: { src: SOURCE },
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
act(() => {
|
|
389
|
+
result.current.applyActions(blob("a"), action("a"));
|
|
390
|
+
});
|
|
391
|
+
rerender({ src: "blob:saved" });
|
|
392
|
+
|
|
393
|
+
expect(result.current.currentImageUrl).toBe("blob:saved");
|
|
394
|
+
expect(result.current.currentBlob).toBeNull();
|
|
395
|
+
expect(result.current.canUndo).toBe(false);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("releases its edited image on unmount", () => {
|
|
399
|
+
let { result, unmount } = renderHook(() => useImageHistory(SOURCE, new Map()));
|
|
400
|
+
|
|
401
|
+
let url = "";
|
|
402
|
+
act(() => {
|
|
403
|
+
url = result.current.applyActions(blob("a"), action("a"));
|
|
404
|
+
});
|
|
405
|
+
unmount();
|
|
406
|
+
|
|
407
|
+
expect(revoked).toContain(url);
|
|
408
|
+
});
|
|
409
|
+
});
|