@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,165 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, nextTick, onUnmounted, ref } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext } from "vue";
|
|
4
|
+
import { createPinia, setActivePinia } from "pinia";
|
|
5
|
+
import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
|
|
6
|
+
|
|
7
|
+
import MapoConfirmDialog from "../runtime/components/MapoConfirmDialog.vue";
|
|
8
|
+
|
|
9
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
10
|
+
|
|
11
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
12
|
+
// enough to mount the component with real reactivity, watchers and events.
|
|
13
|
+
interface HeadlessNode {
|
|
14
|
+
parent: HeadlessNode | null;
|
|
15
|
+
children: HeadlessNode[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
19
|
+
|
|
20
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
21
|
+
createElement: headlessNode,
|
|
22
|
+
createText: headlessNode,
|
|
23
|
+
createComment: headlessNode,
|
|
24
|
+
setText: () => {},
|
|
25
|
+
setElementText: () => {},
|
|
26
|
+
patchProp: () => {},
|
|
27
|
+
insert(child, parent, anchor) {
|
|
28
|
+
child.parent = parent;
|
|
29
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
30
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
31
|
+
},
|
|
32
|
+
remove(child) {
|
|
33
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
34
|
+
child.parent = null;
|
|
35
|
+
},
|
|
36
|
+
parentNode: (node) => node.parent,
|
|
37
|
+
nextSibling: (node) =>
|
|
38
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const passthrough = (name: string): Component => ({
|
|
42
|
+
name,
|
|
43
|
+
inheritAttrs: false,
|
|
44
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
45
|
+
return () => h("div", null, slots.default?.());
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Nuxt UI overlays have no z-index: they stack in the order their portals enter
|
|
50
|
+
// <body>, which is the order the overlays mount. The stubs record that order.
|
|
51
|
+
const mountOrder: string[] = [];
|
|
52
|
+
|
|
53
|
+
// `UModal` declares what the dialog binds: `open` in, and `after:leave` out once
|
|
54
|
+
// the close animation has ended.
|
|
55
|
+
let modal: { readonly open: boolean | undefined; finishLeave(): void } | null =
|
|
56
|
+
null;
|
|
57
|
+
|
|
58
|
+
const UModal: Component = {
|
|
59
|
+
name: "UModal",
|
|
60
|
+
props: { open: Boolean, dismissible: Boolean },
|
|
61
|
+
emits: ["after:leave"],
|
|
62
|
+
setup(props: { open?: boolean }, { emit }: SetupContext<["after:leave"]>) {
|
|
63
|
+
mountOrder.push("confirm");
|
|
64
|
+
modal = {
|
|
65
|
+
get open() {
|
|
66
|
+
return props.open;
|
|
67
|
+
},
|
|
68
|
+
finishLeave: () => emit("after:leave"),
|
|
69
|
+
};
|
|
70
|
+
onUnmounted(() => {
|
|
71
|
+
modal = null;
|
|
72
|
+
});
|
|
73
|
+
return () => h("div");
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// An overlay that a page opens once the layout is already there, like the media
|
|
78
|
+
// editor drawer.
|
|
79
|
+
const Drawer: Component = {
|
|
80
|
+
name: "Drawer",
|
|
81
|
+
setup() {
|
|
82
|
+
mountOrder.push("drawer");
|
|
83
|
+
return () => h("div");
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const drawerOpen = ref(false);
|
|
88
|
+
|
|
89
|
+
let app: App<HeadlessNode> | undefined;
|
|
90
|
+
|
|
91
|
+
/** Mounts the dialog the way the layout does; returns the store that drives it. */
|
|
92
|
+
function mountLayout() {
|
|
93
|
+
const pinia = createPinia();
|
|
94
|
+
setActivePinia(pinia);
|
|
95
|
+
const root: Component = {
|
|
96
|
+
// The page comes first, then the root components, as in `mapo-default`.
|
|
97
|
+
render: () => [
|
|
98
|
+
drawerOpen.value ? h(Drawer) : null,
|
|
99
|
+
h(MapoConfirmDialog as Component),
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
app = createApp(root);
|
|
103
|
+
app.use(pinia);
|
|
104
|
+
app.component("UModal", UModal);
|
|
105
|
+
app.component("UButton", passthrough("UButton"));
|
|
106
|
+
app.mount(headlessNode());
|
|
107
|
+
return useConfirmStore();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
mountOrder.length = 0;
|
|
112
|
+
drawerOpen.value = false;
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
afterEach(() => {
|
|
116
|
+
app?.unmount();
|
|
117
|
+
app = undefined;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("MapoConfirmDialog", () => {
|
|
121
|
+
it("opens above an overlay the page opened after the layout", async () => {
|
|
122
|
+
const confirm = mountLayout();
|
|
123
|
+
drawerOpen.value = true;
|
|
124
|
+
await nextTick();
|
|
125
|
+
|
|
126
|
+
void confirm.ask({ message: "Delete?" });
|
|
127
|
+
await nextTick();
|
|
128
|
+
|
|
129
|
+
expect(modal?.open).toBe(true);
|
|
130
|
+
expect(mountOrder).toEqual(["drawer", "confirm"]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("stays mounted until its close animation has ended", async () => {
|
|
134
|
+
const confirm = mountLayout();
|
|
135
|
+
const answer = confirm.ask({ message: "Delete?" });
|
|
136
|
+
await nextTick();
|
|
137
|
+
|
|
138
|
+
confirm.confirm();
|
|
139
|
+
await nextTick();
|
|
140
|
+
|
|
141
|
+
expect(modal?.open).toBe(false);
|
|
142
|
+
modal?.finishLeave();
|
|
143
|
+
await nextTick();
|
|
144
|
+
|
|
145
|
+
expect(modal).toBeNull();
|
|
146
|
+
await expect(answer).resolves.toBe(true);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("stays open when asked again while it was closing", async () => {
|
|
150
|
+
const confirm = mountLayout();
|
|
151
|
+
void confirm.ask({ message: "First?" });
|
|
152
|
+
await nextTick();
|
|
153
|
+
confirm.cancel();
|
|
154
|
+
await nextTick();
|
|
155
|
+
const closing = modal;
|
|
156
|
+
|
|
157
|
+
void confirm.ask({ message: "Second?" });
|
|
158
|
+
await nextTick();
|
|
159
|
+
// The first close animation reports its end after the second question.
|
|
160
|
+
closing?.finishLeave();
|
|
161
|
+
await nextTick();
|
|
162
|
+
|
|
163
|
+
expect(modal?.open).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, onUnmounted } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext, VNode } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoDetail from "../runtime/components/MapoDetail.vue";
|
|
6
|
+
|
|
7
|
+
type AfterEachHook = (to: unknown, from: unknown, failure?: unknown) => void;
|
|
8
|
+
|
|
9
|
+
// The detail reads the router, the stores and the CRUD layer at setup: replace
|
|
10
|
+
// them with in-memory doubles. `vi.hoisted` lets the mock factories share them.
|
|
11
|
+
const router = vi.hoisted(() => ({
|
|
12
|
+
back: vi.fn(),
|
|
13
|
+
replace: vi.fn(),
|
|
14
|
+
afterEach: vi.fn<(hook: AfterEachHook) => () => void>(),
|
|
15
|
+
// Vue Router keeps the previous location of the current entry in `state.back`.
|
|
16
|
+
options: { history: { state: { back: "/news" as string | null } } },
|
|
17
|
+
}));
|
|
18
|
+
const crud = vi.hoisted(() => ({
|
|
19
|
+
options: vi.fn(),
|
|
20
|
+
detail: vi.fn(),
|
|
21
|
+
create: vi.fn(),
|
|
22
|
+
partialUpdate: vi.fn(),
|
|
23
|
+
update: vi.fn(),
|
|
24
|
+
delete: vi.fn(),
|
|
25
|
+
}));
|
|
26
|
+
const confirm = vi.hoisted(() => ({ ask: vi.fn() }));
|
|
27
|
+
|
|
28
|
+
vi.mock("#app", () => ({
|
|
29
|
+
useRouter: () => router,
|
|
30
|
+
onBeforeRouteLeave: () => {},
|
|
31
|
+
useNuxtApp: () => ({ $mapoFormRegistry: {} }),
|
|
32
|
+
}));
|
|
33
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
34
|
+
vi.mock("@mapomodule/core/runtime/api/crud", () => ({ useCrud: () => crud }));
|
|
35
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
36
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
37
|
+
}));
|
|
38
|
+
vi.mock("@mapomodule/store/runtime/stores/confirm", () => ({
|
|
39
|
+
useConfirmStore: () => confirm,
|
|
40
|
+
}));
|
|
41
|
+
vi.mock("@mapomodule/store/runtime/composables/usePermissions", () => ({
|
|
42
|
+
usePermissions: () => ({
|
|
43
|
+
canAdd: () => true,
|
|
44
|
+
canChange: () => true,
|
|
45
|
+
canDelete: () => true,
|
|
46
|
+
}),
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
50
|
+
// enough to mount the component with real reactivity, watchers and events.
|
|
51
|
+
interface HeadlessNode {
|
|
52
|
+
parent: HeadlessNode | null;
|
|
53
|
+
children: HeadlessNode[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
57
|
+
|
|
58
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
59
|
+
createElement: headlessNode,
|
|
60
|
+
createText: headlessNode,
|
|
61
|
+
createComment: headlessNode,
|
|
62
|
+
setText: () => {},
|
|
63
|
+
setElementText: () => {},
|
|
64
|
+
patchProp: () => {},
|
|
65
|
+
insert(child, parent, anchor) {
|
|
66
|
+
child.parent = parent;
|
|
67
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
68
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
69
|
+
},
|
|
70
|
+
remove(child) {
|
|
71
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
72
|
+
child.parent = null;
|
|
73
|
+
},
|
|
74
|
+
parentNode: (node) => node.parent,
|
|
75
|
+
nextSibling: (node) =>
|
|
76
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const passthrough = (name: string): Component => ({
|
|
80
|
+
name,
|
|
81
|
+
inheritAttrs: false,
|
|
82
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
83
|
+
return () => h("div", null, slots.default?.());
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const slotText = (nodes: VNode[] | undefined) =>
|
|
88
|
+
(nodes ?? [])
|
|
89
|
+
.map((node) => (typeof node.children === "string" ? node.children : ""))
|
|
90
|
+
.join("")
|
|
91
|
+
.trim();
|
|
92
|
+
|
|
93
|
+
// `UButton` declares what the detail binds: `loading` and `disabled` in, `click`
|
|
94
|
+
// out. Like the real one, a loading or disabled button emits no click.
|
|
95
|
+
interface ButtonStub {
|
|
96
|
+
readonly label: string;
|
|
97
|
+
readonly loading: boolean;
|
|
98
|
+
readonly disabled: boolean;
|
|
99
|
+
click(): void;
|
|
100
|
+
}
|
|
101
|
+
const buttons: ButtonStub[] = [];
|
|
102
|
+
|
|
103
|
+
const UButton: Component = {
|
|
104
|
+
name: "UButton",
|
|
105
|
+
props: { loading: Boolean, disabled: Boolean },
|
|
106
|
+
emits: ["click"],
|
|
107
|
+
setup(
|
|
108
|
+
props: { loading?: boolean; disabled?: boolean },
|
|
109
|
+
{ emit, slots }: SetupContext<["click"]>,
|
|
110
|
+
) {
|
|
111
|
+
const stub: ButtonStub = {
|
|
112
|
+
get label() {
|
|
113
|
+
return slotText(slots.default?.());
|
|
114
|
+
},
|
|
115
|
+
get loading() {
|
|
116
|
+
return props.loading ?? false;
|
|
117
|
+
},
|
|
118
|
+
get disabled() {
|
|
119
|
+
return props.disabled ?? false;
|
|
120
|
+
},
|
|
121
|
+
click() {
|
|
122
|
+
if (stub.loading) return;
|
|
123
|
+
if (stub.disabled) return;
|
|
124
|
+
emit("click");
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
buttons.push(stub);
|
|
128
|
+
onUnmounted(() => {
|
|
129
|
+
buttons.splice(buttons.indexOf(stub), 1);
|
|
130
|
+
});
|
|
131
|
+
return () => h("button", null, slots.default?.());
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const stubs: Record<string, Component> = {
|
|
136
|
+
UButton,
|
|
137
|
+
UCard: passthrough("UCard"),
|
|
138
|
+
UAlert: passthrough("UAlert"),
|
|
139
|
+
UIcon: passthrough("UIcon"),
|
|
140
|
+
UModal: passthrough("UModal"),
|
|
141
|
+
MapoForm: passthrough("MapoForm"),
|
|
142
|
+
MapoDetailLangSwitch: passthrough("MapoDetailLangSwitch"),
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const button = (label: string) => {
|
|
146
|
+
const found = buttons.find((stub) => stub.label === label);
|
|
147
|
+
if (!found) throw new Error(`no button ${label}`);
|
|
148
|
+
return found;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** What a custom action panel reads from the `side-buttons` slot. */
|
|
152
|
+
interface ActionBindings {
|
|
153
|
+
isSaving: boolean;
|
|
154
|
+
save: (andBack?: boolean) => Promise<void>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let app: App<HeadlessNode> | undefined;
|
|
158
|
+
|
|
159
|
+
function mountDetail(slots: Record<string, (bindings: never) => VNode[]> = {}) {
|
|
160
|
+
const root: Component = {
|
|
161
|
+
render: () =>
|
|
162
|
+
h(MapoDetail as Component, { endpoint: "/api/news/", id: 5 }, slots),
|
|
163
|
+
};
|
|
164
|
+
app = createApp(root);
|
|
165
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
166
|
+
app.mount(headlessNode());
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Runs the pending CRUD promises and the renders they trigger.
|
|
170
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve));
|
|
171
|
+
|
|
172
|
+
const afterEachHooks = new Set<AfterEachHook>();
|
|
173
|
+
|
|
174
|
+
/** Ends the navigation router.back() started; `failure` as Vue Router reports it. */
|
|
175
|
+
const finishNavigation = (failure?: Error) => {
|
|
176
|
+
for (const hook of [...afterEachHooks]) hook({}, {}, failure);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const record = { id: 5, title: "News" };
|
|
180
|
+
|
|
181
|
+
beforeEach(() => {
|
|
182
|
+
router.back.mockReset();
|
|
183
|
+
router.replace.mockReset();
|
|
184
|
+
router.afterEach.mockReset();
|
|
185
|
+
router.afterEach.mockImplementation((hook) => {
|
|
186
|
+
afterEachHooks.add(hook);
|
|
187
|
+
return () => afterEachHooks.delete(hook);
|
|
188
|
+
});
|
|
189
|
+
router.options.history.state.back = "/news";
|
|
190
|
+
afterEachHooks.clear();
|
|
191
|
+
for (const fn of Object.values(crud)) fn.mockReset();
|
|
192
|
+
crud.options.mockResolvedValue({});
|
|
193
|
+
crud.detail.mockResolvedValue(record);
|
|
194
|
+
crud.partialUpdate.mockResolvedValue(record);
|
|
195
|
+
crud.delete.mockResolvedValue(undefined);
|
|
196
|
+
confirm.ask.mockReset();
|
|
197
|
+
confirm.ask.mockResolvedValue(true);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
afterEach(() => {
|
|
201
|
+
app?.unmount();
|
|
202
|
+
app = undefined;
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("MapoDetail actions", () => {
|
|
206
|
+
it("keeps the buttons busy until the page has gone back after Save", async () => {
|
|
207
|
+
mountDetail();
|
|
208
|
+
await settle();
|
|
209
|
+
|
|
210
|
+
button("mapo.save").click();
|
|
211
|
+
await settle();
|
|
212
|
+
|
|
213
|
+
expect(crud.partialUpdate).toHaveBeenCalledTimes(1);
|
|
214
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
215
|
+
// The page is still on screen while the previous route loads.
|
|
216
|
+
expect(button("mapo.save").loading).toBe(true);
|
|
217
|
+
expect(button("mapo.saveContinue").disabled).toBe(true);
|
|
218
|
+
expect(button("mapo.back").disabled).toBe(true);
|
|
219
|
+
expect(button("mapo.delete").disabled).toBe(true);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("frees the buttons when the navigation back is cancelled", async () => {
|
|
223
|
+
mountDetail();
|
|
224
|
+
await settle();
|
|
225
|
+
button("mapo.save").click();
|
|
226
|
+
await settle();
|
|
227
|
+
|
|
228
|
+
// A guard kept the page, such as the unsaved-changes one.
|
|
229
|
+
finishNavigation(new Error("aborted"));
|
|
230
|
+
await settle();
|
|
231
|
+
|
|
232
|
+
expect(button("mapo.save").loading).toBe(false);
|
|
233
|
+
expect(button("mapo.back").disabled).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("saves once and stays busy when a custom button saves twice", async () => {
|
|
237
|
+
let bindings: ActionBindings | undefined;
|
|
238
|
+
mountDetail({
|
|
239
|
+
"side-buttons": (slotProps: ActionBindings) => {
|
|
240
|
+
bindings = slotProps;
|
|
241
|
+
return [];
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
await settle();
|
|
245
|
+
|
|
246
|
+
void bindings?.save(true);
|
|
247
|
+
void bindings?.save(true);
|
|
248
|
+
await settle();
|
|
249
|
+
|
|
250
|
+
expect(crud.partialUpdate).toHaveBeenCalledTimes(1);
|
|
251
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
252
|
+
expect(bindings?.isSaving).toBe(true);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("goes back once when Back is clicked twice", async () => {
|
|
256
|
+
mountDetail();
|
|
257
|
+
await settle();
|
|
258
|
+
|
|
259
|
+
button("mapo.back").click();
|
|
260
|
+
button("mapo.back").click();
|
|
261
|
+
await settle();
|
|
262
|
+
|
|
263
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
264
|
+
expect(button("mapo.back").loading).toBe(true);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("does not lock the buttons when there is no previous page", async () => {
|
|
268
|
+
router.options.history.state.back = null;
|
|
269
|
+
mountDetail();
|
|
270
|
+
await settle();
|
|
271
|
+
|
|
272
|
+
button("mapo.save").click();
|
|
273
|
+
await settle();
|
|
274
|
+
|
|
275
|
+
expect(router.back).toHaveBeenCalledTimes(1);
|
|
276
|
+
expect(button("mapo.save").loading).toBe(false);
|
|
277
|
+
expect(button("mapo.back").disabled).toBe(false);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("keeps Delete busy until the page has gone back", async () => {
|
|
281
|
+
mountDetail();
|
|
282
|
+
await settle();
|
|
283
|
+
|
|
284
|
+
button("mapo.delete").click();
|
|
285
|
+
await settle();
|
|
286
|
+
button("mapo.delete").click();
|
|
287
|
+
await settle();
|
|
288
|
+
|
|
289
|
+
expect(crud.delete).toHaveBeenCalledTimes(1);
|
|
290
|
+
expect(confirm.ask).toHaveBeenCalledTimes(1);
|
|
291
|
+
expect(button("mapo.delete").loading).toBe(true);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, nextTick, onUnmounted } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoDetailLangSwitch from "../runtime/components/MapoDetailLangSwitch.vue";
|
|
6
|
+
|
|
7
|
+
const route = vi.hoisted(() => ({ query: {} as Record<string, string> }));
|
|
8
|
+
const router = vi.hoisted(() => ({ replace: vi.fn() }));
|
|
9
|
+
|
|
10
|
+
vi.mock("#app", () => ({ useRoute: () => route, useRouter: () => router }));
|
|
11
|
+
|
|
12
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
13
|
+
// enough to mount the component with real reactivity, watchers and events. The
|
|
14
|
+
// tabs are template refs the switch scrolls into view, hence the no-op method.
|
|
15
|
+
interface HeadlessNode {
|
|
16
|
+
parent: HeadlessNode | null;
|
|
17
|
+
children: HeadlessNode[];
|
|
18
|
+
scrollIntoView(): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const headlessNode = (): HeadlessNode => ({
|
|
22
|
+
parent: null,
|
|
23
|
+
children: [],
|
|
24
|
+
scrollIntoView: () => {},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
28
|
+
createElement: headlessNode,
|
|
29
|
+
createText: headlessNode,
|
|
30
|
+
createComment: headlessNode,
|
|
31
|
+
setText: () => {},
|
|
32
|
+
setElementText: () => {},
|
|
33
|
+
patchProp: () => {},
|
|
34
|
+
insert(child, parent, anchor) {
|
|
35
|
+
child.parent = parent;
|
|
36
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
37
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
38
|
+
},
|
|
39
|
+
remove(child) {
|
|
40
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
41
|
+
child.parent = null;
|
|
42
|
+
},
|
|
43
|
+
parentNode: (node) => node.parent,
|
|
44
|
+
nextSibling: (node) =>
|
|
45
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// `USelectMenu` declares the contract the switch binds: `modelValue` and `items`
|
|
49
|
+
// in, `update:modelValue` out with the picked item's value (`value-key="value"`),
|
|
50
|
+
// and a default slot that receives `modelValue` and `open`.
|
|
51
|
+
interface SelectItem {
|
|
52
|
+
label: string;
|
|
53
|
+
value: string;
|
|
54
|
+
}
|
|
55
|
+
let select: { readonly items: SelectItem[]; pick(value: string): void } | null =
|
|
56
|
+
null;
|
|
57
|
+
|
|
58
|
+
const USelectMenu: Component = {
|
|
59
|
+
name: "USelectMenu",
|
|
60
|
+
props: { modelValue: String, items: Array },
|
|
61
|
+
emits: ["update:modelValue"],
|
|
62
|
+
setup(
|
|
63
|
+
props: { modelValue?: string; items?: SelectItem[] },
|
|
64
|
+
{ emit, slots }: SetupContext<["update:modelValue"]>,
|
|
65
|
+
) {
|
|
66
|
+
select = {
|
|
67
|
+
get items() {
|
|
68
|
+
return props.items ?? [];
|
|
69
|
+
},
|
|
70
|
+
pick: (value) => emit("update:modelValue", value),
|
|
71
|
+
};
|
|
72
|
+
onUnmounted(() => {
|
|
73
|
+
select = null;
|
|
74
|
+
});
|
|
75
|
+
return () =>
|
|
76
|
+
h(
|
|
77
|
+
"button",
|
|
78
|
+
null,
|
|
79
|
+
slots.default?.({ modelValue: props.modelValue, open: false }),
|
|
80
|
+
);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const NINE_LANGS = ["it", "en", "fr", "de", "es", "pt", "nl", "pl", "ru"];
|
|
85
|
+
const changes = vi.fn();
|
|
86
|
+
|
|
87
|
+
let app: App<HeadlessNode> | undefined;
|
|
88
|
+
|
|
89
|
+
function mountSwitch(props: Record<string, unknown>) {
|
|
90
|
+
const root: Component = {
|
|
91
|
+
render: () =>
|
|
92
|
+
h(MapoDetailLangSwitch as Component, {
|
|
93
|
+
modelValue: "it",
|
|
94
|
+
"onUpdate:modelValue": changes,
|
|
95
|
+
...props,
|
|
96
|
+
}),
|
|
97
|
+
};
|
|
98
|
+
app = createApp(root);
|
|
99
|
+
app.component("USelectMenu", USelectMenu);
|
|
100
|
+
app.mount(headlessNode());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
beforeEach(() => {
|
|
104
|
+
route.query = {};
|
|
105
|
+
router.replace.mockReset();
|
|
106
|
+
changes.mockReset();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
afterEach(() => {
|
|
110
|
+
app?.unmount();
|
|
111
|
+
app = undefined;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("MapoDetailLangSwitch", () => {
|
|
115
|
+
it("shows only the tabs up to the threshold", async () => {
|
|
116
|
+
mountSwitch({ langs: NINE_LANGS.slice(0, 8) });
|
|
117
|
+
await nextTick();
|
|
118
|
+
|
|
119
|
+
expect(select).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("adds the select above the threshold", async () => {
|
|
123
|
+
mountSwitch({ langs: NINE_LANGS });
|
|
124
|
+
await nextTick();
|
|
125
|
+
|
|
126
|
+
expect(select?.items.map((item) => item.value)).toEqual(NINE_LANGS);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("follows a custom threshold", async () => {
|
|
130
|
+
mountSwitch({ langs: ["it", "en"], langThreshold: 0 });
|
|
131
|
+
await nextTick();
|
|
132
|
+
|
|
133
|
+
expect(select).not.toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("switches language from the select", async () => {
|
|
137
|
+
mountSwitch({ langs: NINE_LANGS });
|
|
138
|
+
await nextTick();
|
|
139
|
+
|
|
140
|
+
select?.pick("de");
|
|
141
|
+
|
|
142
|
+
expect(changes).toHaveBeenCalledWith("de");
|
|
143
|
+
expect(router.replace).toHaveBeenCalledWith({ query: { lang: "de" } });
|
|
144
|
+
});
|
|
145
|
+
});
|