@mapomodule/uikit 2.0.0-beta.8 → 2.0.0-experimental.1
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/README.md +47 -174
- package/dist/__tests__/MapoConfirmDialog.component.test.d.ts +2 -0
- package/dist/__tests__/MapoConfirmDialog.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoDetail.component.test.d.ts +2 -0
- package/dist/__tests__/MapoDetail.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts +2 -0
- package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoListActions.component.test.d.ts +2 -0
- package/dist/__tests__/MapoListActions.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoListTable.component.test.d.ts +2 -0
- package/dist/__tests__/MapoListTable.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoSnackBar.component.test.d.ts +2 -0
- package/dist/__tests__/MapoSnackBar.component.test.d.ts.map +1 -0
- package/dist/__tests__/__mocks__/nuxt-app.d.ts +9 -0
- package/dist/__tests__/__mocks__/nuxt-app.d.ts.map +1 -0
- package/dist/__tests__/formValues.test.d.ts +2 -0
- package/dist/__tests__/formValues.test.d.ts.map +1 -0
- package/dist/__tests__/langInfo.test.d.ts +2 -0
- package/dist/__tests__/langInfo.test.d.ts.map +1 -0
- package/dist/__tests__/listTable.test.d.ts +2 -0
- package/dist/__tests__/listTable.test.d.ts.map +1 -0
- package/dist/__tests__/mediaLanguages.test.d.ts +2 -0
- package/dist/__tests__/mediaLanguages.test.d.ts.map +1 -0
- package/dist/__tests__/runtimePages.test.d.ts +2 -0
- package/dist/__tests__/runtimePages.test.d.ts.map +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/module.d.mts +1 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +10 -0
- package/dist/runtime/assets/main.css +1 -1
- package/dist/runtime/components/MapoConfirmDialog.vue +18 -1
- package/dist/runtime/components/MapoConfirmDialog.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDetail.vue +263 -33
- package/dist/runtime/components/MapoDetail.vue.d.ts +191 -9
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDetailExtensions.vue +24 -0
- package/dist/runtime/components/MapoDetailExtensions.vue.d.ts +15 -0
- package/dist/runtime/components/MapoDetailExtensions.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoDetailLangSwitch.vue +21 -25
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts +4 -3
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDropArea.vue.d.ts +1 -1
- package/dist/runtime/components/MapoDropArea.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoList.vue +7 -3
- package/dist/runtime/components/MapoList.vue.d.ts +14 -4
- package/dist/runtime/components/MapoList.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListActions.vue +12 -5
- package/dist/runtime/components/MapoListActions.vue.d.ts +4 -0
- package/dist/runtime/components/MapoListActions.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListQuickEdit.vue +1 -1
- package/dist/runtime/components/MapoListTable.vue +99 -33
- package/dist/runtime/components/MapoListTable.vue.d.ts +11 -6
- package/dist/runtime/components/MapoListTable.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaEditor.vue +10 -4
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts +1 -0
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaFileChanger.vue +1 -1
- package/dist/runtime/components/MapoMediaManager.vue +1 -1
- package/dist/runtime/components/MapoMediaManagerDialog.vue +1 -1
- package/dist/runtime/components/MapoMediaPreview.vue +1 -1
- package/dist/runtime/components/MapoMediaUploader.vue +21 -21
- package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuManager.vue +23 -3
- package/dist/runtime/components/MapoMenuManager.vue.d.ts +2 -2
- package/dist/runtime/components/MapoMenuManager.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuTreeview.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMenuTreeview.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuTreeviewNode.vue.d.ts +2 -2
- package/dist/runtime/components/MapoSnackBar.vue +11 -0
- package/dist/runtime/components/MapoSnackBar.vue.d.ts.map +1 -1
- package/dist/runtime/composables/defineDetailExtension.d.ts +32 -0
- package/dist/runtime/composables/defineDetailExtension.d.ts.map +1 -0
- package/dist/runtime/composables/defineDetailExtension.js +34 -0
- package/dist/runtime/composables/useMapoDetail.d.ts +18 -0
- package/dist/runtime/composables/useMapoDetail.d.ts.map +1 -0
- package/dist/runtime/composables/useMapoDetail.js +5 -0
- package/dist/runtime/page-meta.nuxt.d.ts +4 -0
- package/dist/runtime/stores/media.d.ts +8 -2
- package/dist/runtime/stores/media.d.ts.map +1 -1
- package/dist/runtime/stores/media.js +16 -0
- package/dist/runtime/types/detail.d.ts +167 -0
- package/dist/runtime/types/detail.d.ts.map +1 -0
- package/dist/runtime/types/detail.js +0 -0
- package/dist/runtime/types/list.d.ts +10 -2
- package/dist/runtime/types/list.d.ts.map +1 -1
- package/dist/runtime/utils/formValues.d.ts +8 -0
- package/dist/runtime/utils/formValues.d.ts.map +1 -0
- package/dist/runtime/utils/formValues.js +19 -0
- package/dist/runtime/utils/langInfo.d.ts +30 -0
- package/dist/runtime/utils/langInfo.d.ts.map +1 -0
- package/dist/runtime/utils/langInfo.js +8 -0
- package/dist/runtime/utils/listTable.d.ts +29 -0
- package/dist/runtime/utils/listTable.d.ts.map +1 -0
- package/dist/runtime/utils/listTable.js +21 -0
- package/package.json +7 -8
- package/src/__tests__/MapoConfirmDialog.component.test.ts +165 -0
- package/src/__tests__/MapoDetail.component.test.ts +1107 -0
- package/src/__tests__/MapoDetailLangSwitch.component.test.ts +145 -0
- package/src/__tests__/MapoListActions.component.test.ts +175 -0
- package/src/__tests__/MapoListTable.component.test.ts +504 -0
- package/src/__tests__/MapoSnackBar.component.test.ts +79 -0
- package/src/__tests__/__mocks__/nuxt-app.ts +8 -0
- package/src/__tests__/formValues.test.ts +31 -0
- package/src/__tests__/langInfo.test.ts +48 -0
- package/src/__tests__/listTable.test.ts +82 -0
- package/src/__tests__/mediaLanguages.test.ts +66 -0
- package/src/__tests__/runtimePages.test.ts +17 -0
- package/src/index.ts +8 -0
- package/src/module.ts +12 -0
- package/src/runtime/assets/main.css +13 -0
- package/src/runtime/components/MapoConfirmDialog.vue +28 -1
- package/src/runtime/components/MapoDetail.vue +481 -53
- package/src/runtime/components/MapoDetailExtensions.vue +39 -0
- package/src/runtime/components/MapoDetailLangSwitch.vue +26 -27
- package/src/runtime/components/MapoList.vue +20 -5
- package/src/runtime/components/MapoListActions.vue +16 -4
- package/src/runtime/components/MapoListTable.vue +128 -41
- package/src/runtime/components/MapoMediaEditor.vue +17 -4
- package/src/runtime/components/MapoMediaUploader.vue +25 -21
- package/src/runtime/components/MapoMenuManager.vue +38 -3
- package/src/runtime/components/MapoSnackBar.vue +12 -0
- package/src/runtime/composables/defineDetailExtension.ts +78 -0
- package/src/runtime/composables/useMapoDetail.ts +24 -0
- package/src/runtime/page-meta.nuxt.d.ts +4 -0
- package/src/runtime/stores/media.ts +32 -0
- package/src/runtime/types/detail.ts +178 -0
- package/src/runtime/types/list.ts +10 -2
- package/src/runtime/utils/formValues.ts +28 -0
- package/src/runtime/utils/langInfo.ts +38 -0
- package/src/runtime/utils/listTable.ts +54 -0
|
@@ -0,0 +1,1107 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, inject, nextTick, onUnmounted } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext, VNode } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoDetail from "../runtime/components/MapoDetail.vue";
|
|
6
|
+
import MapoDetailExtensions from "../runtime/components/MapoDetailExtensions.vue";
|
|
7
|
+
import {
|
|
8
|
+
defineDetailExtension,
|
|
9
|
+
detailExtensions,
|
|
10
|
+
} from "../runtime/composables/defineDetailExtension.js";
|
|
11
|
+
import { useMapoDetail } from "../runtime/composables/useMapoDetail.js";
|
|
12
|
+
import type { MapoDetailContext } from "../runtime/types/detail.js";
|
|
13
|
+
|
|
14
|
+
type AfterEachHook = (to: unknown, from: unknown, failure?: unknown) => void;
|
|
15
|
+
|
|
16
|
+
// The detail reads the router, the stores and the CRUD layer at setup: replace
|
|
17
|
+
// them with in-memory doubles. `vi.hoisted` lets the mock factories share them.
|
|
18
|
+
const router = vi.hoisted(() => ({
|
|
19
|
+
back: vi.fn(),
|
|
20
|
+
replace: vi.fn(),
|
|
21
|
+
afterEach: vi.fn<(hook: AfterEachHook) => () => void>(),
|
|
22
|
+
// Vue Router keeps the previous location of the current entry in `state.back`.
|
|
23
|
+
options: { history: { state: { back: "/news" as string | null } } },
|
|
24
|
+
}));
|
|
25
|
+
const crud = vi.hoisted(() => ({
|
|
26
|
+
options: vi.fn(),
|
|
27
|
+
detail: vi.fn(),
|
|
28
|
+
create: vi.fn(),
|
|
29
|
+
partialUpdate: vi.fn(),
|
|
30
|
+
update: vi.fn(),
|
|
31
|
+
delete: vi.fn(),
|
|
32
|
+
}));
|
|
33
|
+
const confirm = vi.hoisted(() => ({ ask: vi.fn() }));
|
|
34
|
+
// The signed-in user: drafts are kept under their id.
|
|
35
|
+
const auth = vi.hoisted(() => ({ info: { id: 7 } as { id: number } | null }));
|
|
36
|
+
type LeaveGuard = (
|
|
37
|
+
to: unknown,
|
|
38
|
+
from: unknown,
|
|
39
|
+
next: (ok: boolean) => void,
|
|
40
|
+
) => void;
|
|
41
|
+
// The unsaved-changes guard each detail registers, newest last.
|
|
42
|
+
const leaveGuards = vi.hoisted(() => [] as LeaveGuard[]);
|
|
43
|
+
// One app instance for every call, as in an app: the extensions live on it.
|
|
44
|
+
const nuxtApp = vi.hoisted(() => ({ $mapoFormRegistry: {} }));
|
|
45
|
+
|
|
46
|
+
vi.mock("#app", () => ({
|
|
47
|
+
useRouter: () => router,
|
|
48
|
+
onBeforeRouteLeave: (guard: LeaveGuard) => {
|
|
49
|
+
leaveGuards.push(guard);
|
|
50
|
+
},
|
|
51
|
+
useNuxtApp: () => nuxtApp,
|
|
52
|
+
}));
|
|
53
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
54
|
+
vi.mock("@mapomodule/core/runtime/api/crud", () => ({ useCrud: () => crud }));
|
|
55
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
56
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
57
|
+
}));
|
|
58
|
+
vi.mock("@mapomodule/store/runtime/stores/confirm", () => ({
|
|
59
|
+
useConfirmStore: () => confirm,
|
|
60
|
+
}));
|
|
61
|
+
vi.mock("@mapomodule/store/runtime/stores/auth", () => ({
|
|
62
|
+
useAuthStore: () => auth,
|
|
63
|
+
}));
|
|
64
|
+
vi.mock("@mapomodule/store/runtime/composables/usePermissions", () => ({
|
|
65
|
+
usePermissions: () => ({
|
|
66
|
+
canAdd: () => true,
|
|
67
|
+
canChange: () => true,
|
|
68
|
+
canDelete: () => true,
|
|
69
|
+
}),
|
|
70
|
+
}));
|
|
71
|
+
|
|
72
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
73
|
+
// enough to mount the component with real reactivity, watchers and events.
|
|
74
|
+
interface HeadlessNode {
|
|
75
|
+
parent: HeadlessNode | null;
|
|
76
|
+
children: HeadlessNode[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
80
|
+
|
|
81
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
82
|
+
createElement: headlessNode,
|
|
83
|
+
createText: headlessNode,
|
|
84
|
+
createComment: headlessNode,
|
|
85
|
+
setText: () => {},
|
|
86
|
+
setElementText: () => {},
|
|
87
|
+
patchProp: () => {},
|
|
88
|
+
insert(child, parent, anchor) {
|
|
89
|
+
child.parent = parent;
|
|
90
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
91
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
92
|
+
},
|
|
93
|
+
remove(child) {
|
|
94
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
95
|
+
child.parent = null;
|
|
96
|
+
},
|
|
97
|
+
parentNode: (node) => node.parent,
|
|
98
|
+
nextSibling: (node) =>
|
|
99
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const passthrough = (name: string): Component => ({
|
|
103
|
+
name,
|
|
104
|
+
inheritAttrs: false,
|
|
105
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
106
|
+
return () => h("div", null, slots.default?.());
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const slotText = (nodes: VNode[] | undefined) =>
|
|
111
|
+
(nodes ?? [])
|
|
112
|
+
.map((node) => (typeof node.children === "string" ? node.children : ""))
|
|
113
|
+
.join("")
|
|
114
|
+
.trim();
|
|
115
|
+
|
|
116
|
+
// `UButton` declares what the detail binds: `loading` and `disabled` in, `click`
|
|
117
|
+
// out. Like the real one, a loading or disabled button emits no click.
|
|
118
|
+
interface ButtonStub {
|
|
119
|
+
readonly label: string;
|
|
120
|
+
readonly loading: boolean;
|
|
121
|
+
readonly disabled: boolean;
|
|
122
|
+
click(): void;
|
|
123
|
+
}
|
|
124
|
+
const buttons: ButtonStub[] = [];
|
|
125
|
+
|
|
126
|
+
const UButton: Component = {
|
|
127
|
+
name: "UButton",
|
|
128
|
+
props: { loading: Boolean, disabled: Boolean },
|
|
129
|
+
emits: ["click"],
|
|
130
|
+
setup(
|
|
131
|
+
props: { loading?: boolean; disabled?: boolean },
|
|
132
|
+
{ emit, slots }: SetupContext<["click"]>,
|
|
133
|
+
) {
|
|
134
|
+
const stub: ButtonStub = {
|
|
135
|
+
get label() {
|
|
136
|
+
return slotText(slots.default?.());
|
|
137
|
+
},
|
|
138
|
+
get loading() {
|
|
139
|
+
return props.loading ?? false;
|
|
140
|
+
},
|
|
141
|
+
get disabled() {
|
|
142
|
+
return props.disabled ?? false;
|
|
143
|
+
},
|
|
144
|
+
click() {
|
|
145
|
+
if (stub.loading) return;
|
|
146
|
+
if (stub.disabled) return;
|
|
147
|
+
emit("click");
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
buttons.push(stub);
|
|
151
|
+
onUnmounted(() => {
|
|
152
|
+
buttons.splice(buttons.indexOf(stub), 1);
|
|
153
|
+
});
|
|
154
|
+
return () => h("button", null, slots.default?.());
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// `MapoForm` keeps the props of each instance, body and sidebar alike.
|
|
159
|
+
interface FormProps {
|
|
160
|
+
annotations?: object;
|
|
161
|
+
fields?: { key: string; type: string; attrs?: Record<string, unknown> }[];
|
|
162
|
+
notifyErrors?: boolean;
|
|
163
|
+
}
|
|
164
|
+
const forms: FormProps[] = [];
|
|
165
|
+
|
|
166
|
+
/** What the detail asked the forms to bring into view: the form's keys, the path. */
|
|
167
|
+
const reveals: { form: string[]; path: string }[] = [];
|
|
168
|
+
|
|
169
|
+
type RegisterReveal = (fn: (path: string) => Promise<boolean>) => () => void;
|
|
170
|
+
|
|
171
|
+
const MapoForm: Component = {
|
|
172
|
+
name: "MapoForm",
|
|
173
|
+
inheritAttrs: false,
|
|
174
|
+
props: {
|
|
175
|
+
annotations: Object,
|
|
176
|
+
fields: Array,
|
|
177
|
+
notifyErrors: { type: Boolean, default: true },
|
|
178
|
+
},
|
|
179
|
+
setup(props: FormProps, { slots }: SetupContext) {
|
|
180
|
+
forms.push(props);
|
|
181
|
+
// As the real form, it lets the detail bring its fields into view: here
|
|
182
|
+
// it answers whether it has the field.
|
|
183
|
+
const register = inject<RegisterReveal | null>(
|
|
184
|
+
"mapoDetailRegisterReveal",
|
|
185
|
+
null,
|
|
186
|
+
);
|
|
187
|
+
const unregister = register?.((path) => {
|
|
188
|
+
const keys = (props.fields ?? []).map((field) => field.key);
|
|
189
|
+
reveals.push({ form: keys, path });
|
|
190
|
+
return Promise.resolve(keys.includes(path));
|
|
191
|
+
});
|
|
192
|
+
onUnmounted(() => {
|
|
193
|
+
forms.splice(forms.indexOf(props), 1);
|
|
194
|
+
unregister?.();
|
|
195
|
+
});
|
|
196
|
+
return () => h("div", null, slots.default?.());
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const stubs: Record<string, Component> = {
|
|
201
|
+
UButton,
|
|
202
|
+
UCard: passthrough("UCard"),
|
|
203
|
+
UAlert: passthrough("UAlert"),
|
|
204
|
+
UIcon: passthrough("UIcon"),
|
|
205
|
+
UModal: passthrough("UModal"),
|
|
206
|
+
MapoForm,
|
|
207
|
+
MapoDetailLangSwitch: passthrough("MapoDetailLangSwitch"),
|
|
208
|
+
MapoDetailExtensions,
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const button = (label: string) => {
|
|
212
|
+
const found = buttons.find((stub) => stub.label === label);
|
|
213
|
+
if (!found) throw new Error(`no button ${label}`);
|
|
214
|
+
return found;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/** What a custom action panel reads from the `side-buttons` slot. */
|
|
218
|
+
interface ActionBindings {
|
|
219
|
+
isSaving: boolean;
|
|
220
|
+
save: (andBack?: boolean) => Promise<void>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let app: App<HeadlessNode> | undefined;
|
|
224
|
+
|
|
225
|
+
function mountDetail(
|
|
226
|
+
slots: Record<string, (bindings: never) => VNode[]> = {},
|
|
227
|
+
extraProps: Record<string, unknown> = {},
|
|
228
|
+
) {
|
|
229
|
+
const props = { endpoint: "/api/news/", id: 5, ...extraProps };
|
|
230
|
+
const root: Component = {
|
|
231
|
+
render: () => h(MapoDetail as Component, props, slots),
|
|
232
|
+
};
|
|
233
|
+
app = createApp(root);
|
|
234
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
235
|
+
app.mount(headlessNode());
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Runs the pending CRUD promises and the renders they trigger.
|
|
239
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve));
|
|
240
|
+
|
|
241
|
+
const afterEachHooks = new Set<AfterEachHook>();
|
|
242
|
+
|
|
243
|
+
/** Ends the navigation router.back() started; `failure` as Vue Router reports it. */
|
|
244
|
+
const finishNavigation = (failure?: Error) => {
|
|
245
|
+
for (const hook of [...afterEachHooks]) hook({}, {}, failure);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const record = { id: 5, title: "News" };
|
|
249
|
+
|
|
250
|
+
beforeEach(() => {
|
|
251
|
+
router.back.mockReset();
|
|
252
|
+
router.replace.mockReset();
|
|
253
|
+
router.afterEach.mockReset();
|
|
254
|
+
router.afterEach.mockImplementation((hook) => {
|
|
255
|
+
afterEachHooks.add(hook);
|
|
256
|
+
return () => afterEachHooks.delete(hook);
|
|
257
|
+
});
|
|
258
|
+
router.options.history.state.back = "/news";
|
|
259
|
+
afterEachHooks.clear();
|
|
260
|
+
for (const fn of Object.values(crud)) fn.mockReset();
|
|
261
|
+
crud.options.mockResolvedValue({});
|
|
262
|
+
crud.detail.mockResolvedValue(record);
|
|
263
|
+
crud.partialUpdate.mockResolvedValue(record);
|
|
264
|
+
crud.delete.mockResolvedValue(undefined);
|
|
265
|
+
confirm.ask.mockReset();
|
|
266
|
+
confirm.ask.mockResolvedValue(true);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
afterEach(() => {
|
|
270
|
+
app?.unmount();
|
|
271
|
+
app = undefined;
|
|
272
|
+
detailExtensions(nuxtApp).splice(0);
|
|
273
|
+
leaveGuards.splice(0);
|
|
274
|
+
auth.info = { id: 7 };
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe("MapoDetail actions", () => {
|
|
278
|
+
it("shows server error toasts only from the main form when sidebar fields are present", async () => {
|
|
279
|
+
mountDetail(
|
|
280
|
+
{},
|
|
281
|
+
{
|
|
282
|
+
fields: [{ key: "title", type: "text" }],
|
|
283
|
+
sidebarFields: [{ key: "slug", type: "text" }],
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
await settle();
|
|
287
|
+
|
|
288
|
+
expect(forms).toHaveLength(2);
|
|
289
|
+
expect(forms.map(({ notifyErrors }) => notifyErrors)).toEqual([
|
|
290
|
+
true,
|
|
291
|
+
false,
|
|
292
|
+
]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("keeps the buttons busy until the page has gone back after Save", async () => {
|
|
296
|
+
mountDetail();
|
|
297
|
+
await settle();
|
|
298
|
+
|
|
299
|
+
button("mapo.save").click();
|
|
300
|
+
await settle();
|
|
301
|
+
|
|
302
|
+
expect(crud.partialUpdate).toHaveBeenCalledTimes(1);
|
|
303
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
304
|
+
// The page is still on screen while the previous route loads.
|
|
305
|
+
expect(button("mapo.save").loading).toBe(true);
|
|
306
|
+
expect(button("mapo.saveContinue").disabled).toBe(true);
|
|
307
|
+
expect(button("mapo.back").disabled).toBe(true);
|
|
308
|
+
expect(button("mapo.delete").disabled).toBe(true);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("frees the buttons when the navigation back is cancelled", async () => {
|
|
312
|
+
mountDetail();
|
|
313
|
+
await settle();
|
|
314
|
+
button("mapo.save").click();
|
|
315
|
+
await settle();
|
|
316
|
+
|
|
317
|
+
// A guard kept the page, such as the unsaved-changes one.
|
|
318
|
+
finishNavigation(new Error("aborted"));
|
|
319
|
+
await settle();
|
|
320
|
+
|
|
321
|
+
expect(button("mapo.save").loading).toBe(false);
|
|
322
|
+
expect(button("mapo.back").disabled).toBe(false);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("saves once and stays busy when a custom button saves twice", async () => {
|
|
326
|
+
let bindings: ActionBindings | undefined;
|
|
327
|
+
mountDetail({
|
|
328
|
+
"side-buttons": (slotProps: ActionBindings) => {
|
|
329
|
+
bindings = slotProps;
|
|
330
|
+
return [];
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
await settle();
|
|
334
|
+
|
|
335
|
+
void bindings?.save(true);
|
|
336
|
+
void bindings?.save(true);
|
|
337
|
+
await settle();
|
|
338
|
+
|
|
339
|
+
expect(crud.partialUpdate).toHaveBeenCalledTimes(1);
|
|
340
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
341
|
+
expect(bindings?.isSaving).toBe(true);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("goes back once when Back is clicked twice", async () => {
|
|
345
|
+
mountDetail();
|
|
346
|
+
await settle();
|
|
347
|
+
|
|
348
|
+
button("mapo.back").click();
|
|
349
|
+
button("mapo.back").click();
|
|
350
|
+
await settle();
|
|
351
|
+
|
|
352
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
353
|
+
expect(button("mapo.back").loading).toBe(true);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("does not lock the buttons when there is no previous page", async () => {
|
|
357
|
+
router.options.history.state.back = null;
|
|
358
|
+
mountDetail();
|
|
359
|
+
await settle();
|
|
360
|
+
|
|
361
|
+
button("mapo.save").click();
|
|
362
|
+
await settle();
|
|
363
|
+
|
|
364
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
365
|
+
expect(button("mapo.save").loading).toBe(false);
|
|
366
|
+
expect(button("mapo.back").disabled).toBe(false);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("keeps Delete busy until the page has gone back", async () => {
|
|
370
|
+
mountDetail();
|
|
371
|
+
await settle();
|
|
372
|
+
|
|
373
|
+
button("mapo.delete").click();
|
|
374
|
+
await settle();
|
|
375
|
+
button("mapo.delete").click();
|
|
376
|
+
await settle();
|
|
377
|
+
|
|
378
|
+
expect(crud.delete).toHaveBeenCalledTimes(1);
|
|
379
|
+
expect(confirm.ask).toHaveBeenCalledTimes(1);
|
|
380
|
+
expect(button("mapo.delete").loading).toBe(true);
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
describe("MapoDetail annotations", () => {
|
|
385
|
+
it("passes the annotations to the body and the sidebar forms", async () => {
|
|
386
|
+
const annotations = { title: { label: "Suggested by AI" } };
|
|
387
|
+
mountDetail(
|
|
388
|
+
{},
|
|
389
|
+
{ annotations, sidebarFields: [{ key: "status", type: "text" }] },
|
|
390
|
+
);
|
|
391
|
+
await settle();
|
|
392
|
+
|
|
393
|
+
expect(forms).toHaveLength(2);
|
|
394
|
+
for (const form of forms) expect(form.annotations).toEqual(annotations);
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
describe("MapoDetail schema mode", () => {
|
|
399
|
+
// What Django REST Framework serves on OPTIONS, with no `schema`.
|
|
400
|
+
const drfMetadata = {
|
|
401
|
+
name: "News List",
|
|
402
|
+
actions: {
|
|
403
|
+
POST: {
|
|
404
|
+
title: { type: "string", required: true, label: "Title" },
|
|
405
|
+
status: {
|
|
406
|
+
type: "choice",
|
|
407
|
+
label: "Status",
|
|
408
|
+
choices: [{ value: "draft", display_name: "Draft" }],
|
|
409
|
+
},
|
|
410
|
+
author: { type: "field", required: true, label: "Author" },
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
it("builds the body fields from DRF metadata", async () => {
|
|
416
|
+
crud.options.mockResolvedValue(drfMetadata);
|
|
417
|
+
mountDetail({}, { experimentalFieldsFromSchema: true });
|
|
418
|
+
await settle();
|
|
419
|
+
|
|
420
|
+
const body = forms[0]!.fields!;
|
|
421
|
+
expect(body.map((field) => [field.key, field.type])).toEqual([
|
|
422
|
+
["title", "text"],
|
|
423
|
+
["status", "select"],
|
|
424
|
+
["author", "field:author"],
|
|
425
|
+
]);
|
|
426
|
+
expect(body[1]!.attrs?.options).toEqual([
|
|
427
|
+
{ text: "Draft", value: "draft" },
|
|
428
|
+
]);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it("reads the OPTIONS response with schemaFromMetadata when given", async () => {
|
|
432
|
+
crud.options.mockResolvedValue(drfMetadata);
|
|
433
|
+
const schemaFromMetadata = vi.fn(() => ({
|
|
434
|
+
type: "object",
|
|
435
|
+
properties: { headline: { type: "string", title: "Headline" } },
|
|
436
|
+
}));
|
|
437
|
+
mountDetail({}, { experimentalFieldsFromSchema: true, schemaFromMetadata });
|
|
438
|
+
await settle();
|
|
439
|
+
|
|
440
|
+
expect(schemaFromMetadata).toHaveBeenCalledWith(drfMetadata);
|
|
441
|
+
expect(forms[0]!.fields!.map((field) => field.key)).toEqual(["headline"]);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it("derives nothing without the opt-in", async () => {
|
|
445
|
+
crud.options.mockResolvedValue(drfMetadata);
|
|
446
|
+
mountDetail();
|
|
447
|
+
await settle();
|
|
448
|
+
|
|
449
|
+
expect(forms[0]!.fields).toEqual([]);
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
/** Mounts a component in `body-top` that keeps the context it reads. */
|
|
454
|
+
function mountWithProbe(
|
|
455
|
+
extraProps: Record<string, unknown> = {},
|
|
456
|
+
extraSlots: Record<string, (bindings: never) => VNode[]> = {},
|
|
457
|
+
) {
|
|
458
|
+
const seen: { detail?: MapoDetailContext } = {};
|
|
459
|
+
const Probe: Component = {
|
|
460
|
+
setup() {
|
|
461
|
+
seen.detail = useMapoDetail();
|
|
462
|
+
return () => h("i");
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
mountDetail({ "body-top": () => [h(Probe)], ...extraSlots }, extraProps);
|
|
466
|
+
return seen;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
describe("MapoDetail context", () => {
|
|
470
|
+
const fields = [{ key: "title", type: "text" }];
|
|
471
|
+
|
|
472
|
+
it("shares the record, the fields and the state with the components inside", async () => {
|
|
473
|
+
const seen = mountWithProbe({ fields, languages: ["it", "en"] });
|
|
474
|
+
await settle();
|
|
475
|
+
const detail = seen.detail!;
|
|
476
|
+
|
|
477
|
+
expect(detail.endpoint.value).toBe("/api/news/");
|
|
478
|
+
expect(detail.id.value).toBe(5);
|
|
479
|
+
expect(detail.isNew.value).toBe(false);
|
|
480
|
+
expect(detail.model.value).toEqual(record);
|
|
481
|
+
expect(detail.fields.value).toEqual(fields);
|
|
482
|
+
expect(detail.languages.value).toEqual(["it", "en"]);
|
|
483
|
+
expect(detail.readonly.value).toBe(false);
|
|
484
|
+
expect(detail.isDirty.value).toBe(false);
|
|
485
|
+
|
|
486
|
+
(detail.model.value as { title: string }).title = "Changed";
|
|
487
|
+
expect(detail.isDirty.value).toBe(true);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it("tells what the page names the record: its model, its permissions' model and how it reads OPTIONS", async () => {
|
|
491
|
+
const schemaFromMetadata = () => undefined;
|
|
492
|
+
const seen = mountWithProbe({
|
|
493
|
+
fields,
|
|
494
|
+
modelName: "Article",
|
|
495
|
+
permissionModel: "article",
|
|
496
|
+
schemaFromMetadata,
|
|
497
|
+
});
|
|
498
|
+
await settle();
|
|
499
|
+
const detail = seen.detail!;
|
|
500
|
+
|
|
501
|
+
expect(detail.modelName?.value).toBe("Article");
|
|
502
|
+
expect(detail.permissionModel?.value).toBe("article");
|
|
503
|
+
expect(detail.schemaFromMetadata?.value).toBe(schemaFromMetadata);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("names nothing the page does not name", async () => {
|
|
507
|
+
const seen = mountWithProbe({ fields });
|
|
508
|
+
await settle();
|
|
509
|
+
const detail = seen.detail!;
|
|
510
|
+
|
|
511
|
+
// Optional in the type: MapoDetail still gives each, empty.
|
|
512
|
+
expect(detail.modelName?.value).toBeNull();
|
|
513
|
+
expect(detail.permissionModel).toBeDefined();
|
|
514
|
+
expect(detail.permissionModel?.value).toBeUndefined();
|
|
515
|
+
expect(detail.schemaFromMetadata).toBeDefined();
|
|
516
|
+
expect(detail.schemaFromMetadata?.value).toBeUndefined();
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("tells the components inside of a save, with the record, before the page moves to it", async () => {
|
|
520
|
+
crud.create.mockResolvedValue({ id: 9, title: "Fresh" });
|
|
521
|
+
const steps: string[] = [];
|
|
522
|
+
router.replace.mockImplementation(async () => {
|
|
523
|
+
steps.push("replace");
|
|
524
|
+
});
|
|
525
|
+
const seen = mountWithProbe({ fields, id: "new" });
|
|
526
|
+
await settle();
|
|
527
|
+
const detail = seen.detail!;
|
|
528
|
+
detail.onSaved?.((saved) => {
|
|
529
|
+
steps.push(`saved ${(saved as { id: number }).id}`);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
await detail.save();
|
|
533
|
+
|
|
534
|
+
expect(steps).toEqual(["saved 9", "replace"]);
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
it("stops telling a handler once removed, and a handler that throws leaves the save done", async () => {
|
|
538
|
+
const seen = mountWithProbe({ fields });
|
|
539
|
+
await settle();
|
|
540
|
+
const detail = seen.detail!;
|
|
541
|
+
const removed = vi.fn();
|
|
542
|
+
const remove = detail.onSaved!(removed);
|
|
543
|
+
remove();
|
|
544
|
+
detail.onSaved!(() => {
|
|
545
|
+
throw new Error("a broken extension");
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
await detail.save();
|
|
549
|
+
|
|
550
|
+
expect(removed).not.toHaveBeenCalled();
|
|
551
|
+
expect(crud.partialUpdate).toHaveBeenCalledTimes(1);
|
|
552
|
+
expect(detail.isSaving.value).toBe(false);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it("takes the operator to a field through the form that holds it", async () => {
|
|
556
|
+
reveals.length = 0;
|
|
557
|
+
const seen = mountWithProbe({
|
|
558
|
+
fields,
|
|
559
|
+
sidebarFields: [{ key: "status", type: "select" }],
|
|
560
|
+
});
|
|
561
|
+
await settle();
|
|
562
|
+
const detail = seen.detail!;
|
|
563
|
+
|
|
564
|
+
await expect(detail.revealField?.("status")).resolves.toBe(true);
|
|
565
|
+
await expect(detail.revealField?.("title")).resolves.toBe(true);
|
|
566
|
+
await expect(detail.revealField?.("missing")).resolves.toBe(false);
|
|
567
|
+
|
|
568
|
+
expect(reveals).toEqual([
|
|
569
|
+
{ form: ["title"], path: "status" },
|
|
570
|
+
{ form: ["status"], path: "status" },
|
|
571
|
+
{ form: ["title"], path: "title" },
|
|
572
|
+
{ form: ["title"], path: "missing" },
|
|
573
|
+
{ form: ["status"], path: "missing" },
|
|
574
|
+
]);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("saves through the context as the buttons do", async () => {
|
|
578
|
+
const seen = mountWithProbe({ fields });
|
|
579
|
+
await settle();
|
|
580
|
+
|
|
581
|
+
await seen.detail!.save();
|
|
582
|
+
|
|
583
|
+
expect(crud.partialUpdate).toHaveBeenCalledTimes(1);
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it("shows the marks set from inside under the annotations prop", async () => {
|
|
587
|
+
const fromProp = { title: { label: "From the page" } };
|
|
588
|
+
const seen = mountWithProbe({ fields, annotations: fromProp });
|
|
589
|
+
await settle();
|
|
590
|
+
|
|
591
|
+
seen.detail!.annotations.value = {
|
|
592
|
+
title: { label: "From inside" },
|
|
593
|
+
slug: { label: "From inside" },
|
|
594
|
+
};
|
|
595
|
+
await settle();
|
|
596
|
+
|
|
597
|
+
expect(forms[0]!.annotations).toEqual({
|
|
598
|
+
title: { label: "From the page" },
|
|
599
|
+
slug: { label: "From inside" },
|
|
600
|
+
});
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
it("asks the endpoint for OPTIONS once, whoever asks", async () => {
|
|
604
|
+
crud.options.mockResolvedValue({ name: "News List" });
|
|
605
|
+
// No languages: the detail asks OPTIONS for them itself.
|
|
606
|
+
const seen = mountWithProbe({ fields });
|
|
607
|
+
await settle();
|
|
608
|
+
|
|
609
|
+
expect(await seen.detail!.metadata()).toEqual({ name: "News List" });
|
|
610
|
+
await seen.detail!.metadata();
|
|
611
|
+
expect(crud.options).toHaveBeenCalledTimes(1);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("answers metadata() with undefined when the endpoint has no OPTIONS", async () => {
|
|
615
|
+
crud.options.mockRejectedValue(new Error("405"));
|
|
616
|
+
const seen = mountWithProbe({ fields, languages: ["it"] });
|
|
617
|
+
await settle();
|
|
618
|
+
|
|
619
|
+
expect(await seen.detail!.metadata()).toBeUndefined();
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it("passes the fields rendered to the slots", async () => {
|
|
623
|
+
let bindings: { fields?: unknown; sidebarFields?: unknown } = {};
|
|
624
|
+
const sidebarFields = [{ key: "status", type: "text" }];
|
|
625
|
+
mountDetail(
|
|
626
|
+
{
|
|
627
|
+
"side-bottom": (slotProps: typeof bindings) => {
|
|
628
|
+
bindings = slotProps;
|
|
629
|
+
return [];
|
|
630
|
+
},
|
|
631
|
+
},
|
|
632
|
+
{ fields, sidebarFields },
|
|
633
|
+
);
|
|
634
|
+
await settle();
|
|
635
|
+
|
|
636
|
+
expect(bindings.fields).toEqual(fields);
|
|
637
|
+
expect(bindings.sidebarFields).toEqual(sidebarFields);
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
describe("MapoDetail extensions", () => {
|
|
642
|
+
/** A component that notes when it renders, under its name. */
|
|
643
|
+
const rendered: string[] = [];
|
|
644
|
+
const noting = (name: string): Component => ({
|
|
645
|
+
setup() {
|
|
646
|
+
rendered.push(name);
|
|
647
|
+
return () => h("i");
|
|
648
|
+
},
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
beforeEach(() => {
|
|
652
|
+
rendered.splice(0);
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
it("renders each extension in its place", async () => {
|
|
656
|
+
defineDetailExtension({
|
|
657
|
+
key: "fill",
|
|
658
|
+
placement: "actions",
|
|
659
|
+
component: noting("fill"),
|
|
660
|
+
});
|
|
661
|
+
defineDetailExtension({
|
|
662
|
+
key: "summary",
|
|
663
|
+
placement: "body-top",
|
|
664
|
+
component: noting("summary"),
|
|
665
|
+
});
|
|
666
|
+
mountDetail();
|
|
667
|
+
await settle();
|
|
668
|
+
|
|
669
|
+
expect(rendered.sort()).toEqual(["fill", "summary"]);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it("lets a page show none of them, or only some", async () => {
|
|
673
|
+
defineDetailExtension({
|
|
674
|
+
key: "a",
|
|
675
|
+
placement: "actions",
|
|
676
|
+
component: noting("a"),
|
|
677
|
+
});
|
|
678
|
+
defineDetailExtension({
|
|
679
|
+
key: "b",
|
|
680
|
+
placement: "actions",
|
|
681
|
+
component: noting("b"),
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
mountDetail({}, { extensions: false });
|
|
685
|
+
await settle();
|
|
686
|
+
expect(rendered).toEqual([]);
|
|
687
|
+
app?.unmount();
|
|
688
|
+
|
|
689
|
+
mountDetail({}, { extensions: ["b"] });
|
|
690
|
+
await settle();
|
|
691
|
+
expect(rendered).toEqual(["b"]);
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it("asks visible() with the context", async () => {
|
|
695
|
+
const visible = vi.fn((detail: MapoDetailContext) => !detail.isNew.value);
|
|
696
|
+
defineDetailExtension({
|
|
697
|
+
key: "duplicate",
|
|
698
|
+
placement: "actions",
|
|
699
|
+
component: noting("duplicate"),
|
|
700
|
+
visible,
|
|
701
|
+
});
|
|
702
|
+
mountDetail({}, { id: "new" });
|
|
703
|
+
await settle();
|
|
704
|
+
|
|
705
|
+
expect(visible).toHaveBeenCalled();
|
|
706
|
+
expect(rendered).toEqual([]);
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
it("orders them, replaces a key registered again, and removes them", async () => {
|
|
710
|
+
defineDetailExtension({
|
|
711
|
+
key: "late",
|
|
712
|
+
placement: "actions",
|
|
713
|
+
component: noting("late"),
|
|
714
|
+
order: 2,
|
|
715
|
+
});
|
|
716
|
+
defineDetailExtension({
|
|
717
|
+
key: "early",
|
|
718
|
+
placement: "actions",
|
|
719
|
+
component: noting("old"),
|
|
720
|
+
});
|
|
721
|
+
defineDetailExtension({
|
|
722
|
+
key: "early",
|
|
723
|
+
placement: "actions",
|
|
724
|
+
component: noting("early"),
|
|
725
|
+
order: 1,
|
|
726
|
+
});
|
|
727
|
+
const remove = defineDetailExtension({
|
|
728
|
+
key: "gone",
|
|
729
|
+
placement: "actions",
|
|
730
|
+
component: noting("gone"),
|
|
731
|
+
order: 3,
|
|
732
|
+
});
|
|
733
|
+
remove();
|
|
734
|
+
mountDetail();
|
|
735
|
+
await settle();
|
|
736
|
+
|
|
737
|
+
expect(rendered).toEqual(["early", "late"]);
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
it("loads a component from a loader", async () => {
|
|
741
|
+
const loaded = noting("loaded");
|
|
742
|
+
defineDetailExtension({
|
|
743
|
+
key: "lazy",
|
|
744
|
+
placement: "body-top",
|
|
745
|
+
component: () => Promise.resolve({ default: loaded }),
|
|
746
|
+
});
|
|
747
|
+
mountDetail();
|
|
748
|
+
// The record, then the loader, then the render of what it loaded.
|
|
749
|
+
for (let step = 0; step < 4; step++) await settle();
|
|
750
|
+
|
|
751
|
+
expect(rendered).toEqual(["loaded"]);
|
|
752
|
+
});
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
describe("MapoDetail drafts", () => {
|
|
756
|
+
const stored = new Map<string, string>();
|
|
757
|
+
const storage = {
|
|
758
|
+
getItem: (key: string) => stored.get(key) ?? null,
|
|
759
|
+
setItem: (key: string, value: string) => void stored.set(key, value),
|
|
760
|
+
removeItem: (key: string) => void stored.delete(key),
|
|
761
|
+
};
|
|
762
|
+
const fields = [
|
|
763
|
+
{ key: "title", type: "text", translatable: true },
|
|
764
|
+
{ key: "token", type: "text", translatable: true, noDraft: true },
|
|
765
|
+
];
|
|
766
|
+
const draftOf = (model: object, savedAt = Date.now()) =>
|
|
767
|
+
JSON.stringify({ model, savedAt });
|
|
768
|
+
|
|
769
|
+
beforeEach(() => {
|
|
770
|
+
stored.clear();
|
|
771
|
+
vi.stubGlobal("localStorage", storage);
|
|
772
|
+
// A record of its own each time: a change through the model writes into it.
|
|
773
|
+
crud.detail.mockImplementation(async () => ({ id: 5, title: "News" }));
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
afterEach(() => {
|
|
777
|
+
vi.useRealTimers();
|
|
778
|
+
vi.unstubAllGlobals();
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
/** Lets the autosave, debounced by two seconds, write. */
|
|
782
|
+
async function autosave(): Promise<void> {
|
|
783
|
+
await nextTick();
|
|
784
|
+
vi.advanceTimersByTime(2000);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
it("counts a new record as changed only once it holds a value", async () => {
|
|
788
|
+
const seen = mountWithProbe({ fields, id: "new" });
|
|
789
|
+
await settle();
|
|
790
|
+
const detail = seen.detail!;
|
|
791
|
+
const model = detail.model.value as Record<string, unknown>;
|
|
792
|
+
expect(detail.isDirty.value).toBe(false);
|
|
793
|
+
|
|
794
|
+
// Blank texts, an editor's empty paragraph, empty lists and objects.
|
|
795
|
+
model.slug = "";
|
|
796
|
+
model.translations = { en: { title: "<p><br></p>" }, it: {} };
|
|
797
|
+
model.tags = [];
|
|
798
|
+
expect(detail.isDirty.value).toBe(false);
|
|
799
|
+
|
|
800
|
+
model.featured = false;
|
|
801
|
+
expect(detail.isDirty.value).toBe(true);
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
it("counts what the page put in a new record before it opened as no change", async () => {
|
|
805
|
+
// A component inside sets a value up, as a page that prefills a record.
|
|
806
|
+
const seen: { detail?: MapoDetailContext } = {};
|
|
807
|
+
const Prefill: Component = {
|
|
808
|
+
setup() {
|
|
809
|
+
seen.detail = useMapoDetail();
|
|
810
|
+
(seen.detail!.model.value as Record<string, unknown>).category = 3;
|
|
811
|
+
return () => h("i");
|
|
812
|
+
},
|
|
813
|
+
};
|
|
814
|
+
mountDetail({ "body-top": () => [h(Prefill)] }, { fields, id: "new" });
|
|
815
|
+
await settle();
|
|
816
|
+
const detail = seen.detail!;
|
|
817
|
+
const model = detail.model.value as Record<string, unknown>;
|
|
818
|
+
expect(detail.isDirty.value).toBe(false);
|
|
819
|
+
|
|
820
|
+
model.slug = "hello";
|
|
821
|
+
expect(detail.isDirty.value).toBe(true);
|
|
822
|
+
model.slug = "";
|
|
823
|
+
expect(detail.isDirty.value).toBe(false);
|
|
824
|
+
model.category = 4;
|
|
825
|
+
expect(detail.isDirty.value).toBe(true);
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it("asks before leaving a new record that holds a value, and before closing the tab", async () => {
|
|
829
|
+
const unloads: ((event: object) => void)[] = [];
|
|
830
|
+
vi.stubGlobal("addEventListener", (type: string, handler: () => void) => {
|
|
831
|
+
if (type === "beforeunload") unloads.push(handler);
|
|
832
|
+
});
|
|
833
|
+
vi.stubGlobal("removeEventListener", () => {});
|
|
834
|
+
const seen = mountWithProbe({ fields, id: "new" });
|
|
835
|
+
await settle();
|
|
836
|
+
const [leave] = leaveGuards;
|
|
837
|
+
const left = vi.fn();
|
|
838
|
+
leave!({}, {}, left);
|
|
839
|
+
await settle();
|
|
840
|
+
expect(confirm.ask).not.toHaveBeenCalled();
|
|
841
|
+
expect(left).toHaveBeenCalledWith(true);
|
|
842
|
+
|
|
843
|
+
(seen.detail!.model.value as Record<string, unknown>).slug = "hello";
|
|
844
|
+
confirm.ask.mockResolvedValue(false);
|
|
845
|
+
const kept = vi.fn();
|
|
846
|
+
leave!({}, {}, kept);
|
|
847
|
+
await settle();
|
|
848
|
+
expect(confirm.ask).toHaveBeenCalledTimes(1);
|
|
849
|
+
expect(kept).toHaveBeenCalledWith(false);
|
|
850
|
+
|
|
851
|
+
const closing = { preventDefault: vi.fn(), returnValue: undefined };
|
|
852
|
+
for (const handler of unloads) handler(closing);
|
|
853
|
+
expect(closing.preventDefault).toHaveBeenCalled();
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
it("keeps a new record's draft under the user, without the noDraft fields", async () => {
|
|
857
|
+
const sidebarFields = [{ key: "pin", type: "text", noDraft: true }];
|
|
858
|
+
const seen = mountWithProbe({
|
|
859
|
+
fields,
|
|
860
|
+
sidebarFields,
|
|
861
|
+
id: "new",
|
|
862
|
+
draft: true,
|
|
863
|
+
});
|
|
864
|
+
await settle();
|
|
865
|
+
vi.useFakeTimers();
|
|
866
|
+
const model = seen.detail!.model.value as Record<string, unknown>;
|
|
867
|
+
model.translations = {
|
|
868
|
+
en: { title: "Hello", token: "t-en" },
|
|
869
|
+
it: { token: "t-it" },
|
|
870
|
+
};
|
|
871
|
+
model.pin = "1234";
|
|
872
|
+
await autosave();
|
|
873
|
+
|
|
874
|
+
expect([...stored.keys()]).toEqual(["mapo:draft:7:/api/news/:new"]);
|
|
875
|
+
const saved = JSON.parse(stored.get("mapo:draft:7:/api/news/:new")!);
|
|
876
|
+
expect(saved.model).toEqual({
|
|
877
|
+
translations: { en: { title: "Hello" }, it: {} },
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
it("removes the draft it wrote once the record is back as loaded, and leaves an earlier one", async () => {
|
|
882
|
+
stored.set("mapo:draft:7:/api/news/:5", draftOf({ title: "Earlier" }));
|
|
883
|
+
const seen = mountWithProbe(
|
|
884
|
+
{ fields, draft: true },
|
|
885
|
+
{ "draft-banner": () => [h("i")] },
|
|
886
|
+
);
|
|
887
|
+
await settle();
|
|
888
|
+
vi.useFakeTimers();
|
|
889
|
+
const model = seen.detail!.model.value as Record<string, unknown>;
|
|
890
|
+
// The record loads: nothing changed, and the earlier draft stays.
|
|
891
|
+
await autosave();
|
|
892
|
+
expect(stored.has("mapo:draft:7:/api/news/:5")).toBe(true);
|
|
893
|
+
|
|
894
|
+
model.title = "Changed";
|
|
895
|
+
await autosave();
|
|
896
|
+
const saved = JSON.parse(stored.get("mapo:draft:7:/api/news/:5")!);
|
|
897
|
+
expect(saved.model.title).toBe("Changed");
|
|
898
|
+
|
|
899
|
+
model.title = "News";
|
|
900
|
+
await autosave();
|
|
901
|
+
expect(stored.size).toBe(0);
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
it("never offers a draft kept without its user, and removes it, nor another user's", async () => {
|
|
905
|
+
stored.set("mapo:draft:/api/news/:5", draftOf({ title: "Whose?" }));
|
|
906
|
+
stored.set("mapo:draft:8:/api/news/:5", draftOf({ title: "Another's" }));
|
|
907
|
+
const found = vi.fn();
|
|
908
|
+
mountWithProbe({ fields, draft: true, onDraftFound: found });
|
|
909
|
+
await settle();
|
|
910
|
+
|
|
911
|
+
expect(found).not.toHaveBeenCalled();
|
|
912
|
+
expect(stored.has("mapo:draft:/api/news/:5")).toBe(false);
|
|
913
|
+
expect(stored.has("mapo:draft:8:/api/news/:5")).toBe(true);
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
it("offers the user's draft, and restores it or discards it", async () => {
|
|
917
|
+
stored.set("mapo:draft:7:/api/news/:5", draftOf({ title: "Draft" }));
|
|
918
|
+
const found = vi.fn();
|
|
919
|
+
// A banner of the page's own: the built-in one leaves with a transition,
|
|
920
|
+
// which needs a DOM. An empty slot would render the built-in one.
|
|
921
|
+
const seen = mountWithProbe(
|
|
922
|
+
{ fields, draft: true, onDraftFound: found },
|
|
923
|
+
{ "draft-banner": () => [h("i")] },
|
|
924
|
+
);
|
|
925
|
+
await settle();
|
|
926
|
+
// The autosave a change schedules never fires after the test.
|
|
927
|
+
vi.useFakeTimers();
|
|
928
|
+
expect(found).toHaveBeenCalledTimes(1);
|
|
929
|
+
const [draft, , restore, discard] = found.mock.calls[0]!;
|
|
930
|
+
expect(draft).toEqual({ title: "Draft" });
|
|
931
|
+
|
|
932
|
+
restore();
|
|
933
|
+
expect(seen.detail!.model.value).toEqual({ id: 5, title: "Draft" });
|
|
934
|
+
discard();
|
|
935
|
+
expect(stored.size).toBe(0);
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
it("drops a draft older than a day, and clears the draft once saved", async () => {
|
|
939
|
+
const dayAndMinute = 86_400_000 + 60_000;
|
|
940
|
+
stored.set(
|
|
941
|
+
"mapo:draft:7:/api/news/:5",
|
|
942
|
+
draftOf({ title: "Stale" }, Date.now() - dayAndMinute),
|
|
943
|
+
);
|
|
944
|
+
const found = vi.fn();
|
|
945
|
+
const seen = mountWithProbe({ fields, draft: true, onDraftFound: found });
|
|
946
|
+
await settle();
|
|
947
|
+
vi.useFakeTimers();
|
|
948
|
+
expect(found).not.toHaveBeenCalled();
|
|
949
|
+
expect(stored.size).toBe(0);
|
|
950
|
+
|
|
951
|
+
stored.set("mapo:draft:7:/api/news/:5", draftOf({ title: "Draft" }));
|
|
952
|
+
await seen.detail!.save();
|
|
953
|
+
expect(stored.size).toBe(0);
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
describe("MapoDetail draft parts", () => {
|
|
958
|
+
const stored = new Map<string, string>();
|
|
959
|
+
const storage = {
|
|
960
|
+
getItem: (key: string) => stored.get(key) ?? null,
|
|
961
|
+
setItem: (key: string, value: string) => void stored.set(key, value),
|
|
962
|
+
removeItem: (key: string) => void stored.delete(key),
|
|
963
|
+
};
|
|
964
|
+
const fields = [{ key: "title", type: "text" }];
|
|
965
|
+
const key = "mapo:draft:7:/api/news/:5";
|
|
966
|
+
/** A page with drafts and a banner of its own: the built-in one needs a DOM to leave. */
|
|
967
|
+
const mountDrafting = (props: Record<string, unknown> = {}) =>
|
|
968
|
+
mountWithProbe(
|
|
969
|
+
{ fields, draft: true, ...props },
|
|
970
|
+
{ "draft-banner": () => [h("i")] },
|
|
971
|
+
);
|
|
972
|
+
|
|
973
|
+
beforeEach(() => {
|
|
974
|
+
stored.clear();
|
|
975
|
+
vi.stubGlobal("localStorage", storage);
|
|
976
|
+
crud.detail.mockImplementation(async () => ({ id: 5, title: "News" }));
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
afterEach(() => {
|
|
980
|
+
vi.useRealTimers();
|
|
981
|
+
vi.unstubAllGlobals();
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
async function autosave(): Promise<void> {
|
|
985
|
+
await nextTick();
|
|
986
|
+
vi.advanceTimersByTime(2000);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
it("keeps what a part saves, on touch(), with the record unchanged", async () => {
|
|
990
|
+
const seen = mountDrafting();
|
|
991
|
+
await settle();
|
|
992
|
+
vi.useFakeTimers();
|
|
993
|
+
const draft = seen.detail!.draft!;
|
|
994
|
+
let notes: object | undefined = { n: 1 };
|
|
995
|
+
draft.part("notes", { save: () => notes, restore: () => {} });
|
|
996
|
+
draft.part("empty", { save: () => undefined, restore: () => {} });
|
|
997
|
+
|
|
998
|
+
draft.touch();
|
|
999
|
+
await autosave();
|
|
1000
|
+
const saved = JSON.parse(stored.get(key)!);
|
|
1001
|
+
expect(saved.model).toEqual({ id: 5, title: "News" });
|
|
1002
|
+
expect(saved.parts).toEqual({ notes: { n: 1 } });
|
|
1003
|
+
|
|
1004
|
+
// Nothing left to keep: the draft this page wrote goes.
|
|
1005
|
+
notes = undefined;
|
|
1006
|
+
draft.touch();
|
|
1007
|
+
await autosave();
|
|
1008
|
+
expect(stored.has(key)).toBe(false);
|
|
1009
|
+
});
|
|
1010
|
+
|
|
1011
|
+
it("leaves out a part that throws, and keeps the others", async () => {
|
|
1012
|
+
const seen = mountDrafting();
|
|
1013
|
+
await settle();
|
|
1014
|
+
vi.useFakeTimers();
|
|
1015
|
+
const draft = seen.detail!.draft!;
|
|
1016
|
+
draft.part("broken", {
|
|
1017
|
+
save: () => {
|
|
1018
|
+
throw new Error("a broken extension");
|
|
1019
|
+
},
|
|
1020
|
+
restore: () => {},
|
|
1021
|
+
});
|
|
1022
|
+
draft.part("notes", { save: () => ({ n: 1 }), restore: () => {} });
|
|
1023
|
+
|
|
1024
|
+
draft.touch();
|
|
1025
|
+
await autosave();
|
|
1026
|
+
expect(JSON.parse(stored.get(key)!).parts).toEqual({ notes: { n: 1 } });
|
|
1027
|
+
});
|
|
1028
|
+
|
|
1029
|
+
it("restores the parts after the values, and a part that comes later gets its data then", async () => {
|
|
1030
|
+
stored.set(
|
|
1031
|
+
key,
|
|
1032
|
+
JSON.stringify({
|
|
1033
|
+
model: { title: "Draft" },
|
|
1034
|
+
savedAt: Date.now(),
|
|
1035
|
+
parts: { notes: { n: 2 }, later: { m: 3 } },
|
|
1036
|
+
}),
|
|
1037
|
+
);
|
|
1038
|
+
const found = vi.fn();
|
|
1039
|
+
const seen = mountDrafting({ onDraftFound: found });
|
|
1040
|
+
await settle();
|
|
1041
|
+
vi.useFakeTimers();
|
|
1042
|
+
const detail = seen.detail!;
|
|
1043
|
+
const titles: unknown[] = [];
|
|
1044
|
+
const notes = vi.fn(() => {
|
|
1045
|
+
titles.push((detail.model.value as { title: string }).title);
|
|
1046
|
+
});
|
|
1047
|
+
const fresh = vi.fn();
|
|
1048
|
+
detail.draft!.part("notes", { save: () => undefined, restore: notes });
|
|
1049
|
+
detail.draft!.part("fresh", { save: () => undefined, restore: fresh });
|
|
1050
|
+
|
|
1051
|
+
const [, , restore] = found.mock.calls[0]!;
|
|
1052
|
+
restore();
|
|
1053
|
+
expect(notes).toHaveBeenCalledWith({ n: 2 });
|
|
1054
|
+
expect(titles).toEqual(["Draft"]);
|
|
1055
|
+
// The draft holds nothing for it: it starts again.
|
|
1056
|
+
expect(fresh).toHaveBeenCalledWith(undefined);
|
|
1057
|
+
|
|
1058
|
+
const later = vi.fn();
|
|
1059
|
+
detail.draft!.part("later", { save: () => undefined, restore: later });
|
|
1060
|
+
expect(later).toHaveBeenCalledWith({ m: 3 });
|
|
1061
|
+
});
|
|
1062
|
+
|
|
1063
|
+
it("gives a later part nothing once the record is saved, and stops asking a part removed", async () => {
|
|
1064
|
+
stored.set(
|
|
1065
|
+
key,
|
|
1066
|
+
JSON.stringify({
|
|
1067
|
+
model: { title: "Draft" },
|
|
1068
|
+
savedAt: Date.now(),
|
|
1069
|
+
parts: { later: { m: 3 } },
|
|
1070
|
+
}),
|
|
1071
|
+
);
|
|
1072
|
+
const found = vi.fn();
|
|
1073
|
+
const seen = mountDrafting({ onDraftFound: found });
|
|
1074
|
+
await settle();
|
|
1075
|
+
vi.useFakeTimers();
|
|
1076
|
+
const detail = seen.detail!;
|
|
1077
|
+
const removed = vi.fn(() => ({ n: 1 }));
|
|
1078
|
+
const remove = detail.draft!.part("gone", {
|
|
1079
|
+
save: removed,
|
|
1080
|
+
restore: () => {},
|
|
1081
|
+
});
|
|
1082
|
+
remove();
|
|
1083
|
+
|
|
1084
|
+
found.mock.calls[0]![2]();
|
|
1085
|
+
await detail.save();
|
|
1086
|
+
const later = vi.fn();
|
|
1087
|
+
detail.draft!.part("later", { save: () => undefined, restore: later });
|
|
1088
|
+
expect(later).not.toHaveBeenCalled();
|
|
1089
|
+
|
|
1090
|
+
detail.draft!.touch();
|
|
1091
|
+
await autosave();
|
|
1092
|
+
expect(removed).not.toHaveBeenCalled();
|
|
1093
|
+
expect(stored.has(key)).toBe(false);
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
it("keeps nothing with drafts off", async () => {
|
|
1097
|
+
const seen = mountWithProbe({ fields });
|
|
1098
|
+
await settle();
|
|
1099
|
+
vi.useFakeTimers();
|
|
1100
|
+
const draft = seen.detail!.draft!;
|
|
1101
|
+
draft.part("notes", { save: () => ({ n: 1 }), restore: () => {} });
|
|
1102
|
+
|
|
1103
|
+
draft.touch();
|
|
1104
|
+
await autosave();
|
|
1105
|
+
expect(stored.size).toBe(0);
|
|
1106
|
+
});
|
|
1107
|
+
});
|