|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#12450 — the Data pillar's flat tab row, and the object header's two |
| 5 | + * icon buttons. |
| 6 | + * |
| 7 | + * Decision B of epic objectui#12446 (maintainer, 2026-10-15): seven flat tabs |
| 8 | + * — Records, Form, Validations, Hooks, Actions, API, Settings — when the |
| 9 | + * content column is 1000 px or wider; narrower, the last five fold back into |
| 10 | + * "Advanced ▾". The folded form keeps its own pins in |
| 11 | + * `DataPillar.advancedKeepsName-11794.test.tsx`; this file pins the flat form. |
| 12 | + * |
| 13 | + * What a DOM test can and cannot see: happy-dom evaluates no container query, |
| 14 | + * so both forms are in the tree here, exactly as they are in a browser, and |
| 15 | + * which one is DISPLAYED is decided by CSS this environment does not run. The |
| 16 | + * pins therefore read what decides it — the content column is the query |
| 17 | + * container, the five flat tabs carry the class that displays them at the |
| 18 | + * fold width and above, the Advanced trigger carries the class that hides it |
| 19 | + * there, and both classes name `DATA_TABS_FOLD_WIDTH` — and the browser |
| 20 | + * screenshots on the pull request show the two widths rendered. |
| 21 | + * |
| 22 | + * The REAL `DataPillar`; the records grid and two panels are stubbed, because |
| 23 | + * only which panel is open is under test, not what it draws. |
| 24 | + */ |
| 25 | + |
| 26 | +import '@testing-library/jest-dom/vitest'; |
| 27 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 28 | +import { cleanup, render, screen, waitFor, within } from '@testing-library/react'; |
| 29 | +import userEvent from '@testing-library/user-event'; |
| 30 | +import { MemoryRouter } from 'react-router-dom'; |
| 31 | + |
| 32 | +const servedObject = { |
| 33 | + name: 'showcase_account', |
| 34 | + label: 'Account', |
| 35 | + fields: { name: { type: 'text', label: 'Account Name' } }, |
| 36 | +}; |
| 37 | + |
| 38 | +const mockClient = { |
| 39 | + list: vi.fn(async () => [{ name: 'showcase_account', label: 'Account' }]), |
| 40 | + listDrafts: vi.fn(async () => []), |
| 41 | + layered: vi.fn(async () => ({ effective: servedObject, code: servedObject })), |
| 42 | + getDraft: vi.fn(async () => null), |
| 43 | + save: vi.fn(async () => ({ ok: true })), |
| 44 | +}; |
| 45 | + |
| 46 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 47 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 48 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 49 | +}); |
| 50 | + |
| 51 | +vi.mock('./packages-io', async (importOriginal) => { |
| 52 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 53 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 54 | +}); |
| 55 | + |
| 56 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 57 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 58 | + return { ...mod, useAdapter: () => ({}) }; |
| 59 | +}); |
| 60 | + |
| 61 | +vi.mock('@object-ui/plugin-view', async (importOriginal) => { |
| 62 | + const mod = await importOriginal<Record<string, unknown>>(); |
| 63 | + return { ...mod, ObjectView: () => <div data-testid="records-panel" /> }; |
| 64 | +}); |
| 65 | + |
| 66 | +vi.mock('./ObjectValidationsPanel', () => ({ ObjectValidationsPanel: () => <div data-testid="rules-panel" /> })); |
| 67 | +vi.mock('./ObjectSettingsPanel', () => ({ ObjectSettingsPanel: () => <div data-testid="settings-panel" /> })); |
| 68 | + |
| 69 | +import { DataPillar } from './StudioDesignSurface'; |
| 70 | +import { SurfaceDeepLinkProvider } from './surfaceDeepLinkChannel'; |
| 71 | +import { |
| 72 | + DATA_CONTENT_CONTAINER, |
| 73 | + DATA_TAB_FLAT_ONLY, |
| 74 | + DATA_TAB_FOLDED_ONLY, |
| 75 | + DATA_TABS_FOLD_WIDTH, |
| 76 | +} from './dataTabsFold'; |
| 77 | +import { t } from '../metadata-admin/i18n'; |
| 78 | + |
| 79 | +const FLAT_KEYS = ['rules', 'hooks', 'actions', 'api', 'settings'] as const; |
| 80 | +const ROW_NAMES = ['records', 'form', ...FLAT_KEYS].map((k) => t(`engine.studio.data.tab.${k}`, 'en')); |
| 81 | +const ADVANCED = t('engine.studio.data.tab.advanced', 'en'); |
| 82 | + |
| 83 | +afterEach(() => { |
| 84 | + cleanup(); |
| 85 | + vi.clearAllMocks(); |
| 86 | +}); |
| 87 | + |
| 88 | +async function renderPillar(props: { readOnly?: boolean } = {}) { |
| 89 | + render( |
| 90 | + <MemoryRouter initialEntries={['/studio/com.example.showcase/data']}> |
| 91 | + <SurfaceDeepLinkProvider> |
| 92 | + <DataPillar packageId="com.example.showcase" readOnly={props.readOnly} /> |
| 93 | + </SurfaceDeepLinkProvider> |
| 94 | + </MemoryRouter>, |
| 95 | + ); |
| 96 | + await waitFor(() => expect(screen.getByTestId('records-panel')).toBeInTheDocument(), { timeout: 8000 }); |
| 97 | +} |
| 98 | + |
| 99 | +/** The header's tab row: the `nav` the item header draws its tabs into. */ |
| 100 | +function tabRow(): HTMLElement { |
| 101 | + const nav = screen.getByTestId('data-object-header').querySelector('nav'); |
| 102 | + expect(nav, 'the object header draws no tab row').not.toBeNull(); |
| 103 | + return nav as HTMLElement; |
| 104 | +} |
| 105 | + |
| 106 | +/** Split a class attribute into its tokens. */ |
| 107 | +function classesOf(el: Element): string[] { |
| 108 | + return (el.getAttribute('class') ?? '').split(/\s+/).filter(Boolean); |
| 109 | +} |
| 110 | + |
| 111 | +describe('the Data pillar’s seven flat tabs (objectui#12450)', () => { |
| 112 | + it('both class strings name the fold width, so the rule and its constant cannot drift apart', () => { |
| 113 | + const query = `@min-[${DATA_TABS_FOLD_WIDTH}px]:`; |
| 114 | + expect(DATA_TABS_FOLD_WIDTH).toBe(1000); |
| 115 | + expect(DATA_TAB_FLAT_ONLY.split(' ')).toEqual(['hidden', `${query}inline-flex`]); |
| 116 | + expect(DATA_TAB_FOLDED_ONLY.split(' ')).toEqual([`${query}hidden`]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('the tab row holds the seven tabs in the artboard’s order, then the folded trigger', async () => { |
| 120 | + await renderPillar(); |
| 121 | + const row = tabRow(); |
| 122 | + const buttons = within(row).getAllByRole('button'); |
| 123 | + expect(buttons.map((b) => b.textContent)).toEqual([...ROW_NAMES, ADVANCED]); |
| 124 | + expect(buttons.at(-1)).toBe(screen.getByTestId('data-tabs-advanced')); |
| 125 | + }); |
| 126 | + |
| 127 | + it('the content column is the query container, and it holds the tab row it measures for', async () => { |
| 128 | + await renderPillar(); |
| 129 | + const column = screen.getByTestId('data-content-column'); |
| 130 | + expect(classesOf(column)).toContain(DATA_CONTENT_CONTAINER); |
| 131 | + expect(column).toContainElement(tabRow()); |
| 132 | + }); |
| 133 | + |
| 134 | + it('the five panels’ flat tabs display only at the fold width and above; the trigger only below it', async () => { |
| 135 | + await renderPillar(); |
| 136 | + for (const key of FLAT_KEYS) { |
| 137 | + const tab = screen.getByTestId(`data-tab-flat-${key}`); |
| 138 | + for (const cls of DATA_TAB_FLAT_ONLY.split(' ')) expect(classesOf(tab)).toContain(cls); |
| 139 | + // `cn` merges the tab's own `inline-flex` away: an unconditional |
| 140 | + // display class left beside `hidden` would show the tab at every width. |
| 141 | + expect(classesOf(tab)).not.toContain('inline-flex'); |
| 142 | + } |
| 143 | + const trigger = screen.getByTestId('data-tabs-advanced'); |
| 144 | + expect(classesOf(trigger)).toContain(DATA_TAB_FOLDED_ONLY); |
| 145 | + expect(classesOf(trigger)).not.toContain('hidden'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('CONTROL: Records and Form are displayed at every width', async () => { |
| 149 | + await renderPillar(); |
| 150 | + const row = tabRow(); |
| 151 | + for (const name of ROW_NAMES.slice(0, 2)) { |
| 152 | + const tab = within(row).getByRole('button', { name }); |
| 153 | + expect(classesOf(tab)).toContain('inline-flex'); |
| 154 | + expect(classesOf(tab)).not.toContain('hidden'); |
| 155 | + expect(classesOf(tab).some((c) => c.startsWith('@'))).toBe(false); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + it('a flat tab opens its panel and takes the active underline; the folded trigger mirrors it', async () => { |
| 160 | + await renderPillar(); |
| 161 | + const validations = screen.getByTestId('data-tab-flat-rules'); |
| 162 | + expect(validations).toHaveAttribute('aria-pressed', 'false'); |
| 163 | + |
| 164 | + await userEvent.click(validations); |
| 165 | + await waitFor(() => expect(screen.getByTestId('rules-panel')).toBeInTheDocument()); |
| 166 | + expect(validations).toHaveAttribute('aria-pressed', 'true'); |
| 167 | + // The two forms say the same thing: at the narrower width the trigger is |
| 168 | + // the one on screen, and it shows a power panel is open. |
| 169 | + expect(screen.getByTestId('data-tabs-advanced')).toHaveAttribute('aria-pressed', 'true'); |
| 170 | + |
| 171 | + await userEvent.click(screen.getByTestId('data-tab-flat-settings')); |
| 172 | + await waitFor(() => expect(screen.getByTestId('settings-panel')).toBeInTheDocument()); |
| 173 | + expect(screen.queryByTestId('rules-panel')).toBeNull(); |
| 174 | + expect(validations).toHaveAttribute('aria-pressed', 'false'); |
| 175 | + expect(screen.getByTestId('data-tab-flat-settings')).toHaveAttribute('aria-pressed', 'true'); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +describe('the object header’s icon buttons (objectui#12450)', () => { |
| 180 | + const OBJECT_SETTINGS = t('engine.studio.data.objectSettings', 'en'); |
| 181 | + const MORE_ACTIONS = t('engine.studio.data.moreActions', 'en'); |
| 182 | + const ADD_FIELD = t('engine.studio.data.addField', 'en'); |
| 183 | + |
| 184 | + it('each is named by an aria-label; Object settings opens the Settings panel', async () => { |
| 185 | + await renderPillar(); |
| 186 | + const header = screen.getByTestId('data-object-header'); |
| 187 | + const settings = within(header).getByRole('button', { name: OBJECT_SETTINGS }); |
| 188 | + expect(settings).toHaveAttribute('aria-label', OBJECT_SETTINGS); |
| 189 | + expect(within(header).getByRole('button', { name: MORE_ACTIONS })).toHaveAttribute('aria-label', MORE_ACTIONS); |
| 190 | + |
| 191 | + await userEvent.click(settings); |
| 192 | + await waitFor(() => expect(screen.getByTestId('settings-panel')).toBeInTheDocument()); |
| 193 | + expect(screen.getByTestId('data-tab-flat-settings')).toHaveAttribute('aria-pressed', 'true'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('More holds Add field, which adds a field and opens its inspector', async () => { |
| 197 | + await renderPillar(); |
| 198 | + // Add field left the header row: no labelled button for it there now. |
| 199 | + const header = screen.getByTestId('data-object-header'); |
| 200 | + expect(within(header).queryByRole('button', { name: ADD_FIELD })).toBeNull(); |
| 201 | + |
| 202 | + await userEvent.click(screen.getByTestId('data-object-more')); |
| 203 | + await userEvent.click(await screen.findByRole('menuitem', { name: ADD_FIELD })); |
| 204 | + expect(await screen.findByTestId('data-field-inspector', undefined, { timeout: 4000 })).toBeInTheDocument(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('CONTROL: a read-only package keeps Object settings and draws no More, whose one entry it does not offer', async () => { |
| 208 | + await renderPillar({ readOnly: true }); |
| 209 | + expect(screen.getByTestId('data-object-settings')).toBeInTheDocument(); |
| 210 | + expect(screen.queryByTestId('data-object-more')).toBeNull(); |
| 211 | + }); |
| 212 | +}); |
0 commit comments