|
| 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 | + * #2959: `FormSchema.fieldTabs` — a tabbed field layout inside ONE form. |
| 11 | + * |
| 12 | + * The contract these pin: |
| 13 | + * - one `<form>` / react-hook-form instance for every tab; |
| 14 | + * - panels are force-mounted (only CSS-hidden), so a tab the user leaves keeps |
| 15 | + * its values AND its validation — react-hook-form skips *unmounted* fields, |
| 16 | + * which is how a required field on an unopened tab used to reach the server; |
| 17 | + * - a failed submit activates the tab holding the first offending field and |
| 18 | + * marks every tab that has one; |
| 19 | + * - a field no tab claims is still rendered (never silently dropped). |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest'; |
| 23 | +import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react'; |
| 24 | +import { ComponentRegistry } from '@object-ui/core'; |
| 25 | +import { toast } from '../../../ui/sonner'; |
| 26 | + |
| 27 | +beforeAll(async () => { |
| 28 | + await import('../../../renderers'); |
| 29 | +}, 30000); |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + vi.spyOn(toast, 'error').mockImplementation(() => 'id' as any); |
| 33 | + if (!(Element.prototype as any).scrollIntoView) { |
| 34 | + (Element.prototype as any).scrollIntoView = () => {}; |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + cleanup(); |
| 40 | + vi.restoreAllMocks(); |
| 41 | +}); |
| 42 | + |
| 43 | +const FIELDS = [ |
| 44 | + { name: 'subject', label: 'Subject', type: 'input', required: true }, |
| 45 | + { name: 'status', label: 'Status', type: 'input' }, |
| 46 | + { name: 'description', label: 'Description', type: 'input', required: true }, |
| 47 | +]; |
| 48 | + |
| 49 | +const TABS = [ |
| 50 | + { key: 'basics', label: 'Basics', fields: ['subject', 'status'] }, |
| 51 | + { key: 'detail', label: 'Detail', description: 'Anything else', fields: ['description'] }, |
| 52 | +]; |
| 53 | + |
| 54 | +function renderTabbedForm(overrides: Record<string, unknown> = {}) { |
| 55 | + const onSubmit = vi.fn(); |
| 56 | + const Form = ComponentRegistry.get('form')!; |
| 57 | + const utils = render( |
| 58 | + <Form |
| 59 | + schema={{ |
| 60 | + type: 'form', |
| 61 | + mode: 'create', |
| 62 | + showSubmit: true, |
| 63 | + showCancel: false, |
| 64 | + submitLabel: 'Create', |
| 65 | + fields: FIELDS, |
| 66 | + fieldTabs: TABS, |
| 67 | + onSubmit, |
| 68 | + ...overrides, |
| 69 | + }} |
| 70 | + />, |
| 71 | + ); |
| 72 | + return { ...utils, onSubmit }; |
| 73 | +} |
| 74 | + |
| 75 | +const submit = () => fireEvent.click(screen.getByRole('button', { name: /create/i })); |
| 76 | +const fill = (name: string, value: string) => { |
| 77 | + const input = document.body.querySelector<HTMLInputElement>(`[data-field="${name}"] input`)!; |
| 78 | + fireEvent.change(input, { target: { value } }); |
| 79 | +}; |
| 80 | +const tab = (name: RegExp) => screen.getByRole('tab', { name }); |
| 81 | + |
| 82 | +describe('form renderer — fieldTabs (#2959)', () => { |
| 83 | + it('renders one form and one panel per tab, with every field mounted', () => { |
| 84 | + const { container } = renderTabbedForm(); |
| 85 | + |
| 86 | + expect(container.querySelectorAll('form')).toHaveLength(1); |
| 87 | + expect(screen.getAllByRole('tab')).toHaveLength(2); |
| 88 | + // Both panels exist up front — the inactive one is hidden by CSS, not |
| 89 | + // unmounted, so its fields are present and registered. |
| 90 | + expect(screen.getByTestId('form-tab-panel:basics')).toBeTruthy(); |
| 91 | + expect(screen.getByTestId('form-tab-panel:detail')).toBeTruthy(); |
| 92 | + for (const name of ['subject', 'status', 'description']) { |
| 93 | + expect(container.querySelector(`[data-field="${name}"]`)).toBeTruthy(); |
| 94 | + } |
| 95 | + // The tab's blurb rides along with its panel. |
| 96 | + expect(screen.getByText('Anything else')).toBeTruthy(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('marks the inactive panel with data-state=inactive (CSS-hidden, still mounted)', () => { |
| 100 | + renderTabbedForm(); |
| 101 | + expect(screen.getByTestId('form-tab-panel:basics')).toHaveAttribute('data-state', 'active'); |
| 102 | + expect(screen.getByTestId('form-tab-panel:detail')).toHaveAttribute('data-state', 'inactive'); |
| 103 | + expect(screen.getByTestId('form-tab-panel:detail').className).toContain( |
| 104 | + 'data-[state=inactive]:hidden', |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('submits values entered across different tabs', async () => { |
| 109 | + const { onSubmit } = renderTabbedForm(); |
| 110 | + |
| 111 | + fill('subject', 'From tab 1'); |
| 112 | + fireEvent.click(tab(/Detail/)); |
| 113 | + fill('description', 'From tab 2'); |
| 114 | + submit(); |
| 115 | + |
| 116 | + await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); |
| 117 | + expect(onSubmit.mock.calls[0][0]).toMatchObject({ |
| 118 | + subject: 'From tab 1', |
| 119 | + description: 'From tab 2', |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('validates fields on tabs the user never opened, and jumps to the offender', async () => { |
| 124 | + const { onSubmit } = renderTabbedForm(); |
| 125 | + |
| 126 | + fill('subject', 'Only tab 1 filled'); |
| 127 | + submit(); |
| 128 | + |
| 129 | + // `description` is required and lives on a tab that was never opened. |
| 130 | + await waitFor(() => expect(tab(/Detail/)).toHaveAttribute('data-state', 'active')); |
| 131 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 132 | + expect(tab(/Detail/)).toHaveAttribute('data-error', 'true'); |
| 133 | + expect(tab(/Basics/)).not.toHaveAttribute('data-error'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('honors defaultFieldTab', () => { |
| 137 | + renderTabbedForm({ defaultFieldTab: 'detail' }); |
| 138 | + expect(screen.getByTestId('form-tab-panel:detail')).toHaveAttribute('data-state', 'active'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('renders fields no tab claims instead of dropping them', () => { |
| 142 | + const { container } = renderTabbedForm({ |
| 143 | + fieldTabs: [ |
| 144 | + { key: 'basics', label: 'Basics', fields: ['subject'] }, |
| 145 | + { key: 'detail', label: 'Detail', fields: ['description'] }, |
| 146 | + ], |
| 147 | + }); |
| 148 | + // `status` belongs to no tab — it must still be in the DOM, outside the panels. |
| 149 | + const status = container.querySelector('[data-field="status"]')!; |
| 150 | + expect(status).toBeTruthy(); |
| 151 | + expect(status.closest('[role="tabpanel"]')).toBeNull(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('falls back to a plain field list for a single tab', () => { |
| 155 | + renderTabbedForm({ fieldTabs: [{ key: 'only', label: 'Only', fields: ['subject'] }] }); |
| 156 | + expect(screen.queryAllByRole('tab')).toHaveLength(0); |
| 157 | + // …and every field still renders, not just the one the lone tab claimed. |
| 158 | + expect(document.body.querySelector('[data-field="description"]')).toBeTruthy(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments