|
22 | 22 | * lone <form> — can't silently regress. |
23 | 23 | */ |
24 | 24 |
|
25 | | -import { describe, it, expect, vi } from 'vitest'; |
26 | | -import { render, waitFor } from '@testing-library/react'; |
| 25 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 26 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
27 | 27 | import React from 'react'; |
28 | 28 | import { registerAllFields } from '@object-ui/fields'; |
29 | 29 | import { ModalForm } from './ModalForm'; |
| 30 | +import { TabbedForm } from './TabbedForm'; |
| 31 | +import { SplitForm } from './SplitForm'; |
30 | 32 | import { sectionFormLayout } from './autoLayout'; |
31 | 33 |
|
32 | 34 | registerAllFields(); |
@@ -67,6 +69,88 @@ const makeDataSource = (): any => ({ |
67 | 69 | findOne: vi.fn(), |
68 | 70 | }); |
69 | 71 |
|
| 72 | +afterEach(() => { |
| 73 | + cleanup(); |
| 74 | +}); |
| 75 | + |
| 76 | +/** |
| 77 | + * The form view's OWN `columns` is a spec key (view.zod FormView.columns), so a |
| 78 | + * sectioned host must honour it — and it OUTRANKS the per-section densities, |
| 79 | + * which describe how a section fills the grid rather than how wide the grid is. |
| 80 | + * |
| 81 | + * `ObjectForm` (simple path) and `ModalForm` already resolved it as |
| 82 | + * `explicit form columns ?? widest section ?? 1`. `TabbedForm` and `SplitForm` |
| 83 | + * read only the sections, so a view that declared `columns: 3` silently |
| 84 | + * rendered single-column in a tabbed or split form while the same metadata gave |
| 85 | + * 3 columns in a modal. |
| 86 | + */ |
| 87 | +describe('view-level `columns` outranks per-section columns', () => { |
| 88 | + const SECTIONS = [ |
| 89 | + { name: 's1', label: 'One', fields: ['a', 'b'] }, |
| 90 | + { name: 's2', label: 'Two', fields: ['c', 'd'] }, |
| 91 | + ]; |
| 92 | + |
| 93 | + it('TabbedForm honours the form view’s columns', async () => { |
| 94 | + render( |
| 95 | + <TabbedForm |
| 96 | + schema={{ |
| 97 | + type: 'object-form', |
| 98 | + formType: 'tabbed', |
| 99 | + objectName: 'lead', |
| 100 | + mode: 'create', |
| 101 | + columns: 3, |
| 102 | + sections: SECTIONS, |
| 103 | + }} |
| 104 | + dataSource={makeDataSource()} |
| 105 | + />, |
| 106 | + ); |
| 107 | + |
| 108 | + await waitFor(() => expect(document.body.querySelector('form')).toBeTruthy()); |
| 109 | + expect(document.body.querySelector('[class*="@2xl:grid-cols-3"]')).not.toBeNull(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('SplitForm honours the form view’s columns', async () => { |
| 113 | + render( |
| 114 | + <SplitForm |
| 115 | + schema={{ |
| 116 | + type: 'object-form', |
| 117 | + formType: 'split', |
| 118 | + objectName: 'lead', |
| 119 | + mode: 'create', |
| 120 | + columns: 3, |
| 121 | + sections: SECTIONS, |
| 122 | + }} |
| 123 | + dataSource={makeDataSource()} |
| 124 | + />, |
| 125 | + ); |
| 126 | + |
| 127 | + await waitFor(() => expect(document.body.querySelector('form')).toBeTruthy()); |
| 128 | + expect(document.body.querySelector('[class*="@2xl:grid-cols-3"]')).not.toBeNull(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('falls back to the widest section when the view declares no columns', async () => { |
| 132 | + render( |
| 133 | + <SplitForm |
| 134 | + schema={{ |
| 135 | + type: 'object-form', |
| 136 | + formType: 'split', |
| 137 | + objectName: 'lead', |
| 138 | + mode: 'create', |
| 139 | + sections: [ |
| 140 | + { name: 's1', label: 'One', columns: 2, fields: ['a', 'b'] }, |
| 141 | + { name: 's2', label: 'Two', fields: ['c', 'd'] }, |
| 142 | + ], |
| 143 | + }} |
| 144 | + dataSource={makeDataSource()} |
| 145 | + />, |
| 146 | + ); |
| 147 | + |
| 148 | + await waitFor(() => expect(document.body.querySelector('form')).toBeTruthy()); |
| 149 | + expect(document.body.querySelector('[class*="@md:grid-cols-2"]')).not.toBeNull(); |
| 150 | + expect(document.body.querySelector('[class*="@2xl:grid-cols-3"]')).toBeNull(); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
70 | 154 | describe('ModalForm — grouped section multi-column layout (#2128)', () => { |
71 | 155 | it('renders a 2-column section grid whose direct children are the fields, not a lone <form>', async () => { |
72 | 156 | render( |
|
0 commit comments