@mapomodule/uikit 2.0.0-beta.13 → 2.0.0-beta.15
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 +46 -184
- 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__/MapoListTable.component.test.d.ts +2 -0
- package/dist/__tests__/MapoListTable.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__/listTable.test.d.ts +2 -0
- package/dist/__tests__/listTable.test.d.ts.map +1 -0
- package/dist/module.json +1 -1
- 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 +43 -11
- package/dist/runtime/components/MapoDetail.vue.d.ts +76 -0
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
- 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/MapoList.vue +3 -2
- package/dist/runtime/components/MapoList.vue.d.ts +9 -4
- package/dist/runtime/components/MapoList.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListTable.vue +98 -32
- 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/MapoMediaPreview.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts.map +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.d.ts +1 -1
- 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/page-meta.nuxt.d.ts +4 -0
- package/dist/runtime/stores/media.d.ts +2 -2
- package/dist/runtime/types/list.d.ts +10 -2
- package/dist/runtime/types/list.d.ts.map +1 -1
- 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 +6 -6
- package/src/__tests__/MapoConfirmDialog.component.test.ts +165 -0
- package/src/__tests__/MapoDetail.component.test.ts +293 -0
- package/src/__tests__/MapoDetailLangSwitch.component.test.ts +145 -0
- package/src/__tests__/MapoListTable.component.test.ts +504 -0
- package/src/__tests__/__mocks__/nuxt-app.ts +8 -0
- package/src/__tests__/listTable.test.ts +82 -0
- package/src/runtime/assets/main.css +9 -0
- package/src/runtime/components/MapoConfirmDialog.vue +28 -1
- package/src/runtime/components/MapoDetail.vue +78 -11
- package/src/runtime/components/MapoDetailLangSwitch.vue +26 -27
- package/src/runtime/components/MapoList.vue +13 -5
- package/src/runtime/components/MapoListTable.vue +128 -41
- package/src/runtime/components/MapoMediaUploader.vue +25 -21
- package/src/runtime/page-meta.nuxt.d.ts +4 -0
- package/src/runtime/types/list.ts +10 -2
- 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,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,82 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ALL_ROWS,
|
|
5
|
+
formatPageSize,
|
|
6
|
+
nextSorting,
|
|
7
|
+
orderingParam,
|
|
8
|
+
parsePageSize,
|
|
9
|
+
toPageSize,
|
|
10
|
+
} from "../runtime/utils/listTable";
|
|
11
|
+
|
|
12
|
+
describe("nextSorting", () => {
|
|
13
|
+
it("cycles a column through ascending, descending and unsorted", () => {
|
|
14
|
+
const asc = nextSorting([], "title");
|
|
15
|
+
expect(asc).toEqual([{ id: "title", desc: false }]);
|
|
16
|
+
const desc = nextSorting(asc, "title");
|
|
17
|
+
expect(desc).toEqual([{ id: "title", desc: true }]);
|
|
18
|
+
expect(nextSorting(desc, "title")).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("replaces the sort on another column", () => {
|
|
22
|
+
expect(
|
|
23
|
+
nextSorting(
|
|
24
|
+
[
|
|
25
|
+
{ id: "title", desc: true },
|
|
26
|
+
{ id: "status", desc: false },
|
|
27
|
+
],
|
|
28
|
+
"created_at",
|
|
29
|
+
),
|
|
30
|
+
).toEqual([{ id: "created_at", desc: false }]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("keeps only the clicked key of a multi-column sort", () => {
|
|
34
|
+
// A multi-column sort can come from the URL; a click narrows it to one key.
|
|
35
|
+
expect(
|
|
36
|
+
nextSorting(
|
|
37
|
+
[
|
|
38
|
+
{ id: "title", desc: false },
|
|
39
|
+
{ id: "status", desc: false },
|
|
40
|
+
],
|
|
41
|
+
"status",
|
|
42
|
+
),
|
|
43
|
+
).toEqual([{ id: "status", desc: true }]);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("page size helpers", () => {
|
|
48
|
+
it("stores the 'all' option as ALL_ROWS", () => {
|
|
49
|
+
expect(toPageSize("all")).toBe(ALL_ROWS);
|
|
50
|
+
expect(toPageSize(50)).toBe(50);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("reads page_size from the URL", () => {
|
|
54
|
+
expect(parsePageSize("all", 20)).toBe(ALL_ROWS);
|
|
55
|
+
expect(parsePageSize("50", 20)).toBe(50);
|
|
56
|
+
expect(parsePageSize("-3", 20)).toBe(20);
|
|
57
|
+
expect(parsePageSize("abc", 20)).toBe(20);
|
|
58
|
+
expect(parsePageSize(undefined, 20)).toBe(20);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("writes page_size to the URL", () => {
|
|
62
|
+
expect(formatPageSize(ALL_ROWS)).toBe("all");
|
|
63
|
+
expect(formatPageSize(50)).toBe("50");
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("orderingParam", () => {
|
|
68
|
+
it("joins the keys, with `-` for descending", () => {
|
|
69
|
+
expect(
|
|
70
|
+
orderingParam([
|
|
71
|
+
{ id: "title", desc: true },
|
|
72
|
+
{ id: "published_at", desc: false },
|
|
73
|
+
]),
|
|
74
|
+
).toBe("-title,published_at");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("writes a related object's field as a Django lookup", () => {
|
|
78
|
+
expect(orderingParam([{ id: "parent.category.name", desc: true }])).toBe(
|
|
79
|
+
"-parent__category__name",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -56,4 +56,13 @@
|
|
|
56
56
|
#__nuxt > * {
|
|
57
57
|
height: 100%;
|
|
58
58
|
}
|
|
59
|
+
|
|
60
|
+
/* Tailwind v4 leaves buttons with the browser's arrow and Nuxt UI adds no
|
|
61
|
+
cursor-pointer, so only links (a UButton with `to`) showed the hand. This
|
|
62
|
+
is the rule Tailwind's upgrade guide suggests. A disabled button keeps Nuxt
|
|
63
|
+
UI's `cursor-not-allowed`: a utility, so it wins over this base rule. */
|
|
64
|
+
button:not(:disabled),
|
|
65
|
+
[role="button"]:not(:disabled) {
|
|
66
|
+
cursor: pointer;
|
|
67
|
+
}
|
|
59
68
|
}
|
|
@@ -1,13 +1,40 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
+
import { ref, watch } from "vue";
|
|
2
3
|
import { useI18n } from "vue-i18n";
|
|
3
4
|
import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
|
|
4
5
|
|
|
5
6
|
const { t } = useI18n();
|
|
6
7
|
const store = useConfirmStore();
|
|
8
|
+
|
|
9
|
+
// Nuxt UI gives overlays no z-index: they stack in the order their portals sit
|
|
10
|
+
// in <body>, and a portal takes its place there when its overlay mounts.
|
|
11
|
+
// Mounted with the layout, this modal would come before every overlay a page
|
|
12
|
+
// opens later, such as the media editor drawer, and show under it while the
|
|
13
|
+
// drawer takes every click. So it mounts when a confirmation is asked, above
|
|
14
|
+
// whatever asked for it, and unmounts once its close animation has ended.
|
|
15
|
+
const modalMounted = ref(store.active);
|
|
16
|
+
|
|
17
|
+
watch(
|
|
18
|
+
() => store.active,
|
|
19
|
+
(active) => {
|
|
20
|
+
if (active) modalMounted.value = true;
|
|
21
|
+
},
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
function onAfterLeave() {
|
|
25
|
+
// Asked again while the previous dialog was closing: it is open again.
|
|
26
|
+
if (store.active) return;
|
|
27
|
+
modalMounted.value = false;
|
|
28
|
+
}
|
|
7
29
|
</script>
|
|
8
30
|
|
|
9
31
|
<template>
|
|
10
|
-
<UModal
|
|
32
|
+
<UModal
|
|
33
|
+
v-if="modalMounted"
|
|
34
|
+
v-model:open="store.active"
|
|
35
|
+
:dismissible="false"
|
|
36
|
+
@after:leave="onAfterLeave"
|
|
37
|
+
>
|
|
11
38
|
<template #content>
|
|
12
39
|
<div class="p-6 space-y-4">
|
|
13
40
|
<div class="space-y-1">
|