@mapomodule/form 2.0.0-beta.14 → 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.
Files changed (30) hide show
  1. package/dist/__tests__/MapoFormGroup.component.test.d.ts +2 -0
  2. package/dist/__tests__/MapoFormGroup.component.test.d.ts.map +1 -0
  3. package/dist/__tests__/fieldVisibility.component.test.d.ts +2 -0
  4. package/dist/__tests__/fieldVisibility.component.test.d.ts.map +1 -0
  5. package/dist/module.json +1 -1
  6. package/dist/runtime/components/MapoFormField.vue +5 -10
  7. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  8. package/dist/runtime/components/MapoFormFlatSection.vue +21 -3
  9. package/dist/runtime/components/MapoFormFlatSection.vue.d.ts +1 -0
  10. package/dist/runtime/components/MapoFormFlatSection.vue.d.ts.map +1 -1
  11. package/dist/runtime/components/MapoFormGroup.vue +69 -34
  12. package/dist/runtime/components/MapoFormGroup.vue.d.ts +19 -4
  13. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  14. package/dist/runtime/components/fields/MapoRepeaterItem.vue +18 -2
  15. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  16. package/dist/runtime/types/descriptor.d.ts +10 -2
  17. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  18. package/dist/runtime/utils/tabVisibility.d.ts +8 -2
  19. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -1
  20. package/dist/runtime/utils/tabVisibility.js +20 -2
  21. package/package.json +2 -2
  22. package/src/__tests__/MapoFormGroup.component.test.ts +130 -0
  23. package/src/__tests__/fieldVisibility.component.test.ts +546 -0
  24. package/src/__tests__/tabVisibility.test.ts +27 -0
  25. package/src/runtime/components/MapoFormField.vue +7 -13
  26. package/src/runtime/components/MapoFormFlatSection.vue +38 -5
  27. package/src/runtime/components/MapoFormGroup.vue +116 -43
  28. package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
  29. package/src/runtime/types/descriptor.ts +10 -2
  30. package/src/runtime/utils/tabVisibility.ts +42 -4
