|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * FormView spec conformance round-trip (#2545). |
| 11 | + * |
| 12 | + * The bridge must never silently drop `@objectstack/spec` FormViewSchema |
| 13 | + * configuration: every serializable spec key is either mapped onto the |
| 14 | + * `object-form` node or explicitly listed in IGNORED_SPEC_KEYS with a reason. |
| 15 | + * The fixture below carries every top-level FormViewSchema key (spec 14.6.0 / |
| 16 | + * 15.0.0 — identical key sets), so a newly-added spec key that the bridge |
| 17 | + * ignores will fail the completeness assertion when the fixture is updated. |
| 18 | + */ |
| 19 | +import { describe, it, expect } from 'vitest'; |
| 20 | +import { SpecBridge } from '../SpecBridge'; |
| 21 | + |
| 22 | +/** Spec keys intentionally NOT copied onto the node, with reasons. */ |
| 23 | +const IGNORED_SPEC_KEYS: Record<string, string> = { |
| 24 | + type: 'mapped to node.formType (ObjectUI rename), not carried verbatim', |
| 25 | + groups: 'legacy alias of sections — normalized into node.sections', |
| 26 | +}; |
| 27 | + |
| 28 | +/** Every top-level serializable key of spec FormViewSchema (14.6.0 / 15.0.0). */ |
| 29 | +const FULL_SPEC_FORM_VIEW = { |
| 30 | + type: 'wizard', |
| 31 | + layout: 'grid', |
| 32 | + columns: 2, |
| 33 | + title: 'Edit Opportunity', |
| 34 | + description: 'All the fields', |
| 35 | + defaultTab: 'details', |
| 36 | + tabPosition: 'left', |
| 37 | + allowSkip: true, |
| 38 | + showStepIndicator: false, |
| 39 | + splitDirection: 'horizontal', |
| 40 | + splitSize: 40, |
| 41 | + splitResizable: true, |
| 42 | + drawerSide: 'right', |
| 43 | + drawerWidth: '480px', |
| 44 | + modalSize: 'lg', |
| 45 | + data: { provider: 'object', object: 'opportunity' }, |
| 46 | + sections: [ |
| 47 | + { |
| 48 | + name: 'basic_info', |
| 49 | + label: 'Basic Info', |
| 50 | + description: 'Who and what', |
| 51 | + collapsible: true, |
| 52 | + collapsed: false, |
| 53 | + columns: 2, |
| 54 | + visibleWhen: 'record.stage != "closed"', |
| 55 | + fields: [ |
| 56 | + { |
| 57 | + field: 'name', |
| 58 | + type: 'text', |
| 59 | + label: 'Name', |
| 60 | + required: true, |
| 61 | + placeholder: 'Acme deal', |
| 62 | + helpText: 'Deal name', |
| 63 | + colSpan: 2, |
| 64 | + widget: 'input', |
| 65 | + dependsOn: ['account'], |
| 66 | + visibleWhen: 'record.active == true', |
| 67 | + }, |
| 68 | + { |
| 69 | + field: 'account', |
| 70 | + type: 'lookup', |
| 71 | + reference: 'account', |
| 72 | + options: [{ label: 'A', value: 'a' }], |
| 73 | + readonly: true, |
| 74 | + hidden: false, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ], |
| 79 | + subforms: [{ childObject: 'opportunity_line_item', amountField: 'amount' }], |
| 80 | + defaultSort: [{ field: 'name', order: 'asc' }], |
| 81 | + sharing: { visibility: 'team' }, |
| 82 | + submitBehavior: { kind: 'redirect', url: '/done' }, |
| 83 | + aria: { ariaLabel: 'Opportunity form', role: 'form' }, |
| 84 | +}; |
| 85 | + |
| 86 | +describe('FormView spec conformance (#2545)', () => { |
| 87 | + it('carries every spec FormViewSchema key onto the node (no silent drops)', () => { |
| 88 | + const bridge = new SpecBridge(); |
| 89 | + const node = bridge.transformFormView(FULL_SPEC_FORM_VIEW); |
| 90 | + |
| 91 | + for (const key of Object.keys(FULL_SPEC_FORM_VIEW)) { |
| 92 | + if (key in IGNORED_SPEC_KEYS) continue; |
| 93 | + expect(node[key], `spec key "${key}" was silently dropped by the bridge`).toBeDefined(); |
| 94 | + } |
| 95 | + // The two intentionally-diverted keys land in their mapped slots. |
| 96 | + expect(node.formType).toBe('wizard'); |
| 97 | + expect(node.sections).toHaveLength(1); |
| 98 | + }); |
| 99 | + |
| 100 | + it('passes shared layout/variant keys through verbatim', () => { |
| 101 | + const bridge = new SpecBridge(); |
| 102 | + const node = bridge.transformFormView(FULL_SPEC_FORM_VIEW); |
| 103 | + |
| 104 | + expect(node.layout).toBe('grid'); |
| 105 | + expect(node.columns).toBe(2); |
| 106 | + expect(node.title).toBe('Edit Opportunity'); |
| 107 | + expect(node.description).toBe('All the fields'); |
| 108 | + expect(node.defaultTab).toBe('details'); |
| 109 | + expect(node.tabPosition).toBe('left'); |
| 110 | + expect(node.allowSkip).toBe(true); |
| 111 | + expect(node.showStepIndicator).toBe(false); |
| 112 | + expect(node.splitDirection).toBe('horizontal'); |
| 113 | + expect(node.splitSize).toBe(40); |
| 114 | + expect(node.splitResizable).toBe(true); |
| 115 | + expect(node.drawerSide).toBe('right'); |
| 116 | + expect(node.drawerWidth).toBe('480px'); |
| 117 | + expect(node.modalSize).toBe('lg'); |
| 118 | + expect(node.subforms).toEqual(FULL_SPEC_FORM_VIEW.subforms); |
| 119 | + expect(node.submitBehavior).toEqual({ kind: 'redirect', url: '/done' }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('preserves spec FormSection name/description/visibleWhen', () => { |
| 123 | + const bridge = new SpecBridge(); |
| 124 | + const node = bridge.transformFormView(FULL_SPEC_FORM_VIEW); |
| 125 | + const section = (node.sections as any[])[0]; |
| 126 | + |
| 127 | + expect(section.name).toBe('basic_info'); |
| 128 | + expect(section.description).toBe('Who and what'); |
| 129 | + expect(section.visibleWhen).toBe('record.stage != "closed"'); |
| 130 | + expect(section.label).toBe('Basic Info'); |
| 131 | + expect(section.columns).toBe(2); |
| 132 | + }); |
| 133 | + |
| 134 | + it('preserves spec FormField type/options/reference', () => { |
| 135 | + const bridge = new SpecBridge(); |
| 136 | + const node = bridge.transformFormView(FULL_SPEC_FORM_VIEW); |
| 137 | + const [name, account] = (node.sections as any[])[0].fields; |
| 138 | + |
| 139 | + expect(name.type).toBe('text'); |
| 140 | + // field-level visibleWhen lands in the renderer's visibleOn slot (ADR-0089) |
| 141 | + expect(name.visibleOn).toBe('record.active == true'); |
| 142 | + expect(account.type).toBe('lookup'); |
| 143 | + expect(account.reference).toBe('account'); |
| 144 | + expect(account.options).toEqual([{ label: 'A', value: 'a' }]); |
| 145 | + }); |
| 146 | + |
| 147 | + it('normalizes legacy groups into sections (groups-only spec now renders)', () => { |
| 148 | + const bridge = new SpecBridge(); |
| 149 | + const node = bridge.transformFormView({ |
| 150 | + type: 'simple', |
| 151 | + groups: [ |
| 152 | + { label: 'Legacy Group', fields: [{ field: 'name' }] }, |
| 153 | + ], |
| 154 | + }); |
| 155 | + |
| 156 | + expect(node.groups).toBeUndefined(); |
| 157 | + const sections = node.sections as any[]; |
| 158 | + expect(sections).toHaveLength(1); |
| 159 | + expect(sections[0].label).toBe('Legacy Group'); |
| 160 | + expect(sections[0].fields[0].name).toBe('name'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('prefers sections over groups when both are present', () => { |
| 164 | + const bridge = new SpecBridge(); |
| 165 | + const node = bridge.transformFormView({ |
| 166 | + sections: [{ label: 'Canonical', fields: [] }], |
| 167 | + groups: [{ label: 'Legacy', fields: [] }], |
| 168 | + }); |
| 169 | + |
| 170 | + const sections = node.sections as any[]; |
| 171 | + expect(sections).toHaveLength(1); |
| 172 | + expect(sections[0].label).toBe('Canonical'); |
| 173 | + }); |
| 174 | +}); |
0 commit comments