|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack-ai/objectstack#26843 — the form page draws a form view's |
| 5 | + * `type: 'tabbed'` and `layout: 'horizontal'`, which it never read. |
| 6 | + * |
| 7 | + * - `type: 'tabbed'`: one tab per section, the form view's `defaultTab` open |
| 8 | + * first and `tabPosition` placing the strip — the semantics of the app's |
| 9 | + * tabbed form. A submit carries every section's fields, the closed tabs' |
| 10 | + * included, and a refused submit opens the tab holding the field it names. |
| 11 | + * - `layout: 'horizontal'`: each row draws its label beside the control from |
| 12 | + * `sm` up, the arrangement the form renderer gives the same key. |
| 13 | + * |
| 14 | + * CONTROLS: a `type: 'simple'` view, and a tabbed view with one section, draw |
| 15 | + * stacked sections and no tab strip; no `layout` draws no grid row. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 19 | +import { render, screen, waitFor, cleanup, within } from '@testing-library/react'; |
| 20 | +import userEvent from '@testing-library/user-event'; |
| 21 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 22 | +import { createI18n } from '@object-ui/i18n'; |
| 23 | +import { FormPage, resolveActiveTab, sectionTabKey } from './FormPage'; |
| 24 | + |
| 25 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 26 | + |
| 27 | +const OBJECT_SCHEMA = { |
| 28 | + name: 'showcase_ticket', |
| 29 | + label: 'Ticket', |
| 30 | + fields: { |
| 31 | + title: { type: 'text', label: 'Title' }, |
| 32 | + notes: { type: 'text', label: 'Notes' }, |
| 33 | + channel: { type: 'text', label: 'Channel' }, |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +const THREE_SECTIONS = [ |
| 38 | + { name: 'main', label: 'Main', fields: ['title'] }, |
| 39 | + { name: 'more', label: 'More', fields: ['notes'] }, |
| 40 | + { name: 'routing', label: 'Routing', fields: ['channel'] }, |
| 41 | +]; |
| 42 | + |
| 43 | +let submits: Array<{ url: string; body: Record<string, unknown> }> = []; |
| 44 | + |
| 45 | +function stubFetch(form: Record<string, unknown>, objectSchema: unknown = OBJECT_SCHEMA) { |
| 46 | + return vi.fn(async (url: string, init?: RequestInit) => { |
| 47 | + const path = String(url); |
| 48 | + if (init?.method === 'POST') { |
| 49 | + submits.push({ url: path, body: JSON.parse(String(init.body)) }); |
| 50 | + } |
| 51 | + const body = path.includes('/meta/view/') |
| 52 | + ? { |
| 53 | + name: 'showcase_ticket.intake', |
| 54 | + object: 'showcase_ticket', |
| 55 | + viewKind: 'form', |
| 56 | + label: 'Intake', |
| 57 | + config: { type: 'tabbed', sections: THREE_SECTIONS, ...form }, |
| 58 | + } |
| 59 | + : path.includes('/meta/object/') |
| 60 | + ? objectSchema |
| 61 | + : path.includes('/data/showcase_ticket') |
| 62 | + ? { object: 'showcase_ticket', id: 't1', record: { id: 't1' } } |
| 63 | + : null; |
| 64 | + if (!body) throw new Error(`unstubbed fetch: ${path}`); |
| 65 | + return { |
| 66 | + ok: true, |
| 67 | + status: 200, |
| 68 | + statusText: 'OK', |
| 69 | + json: async () => body, |
| 70 | + text: async () => JSON.stringify(body), |
| 71 | + } as unknown as Response; |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +function renderPage() { |
| 76 | + return render( |
| 77 | + <MemoryRouter initialEntries={['/forms/showcase_ticket.intake']}> |
| 78 | + <Routes> |
| 79 | + <Route path="/forms/:name" element={<FormPage mode="internal" />} /> |
| 80 | + </Routes> |
| 81 | + </MemoryRouter>, |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +const input = (name: string) => document.querySelector(`[name="${name}"]`) as HTMLInputElement | null; |
| 86 | +const tab = (key: string) => screen.queryByTestId(`form-tab:${key}`); |
| 87 | +const panel = (key: string) => screen.queryByTestId(`form-tab-panel:${key}`); |
| 88 | +const isOpen = (key: string) => tab(key)?.getAttribute('data-state') === 'active'; |
| 89 | + |
| 90 | +beforeEach(() => { |
| 91 | + submits = []; |
| 92 | + createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 93 | +}); |
| 94 | + |
| 95 | +afterEach(() => { |
| 96 | + cleanup(); |
| 97 | + vi.unstubAllGlobals(); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('FormPage draws a tabbed form view as tabs (#26843)', () => { |
| 101 | + it('one tab per section, named by its heading, every panel mounted', async () => { |
| 102 | + vi.stubGlobal('fetch', stubFetch({})); |
| 103 | + renderPage(); |
| 104 | + await waitFor(() => expect(tab('main')).not.toBeNull()); |
| 105 | + const strip = screen.getByRole('tablist'); |
| 106 | + expect(within(strip).getAllByRole('tab').map((t) => t.textContent)).toEqual(['Main', 'More', 'Routing']); |
| 107 | + // No stacked section card beside the strip. |
| 108 | + expect(document.querySelector('form section')).toBeNull(); |
| 109 | + // Force-mounted: a closed tab's control exists, only CSS-hidden. (The |
| 110 | + // widgets are lazy, so they arrive after the strip.) |
| 111 | + await waitFor(() => { |
| 112 | + for (const name of ['title', 'notes', 'channel']) expect(input(name), name).not.toBeNull(); |
| 113 | + }); |
| 114 | + expect(panel('more')?.getAttribute('data-state')).toBe('inactive'); |
| 115 | + expect(panel('more')?.className).toContain('data-[state=inactive]:hidden'); |
| 116 | + // With no `defaultTab`, the first tab opens. |
| 117 | + expect(isOpen('main')).toBe(true); |
| 118 | + }); |
| 119 | + |
| 120 | + it('the form view\'s defaultTab opens first', async () => { |
| 121 | + vi.stubGlobal('fetch', stubFetch({ defaultTab: 'routing' })); |
| 122 | + renderPage(); |
| 123 | + await waitFor(() => expect(tab('routing')).not.toBeNull()); |
| 124 | + expect(isOpen('routing')).toBe(true); |
| 125 | + expect(isOpen('main')).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it('a tab click switches the open panel', async () => { |
| 129 | + vi.stubGlobal('fetch', stubFetch({})); |
| 130 | + renderPage(); |
| 131 | + await waitFor(() => expect(tab('more')).not.toBeNull()); |
| 132 | + await userEvent.click(tab('more')!); |
| 133 | + expect(isOpen('more')).toBe(true); |
| 134 | + expect(panel('main')?.getAttribute('data-state')).toBe('inactive'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('tabPosition: left draws a vertical strip', async () => { |
| 138 | + vi.stubGlobal('fetch', stubFetch({ tabPosition: 'left' })); |
| 139 | + renderPage(); |
| 140 | + await waitFor(() => expect(tab('main')).not.toBeNull()); |
| 141 | + expect(screen.getByRole('tablist').getAttribute('aria-orientation')).toBe('vertical'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('a submit carries every section\'s fields, the closed tabs\' included', async () => { |
| 145 | + vi.stubGlobal('fetch', stubFetch({ defaults: { channel: 'web' } })); |
| 146 | + renderPage(); |
| 147 | + await waitFor(() => expect(input('title')).not.toBeNull()); |
| 148 | + await userEvent.type(input('title')!, 'Printer jam'); |
| 149 | + await userEvent.click(tab('more')!); |
| 150 | + await userEvent.type(input('notes')!, 'Third floor'); |
| 151 | + // `routing` is never opened: its field carries the form view's default. |
| 152 | + await userEvent.click(screen.getByRole('button', { name: 'Submit' })); |
| 153 | + await waitFor(() => expect(submits).toHaveLength(1)); |
| 154 | + expect(submits[0].body).toMatchObject({ title: 'Printer jam', notes: 'Third floor', channel: 'web' }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('a refused submit opens the tab holding the field it names', async () => { |
| 158 | + const schema = { |
| 159 | + ...OBJECT_SCHEMA, |
| 160 | + fields: { ...OBJECT_SCHEMA.fields, channel: { type: 'text', label: 'Channel', required: true } }, |
| 161 | + }; |
| 162 | + vi.stubGlobal('fetch', stubFetch({}, schema)); |
| 163 | + renderPage(); |
| 164 | + await waitFor(() => expect(input('title')).not.toBeNull()); |
| 165 | + expect(isOpen('main')).toBe(true); |
| 166 | + await userEvent.click(screen.getByRole('button', { name: 'Submit' })); |
| 167 | + await waitFor(() => expect(isOpen('routing')).toBe(true)); |
| 168 | + expect(submits).toHaveLength(0); |
| 169 | + }); |
| 170 | + |
| 171 | + it('CONTROL: a single-section tabbed view draws its section stacked, no strip', async () => { |
| 172 | + vi.stubGlobal('fetch', stubFetch({ sections: [THREE_SECTIONS[0]] })); |
| 173 | + renderPage(); |
| 174 | + await waitFor(() => expect(input('title')).not.toBeNull()); |
| 175 | + expect(screen.queryByRole('tablist')).toBeNull(); |
| 176 | + expect(document.querySelectorAll('form section')).toHaveLength(1); |
| 177 | + }); |
| 178 | + |
| 179 | + it('CONTROL: a simple view draws every section stacked, no strip', async () => { |
| 180 | + vi.stubGlobal('fetch', stubFetch({ type: 'simple', defaultTab: 'routing' })); |
| 181 | + renderPage(); |
| 182 | + await waitFor(() => expect(input('title')).not.toBeNull()); |
| 183 | + expect(screen.queryByRole('tablist')).toBeNull(); |
| 184 | + expect([...document.querySelectorAll('form section h2')].map((h) => h.textContent)).toEqual([ |
| 185 | + 'Main', |
| 186 | + 'More', |
| 187 | + 'Routing', |
| 188 | + ]); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe('FormPage draws layout: horizontal rows (#26843)', () => { |
| 193 | + const LABELS: Record<string, string> = { title: 'Title', notes: 'Notes', channel: 'Channel' }; |
| 194 | + /** The row a field's `<label>` sits in. */ |
| 195 | + const row = (name: string) => screen.getByText(LABELS[name]).closest('label')!.parentElement!; |
| 196 | + |
| 197 | + it('each row puts its label beside the control at sm, stacked below', async () => { |
| 198 | + vi.stubGlobal('fetch', stubFetch({ type: 'simple', layout: 'horizontal' })); |
| 199 | + renderPage(); |
| 200 | + await waitFor(() => expect(input('title')).not.toBeNull()); |
| 201 | + const classes = row('title').className.split(/\s+/); |
| 202 | + expect(classes).toContain('sm:grid'); |
| 203 | + expect(classes).toContain('sm:[&>:not(label)]:col-start-2'); |
| 204 | + // Below sm nothing changes: every class the row carries is sm:-prefixed. |
| 205 | + expect(classes.filter((c) => !c.startsWith('sm:'))).toEqual([]); |
| 206 | + expect(row('title').firstElementChild?.tagName).toBe('LABEL'); |
| 207 | + // The label still names its control. |
| 208 | + expect(screen.getByLabelText('Title')).toBe(input('title')); |
| 209 | + }); |
| 210 | + |
| 211 | + it('a tabbed view keeps it on every tab', async () => { |
| 212 | + vi.stubGlobal('fetch', stubFetch({ layout: 'horizontal' })); |
| 213 | + renderPage(); |
| 214 | + await waitFor(() => expect(input('notes')).not.toBeNull()); |
| 215 | + for (const name of ['title', 'notes', 'channel']) { |
| 216 | + expect(row(name).className.split(/\s+/), name).toContain('sm:grid'); |
| 217 | + } |
| 218 | + }); |
| 219 | + |
| 220 | + it('CONTROL: no layout, and vertical — no grid row', async () => { |
| 221 | + for (const layout of [undefined, 'vertical']) { |
| 222 | + vi.stubGlobal('fetch', stubFetch({ type: 'simple', ...(layout ? { layout } : {}) })); |
| 223 | + renderPage(); |
| 224 | + await waitFor(() => expect(input('title')).not.toBeNull()); |
| 225 | + expect(row('title').className.split(/\s+/), String(layout)).not.toContain('sm:grid'); |
| 226 | + cleanup(); |
| 227 | + } |
| 228 | + }); |
| 229 | +}); |
| 230 | + |
| 231 | +describe('the tab rules (#26843)', () => { |
| 232 | + it('a section\'s tab is its name, else its heading, else its position — TabbedForm\'s rule', () => { |
| 233 | + expect(sectionTabKey({ name: 'main', label: 'Main' }, 0)).toBe('main'); |
| 234 | + expect(sectionTabKey({ label: 'Main' }, 0)).toBe('Main'); |
| 235 | + expect(sectionTabKey({}, 2)).toBe('tab-2'); |
| 236 | + }); |
| 237 | + |
| 238 | + it('the open tab is the pick, else defaultTab, else the first — each only if drawn', () => { |
| 239 | + const drawn = ['a', 'b', 'c']; |
| 240 | + expect(resolveActiveTab(drawn, 'c', 'b')).toBe('c'); |
| 241 | + expect(resolveActiveTab(drawn, null, 'b')).toBe('b'); |
| 242 | + expect(resolveActiveTab(drawn, 'gone', 'b')).toBe('b'); |
| 243 | + expect(resolveActiveTab(drawn, null, 'hidden')).toBe('a'); |
| 244 | + expect(resolveActiveTab([], null, 'b')).toBeUndefined(); |
| 245 | + }); |
| 246 | +}); |
0 commit comments