@mapomodule/form 2.0.0-beta.9 → 2.0.0-experimental.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -119
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -0
- 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 +9 -0
- package/dist/runtime/components/MapoFocusPortal.vue +9 -2
- package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoForm.vue +28 -6
- package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
- package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
- package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
- package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoFormField.vue +47 -11
- package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
- package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +18 -2
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormTabs.vue +50 -8
- package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
- package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
- package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
- package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
- package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
- package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
- package/dist/runtime/composables/useFieldAnnotation.js +16 -0
- package/dist/runtime/composables/useFocusMode.d.ts +3 -1
- package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
- package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
- package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
- package/dist/runtime/composables/useFormFromSchema.js +171 -32
- package/dist/runtime/composables/useMapoForm.d.ts +21 -3
- package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
- package/dist/runtime/composables/useMapoForm.js +49 -15
- package/dist/runtime/types/annotation.d.ts +43 -0
- package/dist/runtime/types/annotation.d.ts.map +1 -0
- package/dist/runtime/types/annotation.js +0 -0
- package/dist/runtime/types/descriptor.d.ts +81 -4
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +2 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/annotations.d.ts +40 -0
- package/dist/runtime/utils/annotations.d.ts.map +1 -0
- package/dist/runtime/utils/annotations.js +78 -0
- package/dist/runtime/utils/draft.d.ts +21 -0
- package/dist/runtime/utils/draft.d.ts.map +1 -0
- package/dist/runtime/utils/draft.js +108 -0
- package/dist/runtime/utils/fieldComponent.d.ts +9 -0
- package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
- package/dist/runtime/utils/fieldComponent.js +12 -0
- package/dist/runtime/utils/fieldFocus.d.ts +24 -0
- package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
- package/dist/runtime/utils/fieldFocus.js +41 -0
- package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
- package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
- package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
- package/dist/runtime/utils/repeaterTemplates.js +51 -0
- package/dist/runtime/utils/reveal.d.ts +34 -0
- package/dist/runtime/utils/reveal.d.ts.map +1 -0
- package/dist/runtime/utils/reveal.js +29 -0
- package/dist/runtime/utils/tabVisibility.d.ts +18 -0
- package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
- package/dist/runtime/utils/tabVisibility.js +18 -0
- package/package.json +5 -5
- package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
- package/src/__tests__/MapoForm.component.test.ts +79 -0
- package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
- package/src/__tests__/MapoFormField.component.test.ts +508 -0
- package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
- package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
- package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
- package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
- package/src/__tests__/annotations.test.ts +227 -0
- package/src/__tests__/draft.test.ts +221 -0
- package/src/__tests__/fieldComponent.test.ts +31 -0
- package/src/__tests__/fieldExtensions.test.ts +63 -0
- package/src/__tests__/fieldFocus.test.ts +178 -0
- package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
- package/src/__tests__/headlessRenderer.ts +85 -0
- package/src/__tests__/repeaterTemplates.test.ts +154 -0
- package/src/__tests__/reveal.test.ts +89 -0
- package/src/__tests__/schemaSources.test.ts +132 -0
- package/src/__tests__/tabVisibility.test.ts +102 -0
- package/src/__tests__/useFormFromSchema.test.ts +387 -0
- package/src/__tests__/useMapoForm.test.ts +213 -2
- package/src/index.ts +9 -0
- package/src/module.ts +9 -0
- package/src/runtime/components/MapoFocusPortal.vue +15 -2
- package/src/runtime/components/MapoForm.vue +47 -4
- package/src/runtime/components/MapoFormAnnotation.vue +113 -0
- package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
- package/src/runtime/components/MapoFormField.vue +72 -11
- package/src/runtime/components/MapoFormGroup.vue +20 -2
- package/src/runtime/components/MapoFormTabs.vue +64 -8
- package/src/runtime/components/fields/MapoMediaField.vue +5 -3
- package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
- package/src/runtime/components/fields/MapoRepeater.vue +82 -14
- package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
- package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
- package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
- package/src/runtime/composables/useFieldAnnotation.ts +38 -0
- package/src/runtime/composables/useFocusMode.ts +7 -1
- package/src/runtime/composables/useFormFromSchema.ts +303 -41
- package/src/runtime/composables/useMapoForm.ts +103 -23
- package/src/runtime/types/annotation.ts +53 -0
- package/src/runtime/types/descriptor.ts +94 -4
- package/src/runtime/types/index.ts +10 -0
- package/src/runtime/utils/annotations.ts +169 -0
- package/src/runtime/utils/draft.ts +158 -0
- package/src/runtime/utils/fieldComponent.ts +34 -0
- package/src/runtime/utils/fieldFocus.ts +73 -0
- package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
- package/src/runtime/utils/repeaterTemplates.ts +103 -0
- package/src/runtime/utils/reveal.ts +68 -0
- package/src/runtime/utils/tabVisibility.ts +44 -0
- package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
- package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
- package/dist/__tests__/camomillaRoundTrip.test.d.ts +0 -2
- package/dist/__tests__/camomillaRoundTrip.test.d.ts.map +0 -1
- package/dist/__tests__/registry.test.d.ts +0 -2
- package/dist/__tests__/registry.test.d.ts.map +0 -1
- package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
- package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
- package/dist/__tests__/useMapoForm.test.d.ts +0 -2
- package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
annotationFor,
|
|
4
|
+
entriesUnder,
|
|
5
|
+
tabAnnotationGroups,
|
|
6
|
+
valuePath,
|
|
7
|
+
} from "../runtime/utils/annotations.js";
|
|
8
|
+
import type {
|
|
9
|
+
AnyFieldDescriptor,
|
|
10
|
+
FieldAnnotation,
|
|
11
|
+
FieldAnnotations,
|
|
12
|
+
} from "../runtime/types/index.js";
|
|
13
|
+
import type { TabEntry } from "../runtime/types/layout.js";
|
|
14
|
+
|
|
15
|
+
const ai: FieldAnnotation = {
|
|
16
|
+
label: "Suggested by AI",
|
|
17
|
+
icon: "i-lucide-sparkles",
|
|
18
|
+
color: "info",
|
|
19
|
+
};
|
|
20
|
+
const toCheck: FieldAnnotation = { ...ai, label: "To check", color: "warning" };
|
|
21
|
+
const note: FieldAnnotation = { label: "Note" };
|
|
22
|
+
|
|
23
|
+
/** A tab with one flat group of fields, plus optional sub-tabs and child tabs. */
|
|
24
|
+
function tabOf(
|
|
25
|
+
fields: AnyFieldDescriptor[],
|
|
26
|
+
extra: {
|
|
27
|
+
subtabs?: Record<string, AnyFieldDescriptor[]>;
|
|
28
|
+
children?: TabEntry[];
|
|
29
|
+
} = {},
|
|
30
|
+
): TabEntry {
|
|
31
|
+
return {
|
|
32
|
+
name: "content",
|
|
33
|
+
groups: new Map([
|
|
34
|
+
[
|
|
35
|
+
"__flat__",
|
|
36
|
+
{ fields, subtabs: new Map(Object.entries(extra.subtabs ?? {})) },
|
|
37
|
+
],
|
|
38
|
+
]),
|
|
39
|
+
children: new Map((extra.children ?? []).map((tab) => [tab.name, tab])),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** How many marks the badges of a tab count, all groups together. */
|
|
44
|
+
const countOf = (
|
|
45
|
+
tab: TabEntry,
|
|
46
|
+
annotations: FieldAnnotations,
|
|
47
|
+
model: Record<string, unknown> = {},
|
|
48
|
+
lang = "",
|
|
49
|
+
) =>
|
|
50
|
+
tabAnnotationGroups(tab, annotations, model, lang).reduce(
|
|
51
|
+
(sum, group) => sum + group.count,
|
|
52
|
+
0,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
describe("valuePath", () => {
|
|
56
|
+
it("is the key of a field", () => {
|
|
57
|
+
expect(valuePath({ key: "title", type: "text" }, "it")).toBe("title");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("is the translation in the current language for a translatable field", () => {
|
|
61
|
+
const field: AnyFieldDescriptor = {
|
|
62
|
+
key: "title",
|
|
63
|
+
type: "text",
|
|
64
|
+
translatable: true,
|
|
65
|
+
};
|
|
66
|
+
expect(valuePath(field, "it")).toBe("translations.it.title");
|
|
67
|
+
expect(valuePath(field, "")).toBe("title");
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("annotationFor", () => {
|
|
72
|
+
it("finds the annotation at the path of the value", () => {
|
|
73
|
+
expect(annotationFor({ title: ai }, "title", "title")).toBe(ai);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("prefers the current language, then the bare key of a translatable field", () => {
|
|
77
|
+
const annotations = { "translations.it.title": toCheck, title: ai };
|
|
78
|
+
expect(annotationFor(annotations, "title", "translations.it.title")).toBe(
|
|
79
|
+
toCheck,
|
|
80
|
+
);
|
|
81
|
+
expect(annotationFor({ title: ai }, "title", "translations.it.title")).toBe(
|
|
82
|
+
ai,
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("finds nothing for a field without annotations", () => {
|
|
87
|
+
expect(annotationFor({ slug: ai }, "title", "title")).toBeUndefined();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("entriesUnder", () => {
|
|
92
|
+
it("keeps the entries under the prefix, keyed relative to it", () => {
|
|
93
|
+
const record = {
|
|
94
|
+
"blocks.0.title": ai,
|
|
95
|
+
"blocks.1.title": note,
|
|
96
|
+
"blocks.0": toCheck,
|
|
97
|
+
title: note,
|
|
98
|
+
};
|
|
99
|
+
expect(entriesUnder(record, "blocks.0")).toEqual({ title: ai });
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("tabAnnotationGroups", () => {
|
|
104
|
+
it("groups the annotations of a tab by icon and color", () => {
|
|
105
|
+
const tab = tabOf([
|
|
106
|
+
{ key: "title", type: "text" },
|
|
107
|
+
{ key: "summary", type: "textarea" },
|
|
108
|
+
{ key: "slug", type: "text" },
|
|
109
|
+
{ key: "body", type: "editor" },
|
|
110
|
+
]);
|
|
111
|
+
const annotations = { title: ai, summary: ai, slug: toCheck, body: note };
|
|
112
|
+
|
|
113
|
+
expect(tabAnnotationGroups(tab, annotations, {}, "")).toEqual([
|
|
114
|
+
{
|
|
115
|
+
icon: "i-lucide-sparkles",
|
|
116
|
+
color: "info",
|
|
117
|
+
count: 2,
|
|
118
|
+
labels: [ai.label],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
icon: "i-lucide-sparkles",
|
|
122
|
+
color: "warning",
|
|
123
|
+
count: 1,
|
|
124
|
+
labels: [toCheck.label],
|
|
125
|
+
},
|
|
126
|
+
{ icon: "i-lucide-info", color: "neutral", count: 1, labels: ["Note"] },
|
|
127
|
+
]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("counts the marks in sub-tabs and child tabs", () => {
|
|
131
|
+
const tab = tabOf([{ key: "title", type: "text" }], {
|
|
132
|
+
subtabs: { seo: [{ key: "seo_title", type: "text" }] },
|
|
133
|
+
children: [tabOf([{ key: "author", type: "text" }])],
|
|
134
|
+
});
|
|
135
|
+
const annotations = { title: ai, seo_title: ai, author: ai };
|
|
136
|
+
|
|
137
|
+
expect(countOf(tab, annotations)).toBe(3);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("counts the marks of the items a repeater renders, as they render", () => {
|
|
141
|
+
const blocks: AnyFieldDescriptor = {
|
|
142
|
+
key: "blocks",
|
|
143
|
+
type: "repeater",
|
|
144
|
+
fields: [
|
|
145
|
+
{ key: "title", type: "text" },
|
|
146
|
+
{ key: "note", type: "text", show: ({ model }) => !!model.withNote },
|
|
147
|
+
],
|
|
148
|
+
};
|
|
149
|
+
const model = { blocks: [{ title: "A" }, { title: "B", withNote: true }] };
|
|
150
|
+
const annotations = {
|
|
151
|
+
"blocks.0.title": ai,
|
|
152
|
+
"blocks.1.title": ai,
|
|
153
|
+
"blocks.0.note": ai, // hidden in the first item
|
|
154
|
+
"blocks.1.note": ai,
|
|
155
|
+
"blocks.9.title": ai, // no such item
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
expect(countOf(tabOf([blocks]), annotations, model)).toBe(3);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("counts a translatable field in the current language, inside items too", () => {
|
|
162
|
+
const blocks: AnyFieldDescriptor = {
|
|
163
|
+
key: "blocks",
|
|
164
|
+
type: "repeater",
|
|
165
|
+
fields: [{ key: "title", type: "text", translatable: true }],
|
|
166
|
+
};
|
|
167
|
+
const tab = tabOf([
|
|
168
|
+
{ key: "title", type: "text", translatable: true },
|
|
169
|
+
blocks,
|
|
170
|
+
]);
|
|
171
|
+
const annotations = {
|
|
172
|
+
"translations.it.title": ai,
|
|
173
|
+
"translations.en.title": ai,
|
|
174
|
+
"blocks.0.translations.it.title": ai,
|
|
175
|
+
"blocks.1.translations.en.title": ai,
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
expect(countOf(tab, annotations, { blocks: [{}, {}] }, "it")).toBe(2);
|
|
179
|
+
expect(countOf(tab, annotations, { blocks: [{}, {}] }, "fr")).toBe(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("finds the items of a translatable repeater under its key, as the repeater does", () => {
|
|
183
|
+
const blocks: AnyFieldDescriptor = {
|
|
184
|
+
key: "blocks",
|
|
185
|
+
type: "repeater",
|
|
186
|
+
translatable: true,
|
|
187
|
+
fields: [{ key: "title", type: "text" }],
|
|
188
|
+
};
|
|
189
|
+
const model = { translations: { it: { blocks: [{ title: "A" }] } } };
|
|
190
|
+
|
|
191
|
+
expect(
|
|
192
|
+
countOf(tabOf([blocks]), { "blocks.0.title": ai }, model, "it"),
|
|
193
|
+
).toBe(1);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("uses the fields of each item's template", () => {
|
|
197
|
+
const blocks: AnyFieldDescriptor = {
|
|
198
|
+
key: "blocks",
|
|
199
|
+
type: "repeater",
|
|
200
|
+
attrs: {
|
|
201
|
+
templates: {
|
|
202
|
+
text: [{ key: "body", type: "textarea" }],
|
|
203
|
+
image: [{ key: "alt", type: "text" }],
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
const model = { blocks: [{ _template: "image" }, {}] };
|
|
208
|
+
const annotations = {
|
|
209
|
+
"blocks.0.alt": ai,
|
|
210
|
+
"blocks.0.body": ai, // not a field of the image template
|
|
211
|
+
"blocks.1.body": ai, // no template: the first one, text
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
expect(countOf(tabOf([blocks]), annotations, model)).toBe(2);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("leaves out the fields the model hides", () => {
|
|
218
|
+
const tab = tabOf([
|
|
219
|
+
{ key: "title", type: "text" },
|
|
220
|
+
{ key: "price", type: "number", show: ({ model }) => !!model.paid },
|
|
221
|
+
]);
|
|
222
|
+
const annotations = { title: ai, price: ai };
|
|
223
|
+
|
|
224
|
+
expect(countOf(tab, annotations)).toBe(1);
|
|
225
|
+
expect(countOf(tab, annotations, { paid: true })).toBe(2);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { reactive } from "vue";
|
|
3
|
+
import { modelForDraft } from "../runtime/utils/draft.js";
|
|
4
|
+
import type { AnyFieldDescriptor } from "../runtime/types/index.js";
|
|
5
|
+
|
|
6
|
+
describe("modelForDraft", () => {
|
|
7
|
+
it("leaves out a noDraft field at its key, and keeps the others", () => {
|
|
8
|
+
const fields: AnyFieldDescriptor[] = [
|
|
9
|
+
{ key: "title", type: "text" },
|
|
10
|
+
{ key: "password", type: "text", noDraft: true },
|
|
11
|
+
];
|
|
12
|
+
const draft = modelForDraft({ title: "Hello", password: "s3cret" }, fields);
|
|
13
|
+
expect(draft).toEqual({ title: "Hello" });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("leaves out a noDraft field under a dotted key, and keeps its siblings", () => {
|
|
17
|
+
const fields: AnyFieldDescriptor[] = [
|
|
18
|
+
{ key: "api.name", type: "text" },
|
|
19
|
+
{ key: "api.key", type: "text", noDraft: true },
|
|
20
|
+
];
|
|
21
|
+
const draft = modelForDraft(
|
|
22
|
+
{ api: { name: "Maps", key: "k-123" } },
|
|
23
|
+
fields,
|
|
24
|
+
);
|
|
25
|
+
expect(draft).toEqual({ api: { name: "Maps" } });
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("leaves out a translatable noDraft field in every language and at its key", () => {
|
|
29
|
+
const fields: AnyFieldDescriptor[] = [
|
|
30
|
+
{ key: "title", type: "text", translatable: true },
|
|
31
|
+
{ key: "token", type: "text", translatable: true, noDraft: true },
|
|
32
|
+
];
|
|
33
|
+
const draft = modelForDraft(
|
|
34
|
+
{
|
|
35
|
+
token: "root",
|
|
36
|
+
translations: {
|
|
37
|
+
en: { title: "Hello", token: "t-en" },
|
|
38
|
+
it: { title: "Ciao", token: "t-it" },
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
fields,
|
|
42
|
+
);
|
|
43
|
+
expect(draft).toEqual({
|
|
44
|
+
translations: { en: { title: "Hello" }, it: { title: "Ciao" } },
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("leaves out a synced noDraft field in every language", () => {
|
|
49
|
+
const fields: AnyFieldDescriptor[] = [
|
|
50
|
+
{ key: "pin", type: "text", synci18n: true, noDraft: true },
|
|
51
|
+
];
|
|
52
|
+
const draft = modelForDraft(
|
|
53
|
+
{ translations: { en: { pin: "1234" }, it: { pin: "1234" } } },
|
|
54
|
+
fields,
|
|
55
|
+
);
|
|
56
|
+
expect(draft).toEqual({ translations: { en: {}, it: {} } });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("keeps a value of the same name in the languages for a field that is not translated", () => {
|
|
60
|
+
const fields: AnyFieldDescriptor[] = [
|
|
61
|
+
{ key: "code", type: "text", noDraft: true },
|
|
62
|
+
];
|
|
63
|
+
const draft = modelForDraft(
|
|
64
|
+
{ code: "x", translations: { en: { code: "label" } } },
|
|
65
|
+
fields,
|
|
66
|
+
);
|
|
67
|
+
expect(draft).toEqual({ translations: { en: { code: "label" } } });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("leaves out a noDraft field in each item of a repeater", () => {
|
|
71
|
+
const fields: AnyFieldDescriptor[] = [
|
|
72
|
+
{
|
|
73
|
+
key: "speakers",
|
|
74
|
+
type: "repeater",
|
|
75
|
+
fields: [
|
|
76
|
+
{ key: "name", type: "text" },
|
|
77
|
+
{ key: "phone", type: "text", noDraft: true },
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
const draft = modelForDraft(
|
|
82
|
+
{
|
|
83
|
+
speakers: [
|
|
84
|
+
{ name: "Ada", phone: "555" },
|
|
85
|
+
{ name: "Grace", phone: "556" },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
fields,
|
|
89
|
+
);
|
|
90
|
+
expect(draft).toEqual({ speakers: [{ name: "Ada" }, { name: "Grace" }] });
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("reads each item by its template, and an item without one by the first", () => {
|
|
94
|
+
const fields: AnyFieldDescriptor[] = [
|
|
95
|
+
{
|
|
96
|
+
key: "blocks",
|
|
97
|
+
type: "repeater",
|
|
98
|
+
attrs: {
|
|
99
|
+
templates: {
|
|
100
|
+
login: [
|
|
101
|
+
{ key: "user", type: "text" },
|
|
102
|
+
{ key: "secret", type: "text", noDraft: true },
|
|
103
|
+
],
|
|
104
|
+
text: [
|
|
105
|
+
{ key: "body", type: "textarea" },
|
|
106
|
+
{ key: "secret", type: "text" },
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
const draft = modelForDraft(
|
|
113
|
+
{
|
|
114
|
+
blocks: [
|
|
115
|
+
{ _template: "login", user: "ada", secret: "a" },
|
|
116
|
+
{ _template: "text", body: "Hi", secret: "kept" },
|
|
117
|
+
{ user: "grace", secret: "b" },
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
fields,
|
|
121
|
+
);
|
|
122
|
+
expect(draft).toEqual({
|
|
123
|
+
blocks: [
|
|
124
|
+
{ _template: "login", user: "ada" },
|
|
125
|
+
{ _template: "text", body: "Hi", secret: "kept" },
|
|
126
|
+
{ user: "grace" },
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("reaches repeaters in every language and inside other repeaters", () => {
|
|
132
|
+
const fields: AnyFieldDescriptor[] = [
|
|
133
|
+
{
|
|
134
|
+
key: "sections",
|
|
135
|
+
type: "repeater",
|
|
136
|
+
translatable: true,
|
|
137
|
+
fields: [
|
|
138
|
+
{
|
|
139
|
+
key: "rows",
|
|
140
|
+
type: "repeater",
|
|
141
|
+
fields: [
|
|
142
|
+
{ key: "label", type: "text" },
|
|
143
|
+
{ key: "otp", type: "text", noDraft: true },
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
];
|
|
149
|
+
const draft = modelForDraft(
|
|
150
|
+
{
|
|
151
|
+
translations: {
|
|
152
|
+
en: { sections: [{ rows: [{ label: "A", otp: "1" }] }] },
|
|
153
|
+
it: { sections: [{ rows: [{ label: "B", otp: "2" }] }] },
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
fields,
|
|
157
|
+
);
|
|
158
|
+
expect(draft).toEqual({
|
|
159
|
+
translations: {
|
|
160
|
+
en: { sections: [{ rows: [{ label: "A" }] }] },
|
|
161
|
+
it: { sections: [{ rows: [{ label: "B" }] }] },
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("returns a copy, and leaves the model as it is", () => {
|
|
167
|
+
const fields: AnyFieldDescriptor[] = [
|
|
168
|
+
{ key: "password", type: "text", noDraft: true },
|
|
169
|
+
];
|
|
170
|
+
const model = reactive({ title: "Hello", password: "s3cret" });
|
|
171
|
+
const draft = modelForDraft(model, fields);
|
|
172
|
+
expect(draft).toEqual({ title: "Hello" });
|
|
173
|
+
expect(model).toEqual({ title: "Hello", password: "s3cret" });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("goes through no key that leads to a prototype", () => {
|
|
177
|
+
const fields: AnyFieldDescriptor[] = [
|
|
178
|
+
{ key: "__proto__.polluted", type: "text", noDraft: true },
|
|
179
|
+
{ key: "constructor", type: "text", noDraft: true },
|
|
180
|
+
];
|
|
181
|
+
const draft = modelForDraft({ title: "Hello" }, fields);
|
|
182
|
+
expect(draft).toEqual({ title: "Hello" });
|
|
183
|
+
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("keeps only what JSON carries back as it was: no file, date, map or number JSON cannot write", () => {
|
|
187
|
+
const fields: AnyFieldDescriptor[] = [{ key: "title", type: "text" }];
|
|
188
|
+
const draft = modelForDraft(
|
|
189
|
+
{
|
|
190
|
+
title: "Hello",
|
|
191
|
+
subtitle: undefined,
|
|
192
|
+
cover: new Blob(["image"]),
|
|
193
|
+
published_on: new Date("2026-11-10T09:00:00.000Z"),
|
|
194
|
+
ratio: Number.NaN,
|
|
195
|
+
tags: ["open data", new Date(0), null],
|
|
196
|
+
seen: new Map([["a", 1]]),
|
|
197
|
+
seo: { title: "Open data", image: new Blob(["og"]) },
|
|
198
|
+
},
|
|
199
|
+
fields,
|
|
200
|
+
);
|
|
201
|
+
expect(draft).toEqual({
|
|
202
|
+
title: "Hello",
|
|
203
|
+
tags: ["open data", null],
|
|
204
|
+
seo: { title: "Open data" },
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("keeps a key named __proto__ as data, and ends a value met again inside itself", () => {
|
|
209
|
+
const fields: AnyFieldDescriptor[] = [{ key: "title", type: "text" }];
|
|
210
|
+
const model: Record<string, unknown> = JSON.parse(
|
|
211
|
+
'{"title":"Hello","__proto__":{"polluted":true}}',
|
|
212
|
+
);
|
|
213
|
+
model.self = model;
|
|
214
|
+
|
|
215
|
+
const draft = modelForDraft(model, fields) as Record<string, unknown>;
|
|
216
|
+
|
|
217
|
+
expect(Object.keys(draft)).toEqual(["title", "__proto__"]);
|
|
218
|
+
expect(Object.getPrototypeOf(draft)).toBe(Object.prototype);
|
|
219
|
+
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import type { Component } from "vue";
|
|
3
|
+
import { toFieldComponent } from "../runtime/utils/fieldComponent.js";
|
|
4
|
+
|
|
5
|
+
const loaderOf = (component: Component) => () =>
|
|
6
|
+
Promise.resolve({ default: component });
|
|
7
|
+
|
|
8
|
+
describe("toFieldComponent", () => {
|
|
9
|
+
it("wraps a lazy loader in an async component", () => {
|
|
10
|
+
const loader = loaderOf({ name: "TextField" });
|
|
11
|
+
expect(toFieldComponent(loader)).not.toBe(loader);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("returns the same async component for the same loader", () => {
|
|
15
|
+
const loader = loaderOf({ name: "TextField" });
|
|
16
|
+
expect(toFieldComponent(loader)).toBe(toFieldComponent(loader));
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("gives each loader its own async component", () => {
|
|
20
|
+
const component: Component = { name: "TextField" };
|
|
21
|
+
expect(toFieldComponent(loaderOf(component))).not.toBe(
|
|
22
|
+
toFieldComponent(loaderOf(component)),
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("returns component names and components as they are", () => {
|
|
27
|
+
const component: Component = { name: "StarRatingField" };
|
|
28
|
+
expect(toFieldComponent("UInput")).toBe("UInput");
|
|
29
|
+
expect(toFieldComponent(component)).toBe(component);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AnyFieldDescriptor,
|
|
5
|
+
FieldDescriptor,
|
|
6
|
+
} from "../runtime/types/index.js";
|
|
7
|
+
import { fieldsToJsonSchema } from "../runtime/utils/fieldsToJsonSchema.js";
|
|
8
|
+
|
|
9
|
+
// A package declares a descriptor property of its own. `pnpm typecheck`
|
|
10
|
+
// checks this file: the `@ts-expect-error` lines fail it when the extension
|
|
11
|
+
// stops being checked.
|
|
12
|
+
declare module "../runtime/types/descriptor.js" {
|
|
13
|
+
interface FieldExtensions {
|
|
14
|
+
example?: { hint?: string; exclude?: boolean };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("FieldExtensions", () => {
|
|
19
|
+
it("adds the declared property to every descriptor type", () => {
|
|
20
|
+
expectTypeOf<FieldDescriptor["example"]>().toEqualTypeOf<
|
|
21
|
+
{ hint?: string; exclude?: boolean } | undefined
|
|
22
|
+
>();
|
|
23
|
+
expectTypeOf<AnyFieldDescriptor["example"]>().toEqualTypeOf<
|
|
24
|
+
{ hint?: string; exclude?: boolean } | undefined
|
|
25
|
+
>();
|
|
26
|
+
|
|
27
|
+
const price: FieldDescriptor = {
|
|
28
|
+
key: "price",
|
|
29
|
+
type: "number",
|
|
30
|
+
example: { hint: "Whole euros" },
|
|
31
|
+
};
|
|
32
|
+
expect(price.example?.hint).toBe("Whole euros");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("type-checks the declared property and rejects undeclared ones", () => {
|
|
36
|
+
const fields: FieldDescriptor[] = [
|
|
37
|
+
// @ts-expect-error `hnt` is not in the declared shape.
|
|
38
|
+
{ key: "title", type: "text", example: { hnt: "typo" } },
|
|
39
|
+
// @ts-expect-error `undeclared` was never declared.
|
|
40
|
+
{ key: "title", type: "text", undeclared: true },
|
|
41
|
+
];
|
|
42
|
+
expect(fields).toHaveLength(2);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("reaches the schema through the property option", () => {
|
|
46
|
+
const schema = fieldsToJsonSchema(
|
|
47
|
+
[
|
|
48
|
+
{ key: "price", type: "number", example: { hint: "Whole euros" } },
|
|
49
|
+
{ key: "slug", type: "text", example: { exclude: true } },
|
|
50
|
+
],
|
|
51
|
+
{
|
|
52
|
+
property: (written, field) => {
|
|
53
|
+
if (field.example?.exclude) return undefined;
|
|
54
|
+
const hint = field.example?.hint;
|
|
55
|
+
return hint ? { ...written, description: hint } : written;
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
expect(schema.properties).toEqual({
|
|
60
|
+
price: { type: "number", description: "Whole euros" },
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|