@mapomodule/uikit 2.0.0-beta.8 → 2.0.0-experimental.2
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,504 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, nextTick, onUnmounted, reactive } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext, VNode } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoListTable from "../runtime/components/MapoListTable.vue";
|
|
6
|
+
import { ALL_ROWS } from "../runtime/utils/listTable";
|
|
7
|
+
|
|
8
|
+
// The table reads the route, the stores and the CRUD layer at setup: replace them
|
|
9
|
+
// with in-memory doubles. `vi.hoisted` lets the mock factories below share them.
|
|
10
|
+
const route = vi.hoisted(() => ({ query: {} as Record<string, string> }));
|
|
11
|
+
const router = vi.hoisted(() => ({ replace: vi.fn() }));
|
|
12
|
+
const crud = vi.hoisted(() => ({
|
|
13
|
+
list: vi.fn(),
|
|
14
|
+
delete: vi.fn(),
|
|
15
|
+
updateOrder: vi.fn(),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
vi.mock("#app", () => ({ useRoute: () => route, useRouter: () => router }));
|
|
19
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
20
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
21
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
22
|
+
}));
|
|
23
|
+
vi.mock("@mapomodule/store/runtime/stores/confirm", () => ({
|
|
24
|
+
useConfirmStore: () => ({ ask: vi.fn() }),
|
|
25
|
+
}));
|
|
26
|
+
vi.mock("@mapomodule/store/runtime/composables/usePermissions", () => ({
|
|
27
|
+
usePermissions: () => ({ canChange: () => true, canDelete: () => true }),
|
|
28
|
+
}));
|
|
29
|
+
vi.mock("@mapomodule/core/runtime/api/crud", () => ({ useCrud: () => crud }));
|
|
30
|
+
|
|
31
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
32
|
+
// enough to mount the component with real reactivity, watchers and events.
|
|
33
|
+
interface HeadlessNode {
|
|
34
|
+
parent: HeadlessNode | null;
|
|
35
|
+
children: HeadlessNode[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
39
|
+
|
|
40
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
41
|
+
createElement: headlessNode,
|
|
42
|
+
createText: headlessNode,
|
|
43
|
+
createComment: headlessNode,
|
|
44
|
+
setText: () => {},
|
|
45
|
+
setElementText: () => {},
|
|
46
|
+
patchProp: () => {},
|
|
47
|
+
insert(child, parent, anchor) {
|
|
48
|
+
child.parent = parent;
|
|
49
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
50
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
51
|
+
},
|
|
52
|
+
remove(child) {
|
|
53
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
54
|
+
child.parent = null;
|
|
55
|
+
},
|
|
56
|
+
parentNode: (node) => node.parent,
|
|
57
|
+
nextSibling: (node) =>
|
|
58
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const passthrough = (name: string): Component => ({
|
|
62
|
+
name,
|
|
63
|
+
inheritAttrs: false,
|
|
64
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
65
|
+
return () => h("div", null, slots.default?.());
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// The stubs whose binding is under test declare the real component's contract.
|
|
70
|
+
|
|
71
|
+
// `UPagination` models `page` (prop `page`, event `update:page`), not `modelValue`.
|
|
72
|
+
let pager: { readonly page: number | undefined; select(page: number): void };
|
|
73
|
+
let pagerMounted = false;
|
|
74
|
+
|
|
75
|
+
const UPagination: Component = {
|
|
76
|
+
name: "UPagination",
|
|
77
|
+
props: { page: Number, total: Number, itemsPerPage: Number },
|
|
78
|
+
emits: ["update:page"],
|
|
79
|
+
setup(props: { page?: number }, { emit }: SetupContext<["update:page"]>) {
|
|
80
|
+
pagerMounted = true;
|
|
81
|
+
onUnmounted(() => {
|
|
82
|
+
pagerMounted = false;
|
|
83
|
+
});
|
|
84
|
+
pager = {
|
|
85
|
+
get page() {
|
|
86
|
+
return props.page;
|
|
87
|
+
},
|
|
88
|
+
select: (page) => emit("update:page", page),
|
|
89
|
+
};
|
|
90
|
+
return () => h("nav");
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// `UTable` gets TanStack column defs; the tests render headers and cells from them.
|
|
95
|
+
interface ColumnDef {
|
|
96
|
+
id?: string;
|
|
97
|
+
header?: string | (() => VNode);
|
|
98
|
+
cell?: (context: { row: { original: object; index: number } }) => unknown;
|
|
99
|
+
}
|
|
100
|
+
let table: {
|
|
101
|
+
readonly data: Record<string, unknown>[];
|
|
102
|
+
readonly columns: ColumnDef[];
|
|
103
|
+
readonly empty: string | undefined;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const UTable: Component = {
|
|
107
|
+
name: "UTable",
|
|
108
|
+
props: { data: Array, columns: Array, empty: String },
|
|
109
|
+
setup(props: {
|
|
110
|
+
data?: Record<string, unknown>[];
|
|
111
|
+
columns?: ColumnDef[];
|
|
112
|
+
empty?: string;
|
|
113
|
+
}) {
|
|
114
|
+
table = {
|
|
115
|
+
get data() {
|
|
116
|
+
return props.data ?? [];
|
|
117
|
+
},
|
|
118
|
+
get columns() {
|
|
119
|
+
return props.columns ?? [];
|
|
120
|
+
},
|
|
121
|
+
get empty() {
|
|
122
|
+
return props.empty;
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
return () => h("table");
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// `USelect` is the per-page selector.
|
|
130
|
+
interface SelectItem {
|
|
131
|
+
label: string;
|
|
132
|
+
value: number;
|
|
133
|
+
}
|
|
134
|
+
let pageSizeSelect: {
|
|
135
|
+
readonly items: SelectItem[];
|
|
136
|
+
select(value: number): void;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const USelect: Component = {
|
|
140
|
+
name: "USelect",
|
|
141
|
+
props: { modelValue: Number, items: Array },
|
|
142
|
+
emits: ["update:modelValue"],
|
|
143
|
+
setup(
|
|
144
|
+
props: { items?: SelectItem[] },
|
|
145
|
+
{ emit }: SetupContext<["update:modelValue"]>,
|
|
146
|
+
) {
|
|
147
|
+
pageSizeSelect = {
|
|
148
|
+
get items() {
|
|
149
|
+
return props.items ?? [];
|
|
150
|
+
},
|
|
151
|
+
select: (value) => emit("update:modelValue", value),
|
|
152
|
+
};
|
|
153
|
+
return () => h("select");
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const stubs: Record<string, Component> = {
|
|
158
|
+
UPagination,
|
|
159
|
+
UTable,
|
|
160
|
+
USelect,
|
|
161
|
+
...Object.fromEntries(
|
|
162
|
+
[
|
|
163
|
+
"UInput",
|
|
164
|
+
"UIcon",
|
|
165
|
+
"UCheckbox",
|
|
166
|
+
"UButton",
|
|
167
|
+
"NuxtLink",
|
|
168
|
+
"MapoListQuickEdit",
|
|
169
|
+
].map((name) => [name, passthrough(name)]),
|
|
170
|
+
),
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
let app: App<HeadlessNode> | undefined;
|
|
174
|
+
|
|
175
|
+
/** Mounts the table; the returned props stay reactive, so a test can change them. */
|
|
176
|
+
function mountTable(initial: Record<string, unknown>) {
|
|
177
|
+
const props = reactive({ ...initial });
|
|
178
|
+
const root: Component = {
|
|
179
|
+
render: () => h(MapoListTable as Component, props),
|
|
180
|
+
};
|
|
181
|
+
app = createApp(root);
|
|
182
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
183
|
+
app.mount(headlessNode());
|
|
184
|
+
return props;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Runs the pending fetches, the watchers they trigger and the debounced URL
|
|
188
|
+
// writes (timers are fake, so nothing leaks into the next test).
|
|
189
|
+
const settle = () => vi.runAllTimersAsync();
|
|
190
|
+
|
|
191
|
+
const rows = (count: number) =>
|
|
192
|
+
Array.from({ length: count }, (_, i) => ({
|
|
193
|
+
id: i + 1,
|
|
194
|
+
title: `Row ${i + 1}`,
|
|
195
|
+
}));
|
|
196
|
+
|
|
197
|
+
const lastListParams = () =>
|
|
198
|
+
crud.list.mock.lastCall?.[0] as Record<string, unknown>;
|
|
199
|
+
|
|
200
|
+
const column = (id: string) => {
|
|
201
|
+
const found = table.columns.find((col) => col.id === id);
|
|
202
|
+
if (!found) throw new Error(`no column ${id}`);
|
|
203
|
+
return found;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** The header button of a sortable column, as the table renders it. */
|
|
207
|
+
const sortButton = (id: string) => {
|
|
208
|
+
const header = column(id).header;
|
|
209
|
+
if (typeof header !== "function") throw new Error(`${id} is not sortable`);
|
|
210
|
+
return header().props as {
|
|
211
|
+
"aria-label": string;
|
|
212
|
+
icon: string;
|
|
213
|
+
onClick: () => void;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const listProps = {
|
|
218
|
+
endpoint: "/api/news/",
|
|
219
|
+
columns: [{ key: "title", label: "Title" }],
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
beforeEach(() => {
|
|
223
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
224
|
+
route.query = {};
|
|
225
|
+
router.replace.mockReset();
|
|
226
|
+
crud.list.mockReset();
|
|
227
|
+
crud.list.mockResolvedValue({ results: rows(20), count: 95 });
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
afterEach(() => {
|
|
231
|
+
app?.unmount();
|
|
232
|
+
app = undefined;
|
|
233
|
+
vi.useRealTimers();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe("MapoListTable pagination", () => {
|
|
237
|
+
it("loads the page in the URL and shows it as the current one", async () => {
|
|
238
|
+
route.query = { page: "3" };
|
|
239
|
+
mountTable(listProps);
|
|
240
|
+
await settle();
|
|
241
|
+
|
|
242
|
+
expect(crud.list).toHaveBeenCalledTimes(1);
|
|
243
|
+
expect(crud.list).toHaveBeenLastCalledWith(
|
|
244
|
+
expect.objectContaining({ page: 3, page_size: 20 }),
|
|
245
|
+
);
|
|
246
|
+
expect(pager.page).toBe(3);
|
|
247
|
+
// The first load is not a change: the URL keeps its page.
|
|
248
|
+
expect(router.replace).not.toHaveBeenCalled();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("loads the page picked in the pager and writes it to the URL", async () => {
|
|
252
|
+
mountTable(listProps);
|
|
253
|
+
await settle();
|
|
254
|
+
|
|
255
|
+
pager.select(2);
|
|
256
|
+
await settle();
|
|
257
|
+
|
|
258
|
+
expect(crud.list).toHaveBeenLastCalledWith(
|
|
259
|
+
expect.objectContaining({ page: 2, page_size: 20 }),
|
|
260
|
+
);
|
|
261
|
+
expect(pager.page).toBe(2);
|
|
262
|
+
expect(router.replace).toHaveBeenLastCalledWith({ query: { page: "2" } });
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("goes back to the first page when the filters change", async () => {
|
|
266
|
+
route.query = { page: "3" };
|
|
267
|
+
const props = mountTable(listProps);
|
|
268
|
+
await settle();
|
|
269
|
+
|
|
270
|
+
props.filterParams = { status: "draft" };
|
|
271
|
+
await settle();
|
|
272
|
+
|
|
273
|
+
expect(crud.list).toHaveBeenLastCalledWith(
|
|
274
|
+
expect.objectContaining({ status: "draft", page: 1 }),
|
|
275
|
+
);
|
|
276
|
+
expect(pager.page).toBe(1);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("drops a pending URL write when the list goes away", async () => {
|
|
280
|
+
mountTable(listProps);
|
|
281
|
+
await settle();
|
|
282
|
+
|
|
283
|
+
pager.select(2);
|
|
284
|
+
await nextTick(); // the watcher runs and arms the debounced URL write
|
|
285
|
+
app?.unmount();
|
|
286
|
+
app = undefined;
|
|
287
|
+
await settle();
|
|
288
|
+
|
|
289
|
+
expect(router.replace).not.toHaveBeenCalled();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("uses paginationParams instead of page and page_size", async () => {
|
|
293
|
+
mountTable({
|
|
294
|
+
...listProps,
|
|
295
|
+
paginationParams: ({
|
|
296
|
+
page,
|
|
297
|
+
pageSize,
|
|
298
|
+
}: {
|
|
299
|
+
page: number;
|
|
300
|
+
pageSize: number;
|
|
301
|
+
}) => ({
|
|
302
|
+
offset: (page - 1) * pageSize,
|
|
303
|
+
limit: pageSize,
|
|
304
|
+
}),
|
|
305
|
+
});
|
|
306
|
+
await settle();
|
|
307
|
+
|
|
308
|
+
expect(lastListParams()).toEqual({ offset: 0, limit: 20 });
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe("MapoListTable sorting", () => {
|
|
313
|
+
const sortableProps = {
|
|
314
|
+
endpoint: "/api/news/",
|
|
315
|
+
columns: [
|
|
316
|
+
{ key: "title", label: "Title", sortable: true },
|
|
317
|
+
{ key: "status", label: "Status" },
|
|
318
|
+
],
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
it("cycles a sortable header through ascending, descending and unsorted", async () => {
|
|
322
|
+
mountTable(sortableProps);
|
|
323
|
+
await settle();
|
|
324
|
+
expect(sortButton("title")["aria-label"]).toBe(
|
|
325
|
+
"mapo.listTable.sortAscending",
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
sortButton("title").onClick();
|
|
329
|
+
await settle();
|
|
330
|
+
expect(lastListParams().ordering).toBe("title");
|
|
331
|
+
expect(router.replace).toHaveBeenLastCalledWith({
|
|
332
|
+
query: { ordering: "title" },
|
|
333
|
+
});
|
|
334
|
+
expect(sortButton("title")["aria-label"]).toBe(
|
|
335
|
+
"mapo.listTable.sortDescending",
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
sortButton("title").onClick();
|
|
339
|
+
await settle();
|
|
340
|
+
expect(lastListParams().ordering).toBe("-title");
|
|
341
|
+
expect(sortButton("title")["aria-label"]).toBe(
|
|
342
|
+
"mapo.listTable.clearSorting",
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
sortButton("title").onClick();
|
|
346
|
+
await settle();
|
|
347
|
+
expect(lastListParams()).not.toHaveProperty("ordering");
|
|
348
|
+
expect(router.replace).toHaveBeenLastCalledWith({ query: {} });
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("restores the order from the URL into the header", async () => {
|
|
352
|
+
route.query = { ordering: "-title" };
|
|
353
|
+
mountTable(sortableProps);
|
|
354
|
+
await settle();
|
|
355
|
+
|
|
356
|
+
expect(lastListParams().ordering).toBe("-title");
|
|
357
|
+
expect(sortButton("title").icon).toBe("i-lucide-arrow-down-wide-narrow");
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("keeps a plain header on a column that is not sortable", async () => {
|
|
361
|
+
mountTable(sortableProps);
|
|
362
|
+
await settle();
|
|
363
|
+
|
|
364
|
+
expect(column("status").header).toBe("Status");
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
describe("MapoListTable nested column keys", () => {
|
|
369
|
+
const analyses = [
|
|
370
|
+
{ id: 1, title: "Glucose", category: { name: "Blood" } },
|
|
371
|
+
{ id: 2, title: "Urea", category: { name: "Urine" } },
|
|
372
|
+
{ id: 3, title: "Iron", category: null },
|
|
373
|
+
];
|
|
374
|
+
const nestedColumns = [
|
|
375
|
+
{ key: "title", label: "Title" },
|
|
376
|
+
{ key: "category.name", label: "Category", sortable: true },
|
|
377
|
+
];
|
|
378
|
+
|
|
379
|
+
it("shows a related object's field and sorts on it with a Django lookup", async () => {
|
|
380
|
+
crud.list.mockResolvedValue({ results: analyses, count: 3 });
|
|
381
|
+
mountTable({ endpoint: "/api/analyses/", columns: nestedColumns });
|
|
382
|
+
await settle();
|
|
383
|
+
|
|
384
|
+
const cell = column("category.name").cell!;
|
|
385
|
+
expect(cell({ row: { original: analyses[0]!, index: 0 } })).toBe("Blood");
|
|
386
|
+
expect(cell({ row: { original: analyses[2]!, index: 2 } })).toBe("—");
|
|
387
|
+
|
|
388
|
+
sortButton("category.name").onClick();
|
|
389
|
+
await settle();
|
|
390
|
+
expect(lastListParams().ordering).toBe("category__name");
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const shownTitles = () => table.data.map((row) => row.title);
|
|
394
|
+
|
|
395
|
+
it("sorts nested values in offline mode", async () => {
|
|
396
|
+
mountTable({ items: analyses, columns: nestedColumns });
|
|
397
|
+
await settle();
|
|
398
|
+
|
|
399
|
+
sortButton("category.name").onClick();
|
|
400
|
+
sortButton("category.name").onClick(); // descending
|
|
401
|
+
await settle();
|
|
402
|
+
|
|
403
|
+
// A row without the related object goes last, as for any empty value.
|
|
404
|
+
expect(shownTitles()).toEqual(["Urea", "Glucose", "Iron"]);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("searches nested values in offline mode", async () => {
|
|
408
|
+
route.query = { search: "blood" };
|
|
409
|
+
mountTable({ items: analyses, columns: nestedColumns });
|
|
410
|
+
await settle();
|
|
411
|
+
|
|
412
|
+
expect(shownTitles()).toEqual(["Glucose"]);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("hands bulk actions the same ordering as the list request", async () => {
|
|
416
|
+
const onSelectionQuery = vi.fn();
|
|
417
|
+
crud.list.mockResolvedValue({ results: analyses, count: 3 });
|
|
418
|
+
mountTable({
|
|
419
|
+
endpoint: "/api/analyses/",
|
|
420
|
+
columns: nestedColumns,
|
|
421
|
+
"onUpdate:selectionQuery": onSelectionQuery,
|
|
422
|
+
});
|
|
423
|
+
await settle();
|
|
424
|
+
|
|
425
|
+
sortButton("category.name").onClick();
|
|
426
|
+
sortButton("category.name").onClick(); // descending
|
|
427
|
+
await settle();
|
|
428
|
+
// Select a row, as before running a bulk action.
|
|
429
|
+
const checkbox = column("__select__").cell!({
|
|
430
|
+
row: { original: analyses[0]!, index: 0 },
|
|
431
|
+
}) as VNode;
|
|
432
|
+
(checkbox.props as { "onUpdate:modelValue": () => void })[
|
|
433
|
+
"onUpdate:modelValue"
|
|
434
|
+
]();
|
|
435
|
+
await settle();
|
|
436
|
+
|
|
437
|
+
const query = onSelectionQuery.mock.lastCall?.[0] as URLSearchParams;
|
|
438
|
+
expect(query.toString()).toBe("page_size=20&ordering=-category__name");
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
describe("MapoListTable all rows", () => {
|
|
443
|
+
it("offers 'all' and then requests the list without pagination", async () => {
|
|
444
|
+
mountTable({ ...listProps, pageSizeOptions: [10, "all"] });
|
|
445
|
+
await settle();
|
|
446
|
+
|
|
447
|
+
expect(pageSizeSelect.items).toEqual([
|
|
448
|
+
{ label: "mapo.listTable.perPage", value: 10 },
|
|
449
|
+
{ label: "mapo.listTable.allRows", value: ALL_ROWS },
|
|
450
|
+
]);
|
|
451
|
+
|
|
452
|
+
pageSizeSelect.select(ALL_ROWS);
|
|
453
|
+
await settle();
|
|
454
|
+
|
|
455
|
+
expect(lastListParams()).toEqual({});
|
|
456
|
+
expect(router.replace).toHaveBeenLastCalledWith({
|
|
457
|
+
query: { page_size: "all" },
|
|
458
|
+
});
|
|
459
|
+
expect(pagerMounted).toBe(false);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("restores 'all' from the URL", async () => {
|
|
463
|
+
route.query = { page_size: "all" };
|
|
464
|
+
mountTable({ ...listProps, pageSizeOptions: [10, "all"] });
|
|
465
|
+
await settle();
|
|
466
|
+
|
|
467
|
+
expect(lastListParams()).toEqual({});
|
|
468
|
+
expect(pagerMounted).toBe(false);
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe("MapoListTable empty state and row actions", () => {
|
|
473
|
+
beforeEach(() => {
|
|
474
|
+
crud.list.mockResolvedValue({ results: [], count: 0 });
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("tells an empty list from a search with no match", async () => {
|
|
478
|
+
mountTable(listProps);
|
|
479
|
+
await settle();
|
|
480
|
+
expect(table.empty).toBe("mapo.listTable.noItems");
|
|
481
|
+
|
|
482
|
+
app?.unmount();
|
|
483
|
+
route.query = { search: "zzz" };
|
|
484
|
+
mountTable(listProps);
|
|
485
|
+
await settle();
|
|
486
|
+
expect(table.empty).toBe("mapo.listTable.noResults");
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it("names the icon-only row buttons", async () => {
|
|
490
|
+
mountTable({
|
|
491
|
+
...listProps,
|
|
492
|
+
editFields: [{ key: "title", type: "text" }],
|
|
493
|
+
});
|
|
494
|
+
await settle();
|
|
495
|
+
|
|
496
|
+
const actions = column("__actions__").cell!({
|
|
497
|
+
row: { original: { id: 1, title: "Row 1" }, index: 0 },
|
|
498
|
+
}) as VNode;
|
|
499
|
+
const labels = (actions.children as VNode[]).map(
|
|
500
|
+
(button) => (button.props as { "aria-label": string })["aria-label"],
|
|
501
|
+
);
|
|
502
|
+
expect(labels).toEqual(["mapo.edit", "mapo.delete"]);
|
|
503
|
+
});
|
|
504
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, nextTick, ref } from "vue";
|
|
3
|
+
import type { Component } from "vue";
|
|
4
|
+
import { createPinia, setActivePinia } from "pinia";
|
|
5
|
+
|
|
6
|
+
import MapoSnackBar from "../runtime/components/MapoSnackBar.vue";
|
|
7
|
+
import { useSnackStore } from "@mapomodule/store/runtime/stores/snack";
|
|
8
|
+
|
|
9
|
+
const toast = vi.hoisted(() => ({
|
|
10
|
+
add: vi.fn(),
|
|
11
|
+
remove: vi.fn(),
|
|
12
|
+
toasts: null as { value: { id: string }[] } | null,
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
vi.mock("@nuxt/ui/composables/useToast", () => ({
|
|
16
|
+
useToast: () => toast,
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
interface HeadlessNode {
|
|
20
|
+
parent: HeadlessNode | null;
|
|
21
|
+
children: HeadlessNode[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
25
|
+
|
|
26
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
27
|
+
createElement: headlessNode,
|
|
28
|
+
createText: headlessNode,
|
|
29
|
+
createComment: headlessNode,
|
|
30
|
+
setText: () => {},
|
|
31
|
+
setElementText: () => {},
|
|
32
|
+
patchProp: () => {},
|
|
33
|
+
insert(child, parent, anchor) {
|
|
34
|
+
child.parent = parent;
|
|
35
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
36
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
37
|
+
},
|
|
38
|
+
remove(child) {
|
|
39
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
40
|
+
child.parent = null;
|
|
41
|
+
},
|
|
42
|
+
parentNode: (node) => node.parent,
|
|
43
|
+
nextSibling: (node) =>
|
|
44
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("MapoSnackBar", () => {
|
|
48
|
+
it("removes store messages evicted from Nuxt UI's toast queue", async () => {
|
|
49
|
+
setActivePinia(createPinia());
|
|
50
|
+
const activeToasts = ref<{ id: string }[]>([]);
|
|
51
|
+
toast.toasts = activeToasts;
|
|
52
|
+
toast.add.mockImplementation((item: { id: string }) => {
|
|
53
|
+
activeToasts.value = [...activeToasts.value, item].slice(-5);
|
|
54
|
+
});
|
|
55
|
+
toast.remove.mockImplementation((id: string) => {
|
|
56
|
+
activeToasts.value = activeToasts.value.filter((item) => item.id !== id);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const app = createApp(MapoSnackBar as Component);
|
|
60
|
+
app.mount(headlessNode());
|
|
61
|
+
const snack = useSnackStore();
|
|
62
|
+
|
|
63
|
+
for (let index = 0; index < 8; index++) {
|
|
64
|
+
snack.show(`Message ${index}`);
|
|
65
|
+
}
|
|
66
|
+
await nextTick();
|
|
67
|
+
|
|
68
|
+
expect(snack.messages).toHaveLength(5);
|
|
69
|
+
expect(snack.messages.map(({ id }) => String(id))).toEqual(
|
|
70
|
+
activeToasts.value.map(({ id }) => id),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
snack.dismissAll();
|
|
74
|
+
await nextTick();
|
|
75
|
+
|
|
76
|
+
expect(activeToasts.value).toEqual([]);
|
|
77
|
+
app.unmount();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Placeholder for Nuxt's `#app` in component tests. Nuxt provides `#app` only
|
|
3
|
+
* inside a Nuxt build, but the client transform must resolve every import, so
|
|
4
|
+
* `vitest.config.ts` points `#app` here. It exports nothing on purpose: each test
|
|
5
|
+
* replaces it with `vi.mock("#app", factory)`, holding the composables the
|
|
6
|
+
* component under test calls.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { holdsFormValue } from "../runtime/utils/formValues.js";
|
|
3
|
+
|
|
4
|
+
describe("holdsFormValue", () => {
|
|
5
|
+
it.each([
|
|
6
|
+
["nothing", undefined],
|
|
7
|
+
["null", null],
|
|
8
|
+
["an empty text", ""],
|
|
9
|
+
["spaces", " "],
|
|
10
|
+
["an editor's empty paragraph", "<p></p>"],
|
|
11
|
+
["blank markup", "<p> </p><br/><P> </P>"],
|
|
12
|
+
["an empty list", []],
|
|
13
|
+
["an empty object", {}],
|
|
14
|
+
["objects that hold nothing", { translations: { en: { title: "" } } }],
|
|
15
|
+
])("holds nothing in %s", (_name, value) => {
|
|
16
|
+
expect(holdsFormValue(value)).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it.each([
|
|
20
|
+
["false", false],
|
|
21
|
+
["0", 0],
|
|
22
|
+
["a text", "Hello"],
|
|
23
|
+
["a paragraph", "<p>Hi</p>"],
|
|
24
|
+
["a list", [{}]],
|
|
25
|
+
["a value deep inside", { translations: { it: { featured: false } } }],
|
|
26
|
+
["a date", new Date(0)],
|
|
27
|
+
["a file", new Blob(["x"])],
|
|
28
|
+
])("holds %s", (_name, value) => {
|
|
29
|
+
expect(holdsFormValue(value)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { languagesFromMetadata } from "../runtime/utils/langInfo";
|
|
4
|
+
|
|
5
|
+
describe("languagesFromMetadata", () => {
|
|
6
|
+
it("reads the model's own language list", () => {
|
|
7
|
+
expect(
|
|
8
|
+
languagesFromMetadata({
|
|
9
|
+
lang_info: { translatable: true, languages: ["it", "en"] },
|
|
10
|
+
}),
|
|
11
|
+
).toEqual(["it", "en"]);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("returns nothing for a model that is not translatable", () => {
|
|
15
|
+
// The site is multilingual; this model is not. Tabs here would collect
|
|
16
|
+
// values the backend silently discards.
|
|
17
|
+
expect(
|
|
18
|
+
languagesFromMetadata({
|
|
19
|
+
lang_info: {
|
|
20
|
+
translatable: false,
|
|
21
|
+
languages: [],
|
|
22
|
+
site_languages: ["it", "en"],
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("never falls back to the site's languages", () => {
|
|
29
|
+
// site_languages is context, not permission to translate this model.
|
|
30
|
+
expect(
|
|
31
|
+
languagesFromMetadata({ lang_info: { site_languages: ["it", "en"] } }),
|
|
32
|
+
).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("treats missing lang_info as not translatable", () => {
|
|
36
|
+
expect(languagesFromMetadata({ actions: {} })).toEqual([]);
|
|
37
|
+
expect(languagesFromMetadata(null)).toEqual([]);
|
|
38
|
+
expect(languagesFromMetadata(undefined)).toEqual([]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("drops entries that are not codes", () => {
|
|
42
|
+
expect(
|
|
43
|
+
languagesFromMetadata({
|
|
44
|
+
lang_info: { translatable: true, languages: ["it", 42, null] },
|
|
45
|
+
}),
|
|
46
|
+
).toEqual(["it"]);
|
|
47
|
+
});
|
|
48
|
+
});
|