@mapomodule/uikit 2.0.0-beta.16 → 2.0.0-beta.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/MapoMediaEditor.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaEditor.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaFolderPicker.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaFolderPicker.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaFolders.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaFolders.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaGallery.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaGallery.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaUploadHandover.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaUploadHandover.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMenuManager.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMenuManager.component.test.d.ts.map +1 -0
- package/dist/__tests__/mediaDrag.test.d.ts +2 -0
- package/dist/__tests__/mediaDrag.test.d.ts.map +1 -0
- package/dist/__tests__/mediaFolders.test.d.ts +2 -0
- package/dist/__tests__/mediaFolders.test.d.ts.map +1 -0
- package/dist/__tests__/mediaMetadata.test.d.ts +2 -0
- package/dist/__tests__/mediaMetadata.test.d.ts.map +1 -0
- package/dist/__tests__/mediaStore.test.d.ts +2 -0
- package/dist/__tests__/mediaStore.test.d.ts.map +1 -0
- package/dist/__tests__/mediaUrl.test.d.ts +2 -0
- package/dist/__tests__/mediaUrl.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 +17 -0
- package/dist/module.d.ts +16 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +4 -0
- package/dist/runtime/adapters/defaultMediaAdapter.d.ts +1 -0
- package/dist/runtime/adapters/defaultMediaAdapter.d.ts.map +1 -1
- package/dist/runtime/adapters/defaultMediaAdapter.js +6 -1
- package/dist/runtime/components/MapoDetail.vue +9 -5
- package/dist/runtime/components/MapoDetail.vue.d.ts +3 -3
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDropArea.vue +17 -6
- package/dist/runtime/components/MapoDropArea.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListQuickEdit.vue +7 -1
- package/dist/runtime/components/MapoListQuickEdit.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaEditor.vue +94 -38
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts +4 -0
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaFolderPicker.vue +129 -0
- package/dist/runtime/components/MapoMediaFolderPicker.vue.d.ts +33 -0
- package/dist/runtime/components/MapoMediaFolderPicker.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaFolders.vue +254 -72
- package/dist/runtime/components/MapoMediaFolders.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaGallery.vue +228 -143
- package/dist/runtime/components/MapoMediaGallery.vue.d.ts +5 -1
- package/dist/runtime/components/MapoMediaGallery.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaManager.vue +6 -2
- package/dist/runtime/components/MapoMediaManager.vue.d.ts +11 -2
- package/dist/runtime/components/MapoMediaManager.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaPreview.vue +5 -3
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaUploader.vue +5 -1
- package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuManager.vue +13 -5
- package/dist/runtime/components/MapoMenuManager.vue.d.ts.map +1 -1
- package/dist/runtime/stores/media.d.ts +43 -6
- package/dist/runtime/stores/media.d.ts.map +1 -1
- package/dist/runtime/stores/media.js +201 -16
- package/dist/runtime/types/media.d.ts +17 -2
- package/dist/runtime/types/media.d.ts.map +1 -1
- package/dist/runtime/utils/contentLanguages.d.ts +9 -0
- package/dist/runtime/utils/contentLanguages.d.ts.map +1 -0
- package/dist/runtime/utils/contentLanguages.js +8 -0
- package/dist/runtime/utils/langInfo.d.ts +13 -0
- package/dist/runtime/utils/langInfo.d.ts.map +1 -1
- package/dist/runtime/utils/langInfo.js +9 -0
- package/dist/runtime/utils/mediaDrag.d.ts +21 -0
- package/dist/runtime/utils/mediaDrag.d.ts.map +1 -0
- package/dist/runtime/utils/mediaDrag.js +11 -0
- package/dist/runtime/utils/mediaFolders.d.ts +65 -0
- package/dist/runtime/utils/mediaFolders.d.ts.map +1 -0
- package/dist/runtime/utils/mediaFolders.js +97 -0
- package/dist/runtime/utils/mediaMetadata.d.ts +39 -0
- package/dist/runtime/utils/mediaMetadata.d.ts.map +1 -0
- package/dist/runtime/utils/mediaMetadata.js +97 -0
- package/dist/runtime/utils/mediaUrl.d.ts +16 -0
- package/dist/runtime/utils/mediaUrl.d.ts.map +1 -0
- package/dist/runtime/utils/mediaUrl.js +12 -0
- package/package.json +3 -3
- package/src/__tests__/MapoDetail.component.test.ts +64 -1
- package/src/__tests__/MapoMediaEditor.component.test.ts +447 -0
- package/src/__tests__/MapoMediaFolderPicker.component.test.ts +220 -0
- package/src/__tests__/MapoMediaFolders.component.test.ts +519 -0
- package/src/__tests__/MapoMediaGallery.component.test.ts +211 -0
- package/src/__tests__/MapoMediaUploadHandover.component.test.ts +241 -0
- package/src/__tests__/MapoMenuManager.component.test.ts +175 -0
- package/src/__tests__/langInfo.test.ts +38 -1
- package/src/__tests__/mediaDrag.test.ts +42 -0
- package/src/__tests__/mediaFolders.test.ts +204 -0
- package/src/__tests__/mediaMetadata.test.ts +169 -0
- package/src/__tests__/mediaStore.test.ts +569 -0
- package/src/__tests__/mediaUrl.test.ts +44 -0
- package/src/index.ts +1 -0
- package/src/module.ts +25 -0
- package/src/runtime/adapters/defaultMediaAdapter.ts +11 -1
- package/src/runtime/components/MapoDetail.vue +21 -9
- package/src/runtime/components/MapoDropArea.vue +24 -6
- package/src/runtime/components/MapoListQuickEdit.vue +10 -1
- package/src/runtime/components/MapoMediaEditor.vue +131 -47
- package/src/runtime/components/MapoMediaFolderPicker.vue +176 -0
- package/src/runtime/components/MapoMediaFolders.vue +311 -72
- package/src/runtime/components/MapoMediaGallery.vue +263 -152
- package/src/runtime/components/MapoMediaManager.vue +16 -2
- package/src/runtime/components/MapoMediaPreview.vue +8 -3
- package/src/runtime/components/MapoMediaUploader.vue +8 -1
- package/src/runtime/components/MapoMenuManager.vue +24 -7
- package/src/runtime/stores/media.ts +335 -21
- package/src/runtime/types/media.ts +20 -2
- package/src/runtime/utils/contentLanguages.ts +18 -0
- package/src/runtime/utils/langInfo.ts +25 -0
- package/src/runtime/utils/mediaDrag.ts +29 -0
- package/src/runtime/utils/mediaFolders.ts +182 -0
- package/src/runtime/utils/mediaMetadata.ts +165 -0
- package/src/runtime/utils/mediaUrl.ts +30 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext } from "vue";
|
|
4
|
+
import { createPinia, setActivePinia } from "pinia";
|
|
5
|
+
import type { Pinia } from "pinia";
|
|
6
|
+
|
|
7
|
+
import MapoMediaGallery from "../runtime/components/MapoMediaGallery.vue";
|
|
8
|
+
import { useMediaStore } from "../runtime/stores/media";
|
|
9
|
+
import { MEDIA_DRAG_TYPE } from "../runtime/utils/mediaDrag";
|
|
10
|
+
import type { MediaItem } from "../runtime/types/media";
|
|
11
|
+
|
|
12
|
+
// Gallery cards dragged onto the folder tree: what a drag carries and when a
|
|
13
|
+
// card can be dragged at all.
|
|
14
|
+
const permissions = vi.hoisted(() => ({ canChange: true }));
|
|
15
|
+
|
|
16
|
+
vi.mock("#app", () => ({
|
|
17
|
+
useRuntimeConfig: () => ({ public: {} }),
|
|
18
|
+
useNuxtApp: () => ({}),
|
|
19
|
+
}));
|
|
20
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
21
|
+
vi.mock("@mapomodule/core/runtime/api/crud", () => ({ useCrud: () => ({}) }));
|
|
22
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
23
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
24
|
+
}));
|
|
25
|
+
vi.mock("@mapomodule/store/runtime/stores/confirm", () => ({
|
|
26
|
+
useConfirmStore: () => ({ ask: vi.fn() }),
|
|
27
|
+
}));
|
|
28
|
+
vi.mock("@mapomodule/i18n/runtime/composables/useMapoT", () => ({
|
|
29
|
+
useMapoT: () => (key: string) => key,
|
|
30
|
+
}));
|
|
31
|
+
vi.mock("@mapomodule/store/runtime/composables/usePermissions", () => ({
|
|
32
|
+
usePermissions: () => ({ canChange: () => permissions.canChange }),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects
|
|
36
|
+
// keeps each element's props, so the tests can call its listeners.
|
|
37
|
+
interface HeadlessNode {
|
|
38
|
+
parent: HeadlessNode | null;
|
|
39
|
+
children: HeadlessNode[];
|
|
40
|
+
props: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const headlessNode = (): HeadlessNode => ({
|
|
44
|
+
parent: null,
|
|
45
|
+
children: [],
|
|
46
|
+
props: {},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
50
|
+
createElement: headlessNode,
|
|
51
|
+
createText: headlessNode,
|
|
52
|
+
createComment: headlessNode,
|
|
53
|
+
setText: () => {},
|
|
54
|
+
setElementText: () => {},
|
|
55
|
+
patchProp(node, key, _previous, next) {
|
|
56
|
+
node.props[key] = next;
|
|
57
|
+
},
|
|
58
|
+
insert(child, parent, anchor) {
|
|
59
|
+
child.parent = parent;
|
|
60
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
61
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
62
|
+
},
|
|
63
|
+
remove(child) {
|
|
64
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
65
|
+
child.parent = null;
|
|
66
|
+
},
|
|
67
|
+
parentNode: (node) => node.parent,
|
|
68
|
+
nextSibling: (node) =>
|
|
69
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const passthrough = (name: string): Component => ({
|
|
73
|
+
name,
|
|
74
|
+
inheritAttrs: false,
|
|
75
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
76
|
+
return () => h("div", null, slots.default?.({ triggerPick: () => {} }));
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const stubs: Record<string, Component> = Object.fromEntries(
|
|
81
|
+
[
|
|
82
|
+
"MapoDropArea",
|
|
83
|
+
"UButton",
|
|
84
|
+
"UCheckbox",
|
|
85
|
+
"USkeleton",
|
|
86
|
+
"UPagination",
|
|
87
|
+
"UIcon",
|
|
88
|
+
"MapoMediaPreview",
|
|
89
|
+
"MapoMediaFolderPicker",
|
|
90
|
+
].map((name) => [name, passthrough(name)]),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const media = (id: number): MediaItem => ({
|
|
94
|
+
id,
|
|
95
|
+
file: `/media/${id}.jpg`,
|
|
96
|
+
mime_type: "image/jpeg",
|
|
97
|
+
title: `Photo ${id}`,
|
|
98
|
+
alt_text: "",
|
|
99
|
+
description: "",
|
|
100
|
+
size: 1000,
|
|
101
|
+
created: "2026-11-01T00:00:00Z",
|
|
102
|
+
modified: "2026-11-01T00:00:00Z",
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// A drag event with the data transfer the gallery writes to.
|
|
106
|
+
function dragEvent() {
|
|
107
|
+
const data: Record<string, string> = {};
|
|
108
|
+
const dataTransfer = {
|
|
109
|
+
types: [] as string[],
|
|
110
|
+
effectAllowed: "all",
|
|
111
|
+
setData(type: string, value: string) {
|
|
112
|
+
data[type] = value;
|
|
113
|
+
dataTransfer.types.push(type);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
return { event: { dataTransfer }, data };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const all = (node: HeadlessNode): HeadlessNode[] => [
|
|
120
|
+
node,
|
|
121
|
+
...node.children.flatMap(all),
|
|
122
|
+
];
|
|
123
|
+
// The cards, in the order of `store.medias`: the elements a drag starts from.
|
|
124
|
+
const cards = () => all(root).filter((node) => "onDragstart" in node.props);
|
|
125
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve));
|
|
126
|
+
|
|
127
|
+
let app: App<HeadlessNode> | undefined;
|
|
128
|
+
let pinia: Pinia;
|
|
129
|
+
let root: HeadlessNode;
|
|
130
|
+
|
|
131
|
+
async function mountGallery() {
|
|
132
|
+
root = headlessNode();
|
|
133
|
+
app = createApp({ render: () => h(MapoMediaGallery as Component) });
|
|
134
|
+
app.use(pinia);
|
|
135
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
136
|
+
app.mount(root);
|
|
137
|
+
await settle();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function dragCard(index: number) {
|
|
141
|
+
const drag = dragEvent();
|
|
142
|
+
const onDragstart = cards()[index]?.props.onDragstart as (e: unknown) => void;
|
|
143
|
+
onDragstart(drag.event);
|
|
144
|
+
return drag;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
beforeEach(() => {
|
|
148
|
+
pinia = createPinia();
|
|
149
|
+
setActivePinia(pinia);
|
|
150
|
+
permissions.canChange = true;
|
|
151
|
+
useMediaStore().medias = [media(7), media(8), media(9)];
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
afterEach(() => {
|
|
155
|
+
app?.unmount();
|
|
156
|
+
app = undefined;
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("MapoMediaGallery dragging cards", () => {
|
|
160
|
+
it("drags a card as a media drag", async () => {
|
|
161
|
+
await mountGallery();
|
|
162
|
+
|
|
163
|
+
const { data } = dragCard(0);
|
|
164
|
+
|
|
165
|
+
expect(cards()[0]?.props.draggable).toBe(true);
|
|
166
|
+
expect(useMediaStore().dragged).toEqual({ kind: "media", ids: [7] });
|
|
167
|
+
expect(data[MEDIA_DRAG_TYPE]).toBeDefined();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("takes the whole bulk selection from one of its cards", async () => {
|
|
171
|
+
useMediaStore().editList = [8, 9];
|
|
172
|
+
await mountGallery();
|
|
173
|
+
|
|
174
|
+
dragCard(2);
|
|
175
|
+
|
|
176
|
+
expect(useMediaStore().dragged).toEqual({ kind: "media", ids: [8, 9] });
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("dims the dragged cards until the drag ends", async () => {
|
|
180
|
+
useMediaStore().editList = [8, 9];
|
|
181
|
+
await mountGallery();
|
|
182
|
+
|
|
183
|
+
dragCard(1);
|
|
184
|
+
await settle();
|
|
185
|
+
const dimmed = () =>
|
|
186
|
+
cards().map((card) => String(card.props.class).includes("opacity-50"));
|
|
187
|
+
expect(dimmed()).toEqual([false, true, true]);
|
|
188
|
+
|
|
189
|
+
(cards()[1]?.props.onDragend as () => void)();
|
|
190
|
+
await settle();
|
|
191
|
+
expect(useMediaStore().dragged).toBeNull();
|
|
192
|
+
expect(dimmed()).toEqual([false, false, false]);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("does not drag without the change permission, nor in a picker", async () => {
|
|
196
|
+
permissions.canChange = false;
|
|
197
|
+
await mountGallery();
|
|
198
|
+
|
|
199
|
+
dragCard(0);
|
|
200
|
+
expect(cards()[0]?.props.draggable).toBe(false);
|
|
201
|
+
expect(useMediaStore().dragged).toBeNull();
|
|
202
|
+
|
|
203
|
+
app?.unmount();
|
|
204
|
+
permissions.canChange = true;
|
|
205
|
+
useMediaStore().setSelectionMode("single");
|
|
206
|
+
await mountGallery();
|
|
207
|
+
|
|
208
|
+
dragCard(0);
|
|
209
|
+
expect(useMediaStore().dragged).toBeNull();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, onUnmounted } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext } from "vue";
|
|
4
|
+
import { createPinia, setActivePinia } from "pinia";
|
|
5
|
+
import type { Pinia } from "pinia";
|
|
6
|
+
|
|
7
|
+
import MapoMediaGallery from "../runtime/components/MapoMediaGallery.vue";
|
|
8
|
+
import MapoMediaUploader from "../runtime/components/MapoMediaUploader.vue";
|
|
9
|
+
import { useMediaStore } from "../runtime/stores/media";
|
|
10
|
+
|
|
11
|
+
// "Upload here" in the gallery and files dropped on it reach the uploader
|
|
12
|
+
// through the store's hand-over queue. The CRUD layer and the runtime config
|
|
13
|
+
// are in-memory doubles.
|
|
14
|
+
const crud = vi.hoisted(() => ({ options: vi.fn(), list: vi.fn() }));
|
|
15
|
+
|
|
16
|
+
vi.mock("#app", () => ({
|
|
17
|
+
useRuntimeConfig: () => ({ public: {} }),
|
|
18
|
+
useNuxtApp: () => ({}),
|
|
19
|
+
}));
|
|
20
|
+
vi.mock("vue-i18n", () => ({
|
|
21
|
+
useI18n: () => ({
|
|
22
|
+
t: (key: string, values?: Record<string, unknown>) =>
|
|
23
|
+
values ? `${key} ${JSON.stringify(values)}` : key,
|
|
24
|
+
}),
|
|
25
|
+
}));
|
|
26
|
+
vi.mock("@mapomodule/core/runtime/api/crud", () => ({ useCrud: () => crud }));
|
|
27
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
28
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
29
|
+
}));
|
|
30
|
+
vi.mock("@mapomodule/store/runtime/stores/confirm", () => ({
|
|
31
|
+
useConfirmStore: () => ({ ask: vi.fn() }),
|
|
32
|
+
}));
|
|
33
|
+
vi.mock("@mapomodule/i18n/runtime/composables/useMapoT", () => ({
|
|
34
|
+
useMapoT: () => (key: string) => key,
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
38
|
+
// enough to mount the component with real reactivity, watchers and events.
|
|
39
|
+
interface HeadlessNode {
|
|
40
|
+
parent: HeadlessNode | null;
|
|
41
|
+
children: HeadlessNode[];
|
|
42
|
+
text?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
46
|
+
|
|
47
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
48
|
+
createElement: headlessNode,
|
|
49
|
+
createText: (text) => ({ ...headlessNode(), text }),
|
|
50
|
+
createComment: headlessNode,
|
|
51
|
+
setText: (node, text) => {
|
|
52
|
+
node.text = text;
|
|
53
|
+
},
|
|
54
|
+
setElementText: (node, text) => {
|
|
55
|
+
node.text = text;
|
|
56
|
+
},
|
|
57
|
+
patchProp: () => {},
|
|
58
|
+
insert(child, parent, anchor) {
|
|
59
|
+
child.parent = parent;
|
|
60
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
61
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
62
|
+
},
|
|
63
|
+
remove(child) {
|
|
64
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
65
|
+
child.parent = null;
|
|
66
|
+
},
|
|
67
|
+
parentNode: (node) => node.parent,
|
|
68
|
+
nextSibling: (node) =>
|
|
69
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const textOf = (node: HeadlessNode): string =>
|
|
73
|
+
(node.text ?? "") + node.children.map(textOf).join("");
|
|
74
|
+
|
|
75
|
+
// `MapoDropArea` declares the contract both components bind: a `files` event
|
|
76
|
+
// and a default slot that receives `triggerPick`.
|
|
77
|
+
let dropArea: { drop(files: File[]): void; readonly picks: number } | null =
|
|
78
|
+
null;
|
|
79
|
+
const MapoDropArea: Component = {
|
|
80
|
+
name: "MapoDropArea",
|
|
81
|
+
emits: ["files"],
|
|
82
|
+
setup(_props: unknown, { emit, slots }: SetupContext<["files"]>) {
|
|
83
|
+
let picks = 0;
|
|
84
|
+
dropArea = {
|
|
85
|
+
drop: (files) => emit("files", files),
|
|
86
|
+
get picks() {
|
|
87
|
+
return picks;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
onUnmounted(() => {
|
|
91
|
+
dropArea = null;
|
|
92
|
+
});
|
|
93
|
+
return () =>
|
|
94
|
+
h(
|
|
95
|
+
"div",
|
|
96
|
+
null,
|
|
97
|
+
slots.default?.({
|
|
98
|
+
triggerPick: () => {
|
|
99
|
+
picks++;
|
|
100
|
+
},
|
|
101
|
+
isDragging: false,
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// `UButton` with its label, `click` is what both components listen to.
|
|
108
|
+
const buttons: { label: string; click(): void }[] = [];
|
|
109
|
+
const UButton: Component = {
|
|
110
|
+
name: "UButton",
|
|
111
|
+
emits: ["click"],
|
|
112
|
+
setup(_props: unknown, { emit, slots }: SetupContext<["click"]>) {
|
|
113
|
+
const stub = {
|
|
114
|
+
get label() {
|
|
115
|
+
return (slots.default?.() ?? [])
|
|
116
|
+
.map((node) =>
|
|
117
|
+
typeof node.children === "string" ? node.children : "",
|
|
118
|
+
)
|
|
119
|
+
.join("")
|
|
120
|
+
.trim();
|
|
121
|
+
},
|
|
122
|
+
click: () => emit("click"),
|
|
123
|
+
};
|
|
124
|
+
buttons.push(stub);
|
|
125
|
+
onUnmounted(() => {
|
|
126
|
+
buttons.splice(buttons.indexOf(stub), 1);
|
|
127
|
+
});
|
|
128
|
+
return () => h("button", null, slots.default?.());
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// One `UInput` per text field of each queued file.
|
|
133
|
+
let inputs = 0;
|
|
134
|
+
const UInput: Component = {
|
|
135
|
+
name: "UInput",
|
|
136
|
+
setup() {
|
|
137
|
+
inputs++;
|
|
138
|
+
onUnmounted(() => {
|
|
139
|
+
inputs--;
|
|
140
|
+
});
|
|
141
|
+
return () => h("input");
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const passthrough = (name: string): Component => ({
|
|
146
|
+
name,
|
|
147
|
+
inheritAttrs: false,
|
|
148
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
149
|
+
return () => h("div", null, slots.default?.());
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const stubs: Record<string, Component> = {
|
|
154
|
+
MapoDropArea,
|
|
155
|
+
UButton,
|
|
156
|
+
UInput,
|
|
157
|
+
UIcon: passthrough("UIcon"),
|
|
158
|
+
UCheckbox: passthrough("UCheckbox"),
|
|
159
|
+
USkeleton: passthrough("USkeleton"),
|
|
160
|
+
UPagination: passthrough("UPagination"),
|
|
161
|
+
UFormField: passthrough("UFormField"),
|
|
162
|
+
USelect: passthrough("USelect"),
|
|
163
|
+
UProgress: passthrough("UProgress"),
|
|
164
|
+
UBadge: passthrough("UBadge"),
|
|
165
|
+
MapoMediaPreview: passthrough("MapoMediaPreview"),
|
|
166
|
+
MapoMediaFolderPicker: passthrough("MapoMediaFolderPicker"),
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
let app: App<HeadlessNode> | undefined;
|
|
170
|
+
let pinia: Pinia;
|
|
171
|
+
let root: HeadlessNode;
|
|
172
|
+
const uploadRequests: number[] = [];
|
|
173
|
+
|
|
174
|
+
function mount(component: Component) {
|
|
175
|
+
root = headlessNode();
|
|
176
|
+
app = createApp({
|
|
177
|
+
render: () =>
|
|
178
|
+
h(component, { onUpload: () => uploadRequests.push(Date.now()) }),
|
|
179
|
+
});
|
|
180
|
+
app.use(pinia);
|
|
181
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
182
|
+
app.mount(root);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve));
|
|
186
|
+
const photo = () => new File(["x"], "photo.png", { type: "image/png" });
|
|
187
|
+
|
|
188
|
+
beforeEach(() => {
|
|
189
|
+
pinia = createPinia();
|
|
190
|
+
setActivePinia(pinia);
|
|
191
|
+
uploadRequests.length = 0;
|
|
192
|
+
inputs = 0;
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
afterEach(() => {
|
|
196
|
+
app?.unmount();
|
|
197
|
+
app = undefined;
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("upload from the gallery", () => {
|
|
201
|
+
it("tells where the files land: the folder on screen", async () => {
|
|
202
|
+
const store = useMediaStore();
|
|
203
|
+
store.parentFolders = [{ id: 3, name: "Photos", parent: null }];
|
|
204
|
+
mount(MapoMediaGallery as Component);
|
|
205
|
+
await settle();
|
|
206
|
+
|
|
207
|
+
expect(textOf(root)).toContain(
|
|
208
|
+
'mapo.mediaGallery.dropHint {"folder":"Photos"}',
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("queues the files dropped on it and asks for the Upload tab", async () => {
|
|
213
|
+
mount(MapoMediaGallery as Component);
|
|
214
|
+
const files = [photo(), photo()];
|
|
215
|
+
|
|
216
|
+
dropArea?.drop(files);
|
|
217
|
+
|
|
218
|
+
expect(useMediaStore().queuedUploads).toEqual(files);
|
|
219
|
+
expect(uploadRequests).toHaveLength(1);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("opens the file picker from Upload here", async () => {
|
|
223
|
+
mount(MapoMediaGallery as Component);
|
|
224
|
+
await settle();
|
|
225
|
+
|
|
226
|
+
buttons.find((b) => b.label === "mapo.mediaGallery.uploadHere")?.click();
|
|
227
|
+
|
|
228
|
+
expect(dropArea?.picks).toBe(1);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("lets the uploader queue the handed-over files when it opens", async () => {
|
|
232
|
+
useMediaStore().queueUploads([photo(), photo()]);
|
|
233
|
+
|
|
234
|
+
mount(MapoMediaUploader as Component);
|
|
235
|
+
await settle();
|
|
236
|
+
|
|
237
|
+
// Title, alt text and description for each of the two files.
|
|
238
|
+
expect(inputs).toBe(6);
|
|
239
|
+
expect(useMediaStore().queuedUploads).toEqual([]);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h, onUnmounted } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoMenuManager from "../runtime/components/MapoMenuManager.vue";
|
|
6
|
+
import type { MapoMenu } from "../runtime/types/menu";
|
|
7
|
+
|
|
8
|
+
const crud = vi.hoisted(() => ({
|
|
9
|
+
options: vi.fn(),
|
|
10
|
+
detail: vi.fn(),
|
|
11
|
+
create: vi.fn(),
|
|
12
|
+
update: vi.fn(),
|
|
13
|
+
partialUpdate: vi.fn(),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
// The app's `mapo.uikit` runtime config, changed by the content-language tests.
|
|
17
|
+
const runtimeConfig = vi.hoisted(() => ({
|
|
18
|
+
public: { mapoUikit: { contentLanguages: [] as string[] } },
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock("#app", () => ({ useRuntimeConfig: () => runtimeConfig }));
|
|
22
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
23
|
+
vi.mock("@mapomodule/core/runtime/api/crud", () => ({ useCrud: () => crud }));
|
|
24
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
25
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
26
|
+
}));
|
|
27
|
+
vi.mock("@mapomodule/store/runtime/composables/usePermissions", () => ({
|
|
28
|
+
usePermissions: () => ({ canAdd: () => true, canChange: () => true }),
|
|
29
|
+
}));
|
|
30
|
+
// The manager imports the tree and the node editor by path: replace the
|
|
31
|
+
// modules. The tree renders the `top` slot, where the language switch goes.
|
|
32
|
+
vi.mock("../runtime/components/MapoMenuTreeview.vue", async () => {
|
|
33
|
+
const { h } = await import("vue");
|
|
34
|
+
return {
|
|
35
|
+
default: {
|
|
36
|
+
name: "MapoMenuTreeview",
|
|
37
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
38
|
+
return () => h("div", null, [slots.top?.(), slots.bottom?.()]);
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
vi.mock("../runtime/components/MapoMenuNodeEditor.vue", () => ({
|
|
44
|
+
default: { name: "MapoMenuNodeEditor", render: () => null },
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
48
|
+
// enough to mount the component with real reactivity, watchers and events.
|
|
49
|
+
interface HeadlessNode {
|
|
50
|
+
parent: HeadlessNode | null;
|
|
51
|
+
children: HeadlessNode[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const headlessNode = (): HeadlessNode => ({ parent: null, children: [] });
|
|
55
|
+
|
|
56
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
57
|
+
createElement: headlessNode,
|
|
58
|
+
createText: headlessNode,
|
|
59
|
+
createComment: headlessNode,
|
|
60
|
+
setText: () => {},
|
|
61
|
+
setElementText: () => {},
|
|
62
|
+
patchProp: () => {},
|
|
63
|
+
insert(child, parent, anchor) {
|
|
64
|
+
child.parent = parent;
|
|
65
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
66
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
67
|
+
},
|
|
68
|
+
remove(child) {
|
|
69
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
70
|
+
child.parent = null;
|
|
71
|
+
},
|
|
72
|
+
parentNode: (node) => node.parent,
|
|
73
|
+
nextSibling: (node) =>
|
|
74
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// The language switch, resolved by name like the real one.
|
|
78
|
+
let langSwitch: { readonly langs: string[]; readonly value: string } | null =
|
|
79
|
+
null;
|
|
80
|
+
const MapoDetailLangSwitch: Component = {
|
|
81
|
+
name: "MapoDetailLangSwitch",
|
|
82
|
+
props: { modelValue: String, langs: Array },
|
|
83
|
+
setup(props: { modelValue?: string; langs?: string[] }) {
|
|
84
|
+
langSwitch = {
|
|
85
|
+
get langs() {
|
|
86
|
+
return props.langs ?? [];
|
|
87
|
+
},
|
|
88
|
+
get value() {
|
|
89
|
+
return props.modelValue ?? "";
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
onUnmounted(() => {
|
|
93
|
+
langSwitch = null;
|
|
94
|
+
});
|
|
95
|
+
return () => h("div");
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const stubs: Record<string, Component> = {
|
|
100
|
+
MapoDetailLangSwitch,
|
|
101
|
+
UButton: { name: "UButton", render: () => h("button") },
|
|
102
|
+
UIcon: { name: "UIcon", render: () => null },
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Runs the pending CRUD promises and the renders they trigger.
|
|
106
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve));
|
|
107
|
+
|
|
108
|
+
let app: App<HeadlessNode> | undefined;
|
|
109
|
+
|
|
110
|
+
async function mountManager(props: Record<string, unknown> = {}) {
|
|
111
|
+
app = createApp({
|
|
112
|
+
render: () =>
|
|
113
|
+
h(MapoMenuManager as Component, { endpoint: "/api/menus", ...props }),
|
|
114
|
+
});
|
|
115
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
116
|
+
app.mount(headlessNode());
|
|
117
|
+
await settle();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
beforeEach(() => {
|
|
121
|
+
runtimeConfig.public.mapoUikit.contentLanguages = [];
|
|
122
|
+
for (const fn of Object.values(crud)) fn.mockReset();
|
|
123
|
+
crud.options.mockResolvedValue({});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
afterEach(() => {
|
|
127
|
+
app?.unmount();
|
|
128
|
+
app = undefined;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("MapoMenuManager language switch", () => {
|
|
132
|
+
it("shows the switch with several languages", async () => {
|
|
133
|
+
await mountManager({ languages: ["it", "en"] });
|
|
134
|
+
expect(langSwitch?.langs).toEqual(["it", "en"]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("hides the switch with a single language", async () => {
|
|
138
|
+
await mountManager({ languages: ["it"] });
|
|
139
|
+
expect(langSwitch).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Hiding the switch changes nothing in the data: the tree of a menu
|
|
143
|
+
// translated into one language still lives under that language.
|
|
144
|
+
it("keeps the single language's tree under its translation", async () => {
|
|
145
|
+
crud.options.mockResolvedValue({
|
|
146
|
+
lang_info: { translatable: true, languages: ["it"] },
|
|
147
|
+
});
|
|
148
|
+
const menu: MapoMenu = { key: "main", nodes: [], translations: {} };
|
|
149
|
+
await mountManager({ modelValue: menu });
|
|
150
|
+
|
|
151
|
+
expect(langSwitch).toBeNull();
|
|
152
|
+
expect(menu.translations?.it?.nodes).toEqual([]);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("MapoMenuManager content languages", () => {
|
|
157
|
+
it("keeps only the app's content languages", async () => {
|
|
158
|
+
runtimeConfig.public.mapoUikit.contentLanguages = ["it"];
|
|
159
|
+
const menu: MapoMenu = { key: "main", nodes: [], translations: {} };
|
|
160
|
+
await mountManager({ languages: ["en", "it"], modelValue: menu });
|
|
161
|
+
|
|
162
|
+
expect(langSwitch).toBeNull();
|
|
163
|
+
// English was the page's first language: the tree is the Italian one.
|
|
164
|
+
expect(menu.translations?.it?.nodes).toEqual([]);
|
|
165
|
+
expect(menu.translations?.en).toBeUndefined();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("follows the app's order, so its first language is the starting one", async () => {
|
|
169
|
+
runtimeConfig.public.mapoUikit.contentLanguages = ["en", "it"];
|
|
170
|
+
await mountManager({ languages: ["it", "en"] });
|
|
171
|
+
|
|
172
|
+
expect(langSwitch?.langs).toEqual(["en", "it"]);
|
|
173
|
+
expect(langSwitch?.value).toBe("en");
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
languagesFromMetadata,
|
|
5
|
+
pickLanguage,
|
|
6
|
+
restrictLanguages,
|
|
7
|
+
} from "../runtime/utils/langInfo";
|
|
4
8
|
|
|
5
9
|
describe("languagesFromMetadata", () => {
|
|
6
10
|
it("reads the model's own language list", () => {
|
|
@@ -46,3 +50,36 @@ describe("languagesFromMetadata", () => {
|
|
|
46
50
|
).toEqual(["it"]);
|
|
47
51
|
});
|
|
48
52
|
});
|
|
53
|
+
|
|
54
|
+
describe("pickLanguage", () => {
|
|
55
|
+
it("starts from the preferred language when the list offers it", () => {
|
|
56
|
+
expect(pickLanguage(["it", "en"], "en")).toBe("en");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("falls back to the first language otherwise", () => {
|
|
60
|
+
expect(pickLanguage(["it", "en"], "de")).toBe("it");
|
|
61
|
+
expect(pickLanguage(["it", "en"])).toBe("it");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns an empty code for a model that is not translated", () => {
|
|
65
|
+
expect(pickLanguage([], "it")).toBe("");
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("restrictLanguages", () => {
|
|
70
|
+
it("restricts nothing without content languages", () => {
|
|
71
|
+
expect(restrictLanguages(["it", "en"], [])).toEqual(["it", "en"]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("keeps the content languages, in their order", () => {
|
|
75
|
+
expect(restrictLanguages(["it", "en", "fr"], ["fr", "it"])).toEqual([
|
|
76
|
+
"fr",
|
|
77
|
+
"it",
|
|
78
|
+
]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("never adds a language the list does not offer", () => {
|
|
82
|
+
expect(restrictLanguages(["it", "en"], ["it", "de"])).toEqual(["it"]);
|
|
83
|
+
expect(restrictLanguages([], ["it"])).toEqual([]);
|
|
84
|
+
});
|
|
85
|
+
});
|