@@ -0,0 +1,130 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { createRenderer, h } from "vue";
3
+ import type { App, Component } from "vue";
4
+
5
+ import MapoFormGroup from "../runtime/components/MapoFormGroup.vue";
6
+
7
+ const runtimeConfig = vi.hoisted(() => ({
8
+ public: { mapoForm: { groups: { expanded: true } } },
9
+ }));
10
+
11
+ // Keys of the fields the group renders: its body renders them only while the
12
+ // group is expanded.
13
+ const renderedFields = vi.hoisted(() => [] as string[]);
14
+
15
+ vi.mock("#app", () => ({ useRuntimeConfig: () => runtimeConfig }));
16
+ vi.mock("../runtime/composables/useMapoForm.js", () => ({
17
+ injectMapoForm: () => undefined,
18
+ }));
19
+ vi.mock("../runtime/components/MapoFormField.vue", () => ({
20
+ default: {
21
+ name: "MapoFormField",
22
+ props: { descriptor: { type: Object, required: true } },
23
+ setup(props: { descriptor: { key: string } }) {
24
+ renderedFields.push(props.descriptor.key);
25
+ return () => null;
26
+ },
27
+ },
28
+ }));
29
+
30
+ // Tests run in Node without a DOM. A renderer whose nodes are plain objects is
31
+ // enough to mount the component; `style` takes what `v-show` writes, and
32
+ // `querySelector` gives the field-expand `Teleport` a target.
33
+ interface HeadlessNode {
34
+ parent: HeadlessNode | null;
35
+ children: HeadlessNode[];
36
+ style: Record<string, string>;
37
+ }
38
+
39
+ const headlessNode = (): HeadlessNode => ({
40
+ parent: null,
41
+ children: [],
42
+ style: {},
43
+ });
44
+ const body = headlessNode();
45
+
46
+ const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
47
+ createElement: headlessNode,
48
+ createText: headlessNode,
49
+ createComment: headlessNode,
50
+ setText: () => {},
51
+ setElementText: () => {},
52
+ patchProp: () => {},
53
+ insert(child, parent, anchor) {
54
+ child.parent = parent;
55
+ const at = anchor ? parent.children.indexOf(anchor) : -1;
56
+ parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
57
+ },
58
+ remove(child) {
59
+ child.parent?.children.splice(child.parent.children.indexOf(child), 1);
60
+ child.parent = null;
61
+ },
62
+ parentNode: (node) => node.parent,
63
+ nextSibling: (node) =>
64
+ node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
65
+ querySelector: () => body,
66
+ });
67
+
68
+ const UIcon: Component = { name: "UIcon", render: () => null };
69
+
70
+ let app: App<HeadlessNode> | undefined;
71
+
72
+ /** Mounts a group with one field; `initialExpanded` is passed only when given. */
73
+ function mountGroup(props: { initialExpanded?: boolean } = {}) {
74
+ const root: Component = {
75
+ render: () =>
76
+ h(MapoFormGroup as Component, {
77
+ name: "seo",
78
+ fields: [{ key: "meta_title", type: "text" }],
79
+ ...props,
80
+ }),
81
+ };
82
+ app = createApp(root);
83
+ app.component("UIcon", UIcon);
84
+ app.mount(headlessNode());
85
+ }
86
+
87
+ describe("MapoFormGroup initial state", () => {
88
+ beforeEach(() => {
89
+ renderedFields.length = 0;
90
+ // The field-expand listeners touch `document`, which Node does not have.
91
+ vi.stubGlobal("document", {
92
+ addEventListener: vi.fn(),
93
+ removeEventListener: vi.fn(),
94
+ body: { style: {} },
95
+ });
96
+ });
97
+
98
+ afterEach(() => {
99
+ app?.unmount();
100
+ app = undefined;
101
+ vi.unstubAllGlobals();
102
+ });
103
+
104
+ it("starts expanded when the prop is absent and groups.expanded is true", () => {
105
+ runtimeConfig.public.mapoForm.groups.expanded = true;
106
+ mountGroup();
107
+
108
+ expect(renderedFields).toEqual(["meta_title"]);
109
+ });
110
+
111
+ it("starts collapsed when the prop is absent and groups.expanded is false", () => {
112
+ runtimeConfig.public.mapoForm.groups.expanded = false;
113
+ mountGroup();
114
+
115
+ expect(renderedFields).toEqual([]);
116
+ });
117
+
118
+ it.each([
119
+ { initialExpanded: false, configExpanded: true, fields: [] },
120
+ { initialExpanded: true, configExpanded: false, fields: ["meta_title"] },
121
+ ])(
122
+ "an explicit initialExpanded $initialExpanded wins over groups.expanded $configExpanded",
123
+ ({ initialExpanded, configExpanded, fields }) => {
124
+ runtimeConfig.public.mapoForm.groups.expanded = configExpanded;
125
+ mountGroup({ initialExpanded });
126
+
127
+ expect(renderedFields).toEqual(fields);
128
+ },
129
+ );
130
+ });
@@ -0,0 +1,546 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { createRenderer, h, nextTick, ref } from "vue";
3
+ import type { App, Component, Ref } from "vue";
4
+
5
+ import MapoFormFlatSection from "../runtime/components/MapoFormFlatSection.vue";
6
+ import MapoFormGroup from "../runtime/components/MapoFormGroup.vue";
7
+ import MapoRepeaterItem from "../runtime/components/fields/MapoRepeaterItem.vue";
8
+ import type { AnyFieldDescriptor } from "../runtime/types/index.js";
9
+
10
+ // A field hidden by `visible` or `show` takes its grid cell with it, and a flat
11
+ // section, a group, a sub-tab or a repeater item body with no field to show goes
12
+ // as well.
13
+
14
+ const form = vi.hoisted(() => ({
15
+ context: undefined as
16
+ | { model: Ref<Record<string, unknown>>; currentLang: Ref<string> }
17
+ | undefined,
18
+ }));
19
+
20
+ // The keys of the fields mounted so far, once per mount, and of the ones
21
+ // mounted now: a field that `show` hides stays mounted.
22
+ const fieldMounts = vi.hoisted(() => [] as string[]);
23
+ const mountedFields = vi.hoisted(() => new Set<string>());
24
+
25
+ vi.mock("#app", () => ({
26
+ useRuntimeConfig: () => ({
27
+ public: { mapoForm: { groups: { expanded: true } } },
28
+ }),
29
+ }));
30
+ vi.mock("../runtime/composables/useMapoForm.js", async () => {
31
+ const { ref } = await import("vue");
32
+ return {
33
+ injectMapoForm: () => form.context,
34
+ // A repeater item builds a form of its own; its fields are stubs here.
35
+ useMapoForm: () => ({
36
+ readonly: ref(false),
37
+ ctx: { submitted: ref(false) },
38
+ }),
39
+ };
40
+ });
41
+ vi.mock("../runtime/composables/useFocusMode.js", () => ({
42
+ useFocusMode: () => ({ enter: () => {} }),
43
+ }));
44
+ vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
45
+ vi.mock("../runtime/components/MapoFormField.vue", async () => {
46
+ const { h, onUnmounted } = await import("vue");
47
+ return {
48
+ default: {
49
+ name: "MapoFormField",
50
+ props: { descriptor: { type: Object, required: true } },
51
+ setup(props: { descriptor: { key: string } }) {
52
+ const key = props.descriptor.key;
53
+ fieldMounts.push(key);
54
+ mountedFields.add(key);
55
+ onUnmounted(() => mountedFields.delete(key));
56
+ return () => h("span", { "data-field": key });
57
+ },
58
+ },
59
+ };
60
+ });
61
+
62
+ // Tests run in Node without a DOM. A renderer whose nodes are plain objects
63
+ // keeps each element's tag, props and text; `style` takes what `v-show` writes,
64
+ // and `querySelector` gives the field-expand `Teleport` a target.
65
+ interface HeadlessNode {
66
+ type: string;
67
+ text: string;
68
+ parent: HeadlessNode | null;
69
+ children: HeadlessNode[];
70
+ props: Record<string, unknown>;
71
+ style: Record<string, string>;
72
+ }
73
+
74
+ const headlessNode = (type: string, text = ""): HeadlessNode => ({
75
+ type,
76
+ text,
77
+ parent: null,
78
+ children: [],
79
+ props: {},
80
+ style: {},
81
+ });
82
+ const body = headlessNode("body");
83
+
84
+ const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
85
+ createElement: (type) => headlessNode(type),
86
+ createText: (text) => headlessNode("#text", text),
87
+ createComment: (text) => headlessNode("#comment", text),
88
+ setText: (node, text) => {
89
+ node.text = text;
90
+ },
91
+ setElementText: (node, text) => {
92
+ node.children = [];
93
+ node.text = text;
94
+ },
95
+ patchProp(node, key, _previous, next) {
96
+ node.props[key] = next;
97
+ },
98
+ insert(child, parent, anchor) {
99
+ child.parent = parent;
100
+ const at = anchor ? parent.children.indexOf(anchor) : -1;
101
+ parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
102
+ },
103
+ remove(child) {
104
+ child.parent?.children.splice(child.parent.children.indexOf(child), 1);
105
+ child.parent = null;
106
+ },
107
+ parentNode: (node) => node.parent,
108
+ nextSibling: (node) =>
109
+ node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
110
+ querySelector: () => body,
111
+ });
112
+
113
+ // Nuxt UI components the layouts use, rendered as nothing.
114
+ const nuxtUiStub: Component = { render: () => null };
115
+ const nuxtUiComponents = ["UIcon", "UBadge", "UButton", "UCheckbox", "UInput"];
116
+
117
+ const all = (node: HeadlessNode): HeadlessNode[] => [
118
+ node,
119
+ ...node.children.flatMap(all),
120
+ ];
121
+ const textOf = (node: HeadlessNode): string =>
122
+ node.text + node.children.map(textOf).join("");
123
+ const hasClass = (node: HeadlessNode, name: string) =>
124
+ String(node.props.class ?? "")
125
+ .split(" ")
126
+ .includes(name);
127
+ const isCell = (node: HeadlessNode) =>
128
+ String(node.props.class ?? "").startsWith("col-span-");
129
+
130
+ /** The key of the field a cell holds, or of the `field.<key>` slot that fills it. */
131
+ function keyInCell(cell: HeadlessNode): string | undefined {
132
+ const content = all(cell).find(
133
+ (node) => "data-field" in node.props || "data-slot" in node.props,
134
+ );
135
+ return (content?.props["data-field"] ?? content?.props["data-slot"]) as
136
+ | string
137
+ | undefined;
138
+ }
139
+
140
+ let app: App<HeadlessNode> | undefined;
141
+ let root: HeadlessNode;
142
+ let model: Ref<Record<string, unknown>>;
143
+
144
+ /** The cells of the grids, in order, by the key of what they hold. */
145
+ const cellKeys = () => all(root).filter(isCell).map(keyInCell);
146
+ const cellOf = (key: string) =>
147
+ all(root).find((node) => isCell(node) && keyInCell(node) === key);
148
+ const findByClass = (name: string) =>
149
+ all(root).find((node) => hasClass(node, name));
150
+
151
+ /** Whether a node is on screen: neither it nor an ancestor is hidden by `v-show`. */
152
+ function isDisplayed(node: HeadlessNode | undefined): boolean {
153
+ if (!node) return false;
154
+ for (let at: HeadlessNode | null = node; at; at = at.parent) {
155
+ if (at.style.display === "none") return false;
156
+ }
157
+ return true;
158
+ }
159
+
160
+ /** Mounts a component; props given as a function are read again on each render. */
161
+ function mount(
162
+ component: Component,
163
+ props: Record<string, unknown> | (() => Record<string, unknown>),
164
+ slots: Record<string, () => unknown> = {},
165
+ ) {
166
+ root = headlessNode("root");
167
+ const propsNow = typeof props === "function" ? props : () => props;
168
+ const host: Component = { render: () => h(component, propsNow(), slots) };
169
+ app = createApp(host);
170
+ for (const name of nuxtUiComponents) app.component(name, nuxtUiStub);
171
+ app.mount(root);
172
+ }
173
+
174
+ type Predicate = (ctx: { model: Record<string, unknown> }) => boolean;
175
+
176
+ // Like a page whose content fields depend on the template it uses.
177
+ const isHome: Predicate = ({ model }) => model.template === "home";
178
+ const isLanding: Predicate = ({ model }) => model.template === "landing";
179
+
180
+ const field = (
181
+ key: string,
182
+ options: { cols?: number; visible?: Predicate; show?: Predicate } = {},
183
+ ) => ({ key, type: "text", ...options }) as AnyFieldDescriptor;
184
+
185
+ beforeEach(() => {
186
+ fieldMounts.length = 0;
187
+ mountedFields.clear();
188
+ model = ref<Record<string, unknown>>({ template: "home" });
189
+ form.context = { model, currentLang: ref("") };
190
+ // The field-expand listeners touch `document`, which Node does not have.
191
+ vi.stubGlobal("document", {
192
+ addEventListener: vi.fn(),
193
+ removeEventListener: vi.fn(),
194
+ body: { style: {} },
195
+ });
196
+ });
197
+
198
+ afterEach(() => {
199
+ app?.unmount();
200
+ app = undefined;
201
+ form.context = undefined;
202
+ vi.unstubAllGlobals();
203
+ });
204
+
205
+ describe("MapoFormFlatSection cells", () => {
206
+ it("renders no cell for a field `visible` removes, so the next one takes its place", () => {
207
+ mount(MapoFormFlatSection, {
208
+ fields: [
209
+ field("cta_label", { cols: 6, visible: isLanding }),
210
+ field("hero_title", { cols: 6, visible: isHome }),
211
+ ],
212
+ });
213
+
214
+ expect(cellKeys()).toEqual(["hero_title"]);
215
+ expect([...mountedFields]).toEqual(["hero_title"]);
216
+ });
217
+
218
+ it("keeps the cell of a field `show` hides, hidden, with the field mounted", () => {
219
+ mount(MapoFormFlatSection, {
220
+ fields: [field("title"), field("cta_label", { show: isLanding })],
221
+ });
222
+
223
+ expect(cellKeys()).toEqual(["title", "cta_label"]);
224
+ expect(isDisplayed(cellOf("title"))).toBe(true);
225
+ expect(isDisplayed(cellOf("cta_label"))).toBe(false);
226
+ expect(mountedFields.has("cta_label")).toBe(true);
227
+ });
228
+
229
+ it("brings the cells back when the model changes, without mounting a hidden field again", async () => {
230
+ mount(MapoFormFlatSection, {
231
+ fields: [
232
+ field("title"),
233
+ field("cta_label", { visible: isLanding }),
234
+ field("cta_url", { show: isLanding }),
235
+ ],
236
+ });
237
+ expect(cellKeys()).toEqual(["title", "cta_url"]);
238
+
239
+ model.value.template = "landing";
240
+ await nextTick();
241
+
242
+ expect(cellKeys()).toEqual(["title", "cta_label", "cta_url"]);
243
+ expect(isDisplayed(cellOf("cta_label"))).toBe(true);
244
+ expect(isDisplayed(cellOf("cta_url"))).toBe(true);
245
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
246
+
247
+ model.value.template = "home";
248
+ await nextTick();
249
+
250
+ expect(cellKeys()).toEqual(["title", "cta_url"]);
251
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
252
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
253
+ });
254
+
255
+ it("applies the field's predicates to the cell its `field.<key>` slot fills", async () => {
256
+ mount(
257
+ MapoFormFlatSection,
258
+ {
259
+ fields: [
260
+ field("title"),
261
+ field("cta_label", { visible: isLanding }),
262
+ field("cta_url", { show: isLanding }),
263
+ ],
264
+ },
265
+ {
266
+ "field.cta_label": () => h("em", { "data-slot": "cta_label" }),
267
+ "field.cta_url": () => h("em", { "data-slot": "cta_url" }),
268
+ },
269
+ );
270
+
271
+ expect(cellKeys()).toEqual(["title", "cta_url"]);
272
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
273
+
274
+ model.value.template = "landing";
275
+ await nextTick();
276
+
277
+ expect(cellKeys()).toEqual(["title", "cta_label", "cta_url"]);
278
+ expect(isDisplayed(cellOf("cta_url"))).toBe(true);
279
+ });
280
+
281
+ it("keeps the other cells when a field's predicate throws", () => {
282
+ // Reads a nested value the model does not have yet, as on a new record.
283
+ const seoEnabled: Predicate = ({ model }) =>
284
+ (model.seo as { enabled: boolean }).enabled;
285
+ mount(MapoFormFlatSection, {
286
+ fields: [field("title"), field("seo_title", { visible: seoEnabled })],
287
+ });
288
+
289
+ expect(cellKeys()).toEqual(["title"]);
290
+ });
291
+
292
+ it("renders no grid while `visible` removes every field", () => {
293
+ mount(MapoFormFlatSection, {
294
+ fields: [field("cta_label", { visible: isLanding })],
295
+ });
296
+
297
+ expect(findByClass("grid")).toBeUndefined();
298
+ });
299
+
300
+ it("hides the grid while `show` hides every field, and shows it with one of them", async () => {
301
+ mount(MapoFormFlatSection, {
302
+ fields: [
303
+ field("cta_label", { visible: isLanding }),
304
+ field("cta_url", { show: isLanding }),
305
+ ],
306
+ });
307
+ expect(isDisplayed(findByClass("grid"))).toBe(false);
308
+ expect(mountedFields.has("cta_url")).toBe(true);
309
+
310
+ model.value.template = "landing";
311
+ await nextTick();
312
+
313
+ expect(isDisplayed(findByClass("grid"))).toBe(true);
314
+ });
315
+ });
316
+
317
+ describe("MapoFormGroup cells", () => {
318
+ it("renders the cells of the fields to show and hides the cell of one `show` hides", () => {
319
+ mount(MapoFormGroup, {
320
+ name: "content",
321
+ fields: [
322
+ field("cta_label", { cols: 6, visible: isLanding }),
323
+ field("hero_title", { cols: 6, visible: isHome }),
324
+ field("cta_url", { show: isLanding }),
325
+ ],
326
+ });
327
+
328
+ expect(cellKeys()).toEqual(["hero_title", "cta_url"]);
329
+ expect(isDisplayed(cellOf("hero_title"))).toBe(true);
330
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
331
+ });
332
+
333
+ it("brings a cell back when the model changes", async () => {
334
+ mount(MapoFormGroup, {
335
+ name: "content",
336
+ fields: [field("hero_title"), field("cta_label", { visible: isLanding })],
337
+ });
338
+ expect(cellKeys()).toEqual(["hero_title"]);
339
+
340
+ model.value.template = "landing";
341
+ await nextTick();
342
+
343
+ expect(cellKeys()).toEqual(["hero_title", "cta_label"]);
344
+ });
345
+
346
+ it("applies the field's predicates to the cells its `field.<key>` slots fill, sub-tabs included", async () => {
347
+ mount(
348
+ MapoFormGroup,
349
+ {
350
+ name: "content",
351
+ fields: [field("title"), field("cta_label", { visible: isLanding })],
352
+ subtabs: new Map([
353
+ ["social", [field("og_title", { show: isLanding })]],
354
+ ["basic", [field("meta_title")]],
355
+ ]),
356
+ },
357
+ {
358
+ "field.cta_label": () => h("em", { "data-slot": "cta_label" }),
359
+ "field.og_title": () => h("em", { "data-slot": "og_title" }),
360
+ },
361
+ );
362
+ expect(cellKeys()).toEqual(["title", "og_title", "meta_title"]);
363
+ expect(isDisplayed(cellOf("og_title"))).toBe(false);
364
+
365
+ model.value.template = "landing";
366
+ await nextTick();
367
+
368
+ expect(cellKeys()).toEqual([
369
+ "title",
370
+ "cta_label",
371
+ "og_title",
372
+ "meta_title",
373
+ ]);
374
+ expect(isDisplayed(cellOf("cta_label"))).toBe(true);
375
+ expect(isDisplayed(cellOf("og_title"))).toBe(true);
376
+ });
377
+
378
+ it("renders no card while `visible` removes every field, and renders it once one is kept", async () => {
379
+ mount(MapoFormGroup, {
380
+ name: "call_to_action",
381
+ fields: [
382
+ field("cta_label", { visible: isLanding }),
383
+ field("cta_url", { visible: isLanding }),
384
+ ],
385
+ });
386
+ expect(findByClass("mapo-form-group")).toBeUndefined();
387
+
388
+ model.value.template = "landing";
389
+ await nextTick();
390
+
391
+ expect(isDisplayed(findByClass("mapo-form-group"))).toBe(true);
392
+ expect(cellKeys()).toEqual(["cta_label", "cta_url"]);
393
+ });
394
+
395
+ it("hides the card while `show` hides every field, keeping the fields mounted", async () => {
396
+ mount(MapoFormGroup, {
397
+ name: "call_to_action",
398
+ fields: [
399
+ field("cta_label", { visible: isLanding }),
400
+ field("cta_url", { show: isLanding }),
401
+ ],
402
+ });
403
+ expect(isDisplayed(findByClass("mapo-form-group"))).toBe(false);
404
+ expect(mountedFields.has("cta_url")).toBe(true);
405
+
406
+ model.value.template = "landing";
407
+ await nextTick();
408
+
409
+ expect(isDisplayed(findByClass("mapo-form-group"))).toBe(true);
410
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
411
+ });
412
+ });
413
+
414
+ describe("MapoFormGroup sub-tabs", () => {
415
+ const seoSubtabs = () =>
416
+ new Map([
417
+ ["basic", [field("meta_title")]],
418
+ ["social", [field("og_title", { visible: isLanding })]],
419
+ ["advanced", [field("canonical", { show: isLanding })]],
420
+ ]);
421
+
422
+ const subtabButtons = () =>
423
+ findByClass("mapo-form-subtab-bar")?.children.filter(
424
+ (node) => node.type === "button",
425
+ ) ?? [];
426
+ const subtabLabels = () =>
427
+ subtabButtons().map((button) => textOf(button).trim());
428
+
429
+ async function clickSubtab(label: string) {
430
+ const button = subtabButtons().find(
431
+ (node) => textOf(node).trim() === label,
432
+ );
433
+ (button?.props.onClick as () => void)();
434
+ await nextTick();
435
+ }
436
+
437
+ it("leaves a sub-tab with no field to show out of the bar", () => {
438
+ mount(MapoFormGroup, { name: "seo", fields: [], subtabs: seoSubtabs() });
439
+
440
+ expect(subtabLabels()).toEqual(["basic"]);
441
+ // `visible` removed og_title with its panel; `show` keeps canonical mounted.
442
+ expect(cellOf("og_title")).toBeUndefined();
443
+ expect(isDisplayed(cellOf("canonical"))).toBe(false);
444
+ expect(mountedFields.has("canonical")).toBe(true);
445
+ });
446
+
447
+ it("opens the first shown sub-tab while the picked one is hidden, and the picked one again when it shows", async () => {
448
+ model.value.template = "landing";
449
+ mount(MapoFormGroup, { name: "seo", fields: [], subtabs: seoSubtabs() });
450
+ expect(subtabLabels()).toEqual(["basic", "social", "advanced"]);
451
+
452
+ await clickSubtab("advanced");
453
+ expect(isDisplayed(cellOf("canonical"))).toBe(true);
454
+ expect(isDisplayed(cellOf("meta_title"))).toBe(false);
455
+
456
+ model.value.template = "home";
457
+ await nextTick();
458
+
459
+ expect(subtabLabels()).toEqual(["basic"]);
460
+ expect(isDisplayed(cellOf("meta_title"))).toBe(true);
461
+
462
+ model.value.template = "landing";
463
+ await nextTick();
464
+
465
+ expect(isDisplayed(cellOf("canonical"))).toBe(true);
466
+ expect(isDisplayed(cellOf("meta_title"))).toBe(false);
467
+ });
468
+
469
+ it("renders no card while every sub-tab field is removed", () => {
470
+ mount(MapoFormGroup, {
471
+ name: "seo",
472
+ fields: [],
473
+ subtabs: new Map([
474
+ ["social", [field("og_title", { visible: isLanding })]],
475
+ ]),
476
+ });
477
+
478
+ expect(findByClass("mapo-form-group")).toBeUndefined();
479
+ });
480
+ });
481
+
482
+ describe("MapoRepeaterItem cells", () => {
483
+ // The props a repeater gives each of its items.
484
+ const itemProps = (
485
+ item: Record<string, unknown>,
486
+ fields: AnyFieldDescriptor[],
487
+ ) => ({
488
+ item,
489
+ fields,
490
+ index: 0,
491
+ errorPrefix: "blocks.0",
492
+ parentErrors: {},
493
+ languages: [],
494
+ currentLang: "",
495
+ readonly: false,
496
+ registry: {},
497
+ defaultExpanded: true,
498
+ });
499
+
500
+ beforeEach(() => {
501
+ // An item's predicates read the item's own model, not the root form's.
502
+ form.context = undefined;
503
+ });
504
+
505
+ it("lays out the item's cells like a form section, following the item's model", async () => {
506
+ const item = ref<Record<string, unknown>>({ template: "home" });
507
+ const fields = [
508
+ field("title"),
509
+ field("cta_label", { cols: 6, visible: isLanding }),
510
+ field("hero_title", { cols: 6, visible: isHome }),
511
+ field("cta_url", { show: isLanding }),
512
+ ];
513
+ mount(MapoRepeaterItem, () => itemProps(item.value, fields));
514
+ expect(cellKeys()).toEqual(["title", "hero_title", "cta_url"]);
515
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
516
+ expect(mountedFields.has("cta_url")).toBe(true);
517
+
518
+ item.value = { template: "landing" };
519
+ await nextTick();
520
+
521
+ expect(cellKeys()).toEqual(["title", "cta_label", "cta_url"]);
522
+ expect(isDisplayed(cellOf("cta_url"))).toBe(true);
523
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
524
+ });
525
+
526
+ it("renders no body while `visible` removes every field", () => {
527
+ mount(
528
+ MapoRepeaterItem,
529
+ itemProps({ template: "home" }, [
530
+ field("cta_label", { visible: isLanding }),
531
+ ]),
532
+ );
533
+
534
+ expect(findByClass("grid")).toBeUndefined();
535
+ });
536
+
537
+ it("hides the body while `show` hides every field, keeping the fields mounted", () => {
538
+ mount(
539
+ MapoRepeaterItem,
540
+ itemProps({ template: "home" }, [field("cta_url", { show: isLanding })]),
541
+ );
542
+
543
+ expect(isDisplayed(findByClass("grid"))).toBe(false);
544
+ expect(mountedFields.has("cta_url")).toBe(true);
545
+ });
546
+ });