|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#6846 — the Data pillar's four registry consumers on a POPULATED |
| 5 | + * registry: the contrast half of the empty-registry pins. |
| 6 | + * |
| 7 | + * ## The wrong fix this file exists to catch |
| 8 | + * |
| 9 | + * The empty-state repairs (#7120) and pins (#7120 + this card) all say what |
| 10 | + * a consumer must show when its registry read returns `undefined`. The |
| 11 | + * plausible wrong fix on that axis is an empty-state branch that fires |
| 12 | + * whenever the lookup is falsy in some wider sense — a guard inverted, a |
| 13 | + * `!!` dropped, a branch reordered — so that "the empty case now speaks" |
| 14 | + * while every populated screen goes blank or shows the notice. None of the |
| 15 | + * empty-registry files can see that: their registries are empty by |
| 16 | + * construction. So this file registers the builtin inspectors FIRST, proves |
| 17 | + * they are there, and pins that with them present: |
| 18 | + * |
| 19 | + * - a field click opens the REAL `ObjectFieldInspector`; |
| 20 | + * - Actions mounts the REAL `ActionDefaultInspector`; |
| 21 | + * - Hooks mounts the curated `HookDefaultInspector`, not the generic form; |
| 22 | + * - Settings mounts the REAL `ObjectDefaultInspector`; |
| 23 | + * |
| 24 | + * and that none of the empty-state sentences appears anywhere on screen. |
| 25 | + * Each case asserts a control only the real editor renders BEFORE it asserts |
| 26 | + * the notice's absence, so a blank screen reds on the positive, not on an |
| 27 | + * absence that a blank screen would satisfy. |
| 28 | + * |
| 29 | + * The fifth consumer, the Interfaces canvas, has its populated contrast in |
| 30 | + * {@link file://./StudioDesignSurface.designerRegistryPartial.test.tsx}. |
| 31 | + * |
| 32 | + * ## Why a separate file |
| 33 | + * |
| 34 | + * The registries are plain `Map`s — module state shared by every test in a |
| 35 | + * file. Splitting empty from populated is what lets each file assert its own |
| 36 | + * precondition instead of depending on test order. Same convention as the |
| 37 | + * `designerRegistryMissing` / `designerRegistryPartial` pair. |
| 38 | + */ |
| 39 | + |
| 40 | +import '@testing-library/jest-dom/vitest'; |
| 41 | +import * as React from 'react'; |
| 42 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 43 | +import { render, screen, fireEvent, cleanup, waitFor, within } from '@testing-library/react'; |
| 44 | +import { MemoryRouter } from 'react-router-dom'; |
| 45 | + |
| 46 | +const objectDef = { |
| 47 | + name: 'showcase_task', |
| 48 | + label: 'Task', |
| 49 | + fields: [{ name: 'title', label: 'Title', type: 'text' }], |
| 50 | + actions: [{ name: 'send_email', label: 'Send Email', type: 'quick' }], |
| 51 | +}; |
| 52 | + |
| 53 | +const hook = { |
| 54 | + name: 'guard_hook', |
| 55 | + label: 'Guard', |
| 56 | + object: 'showcase_task', |
| 57 | + events: ['beforeInsert'], |
| 58 | + handler: 'guard_fn', |
| 59 | +}; |
| 60 | + |
| 61 | +const mockClient = { |
| 62 | + save: vi.fn(async () => ({})), |
| 63 | + list: vi.fn(async (type: string) => { |
| 64 | + if (type === 'object') return [{ name: 'showcase_task', label: 'Task' }]; |
| 65 | + if (type === 'hook') return [hook]; |
| 66 | + return []; |
| 67 | + }), |
| 68 | + listDrafts: vi.fn(async () => []), |
| 69 | + layered: vi.fn(async () => ({ effective: objectDef, code: objectDef })), |
| 70 | + getDraft: vi.fn(async () => null), |
| 71 | + // The curated hook editor resolves the bound object's field catalog through |
| 72 | + // `useObjectFields` -> `client.get`. |
| 73 | + get: vi.fn(async () => null), |
| 74 | +}; |
| 75 | + |
| 76 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 77 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 78 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 79 | +}); |
| 80 | + |
| 81 | +vi.mock('./packages-io', async (importOriginal) => { |
| 82 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 83 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 84 | +}); |
| 85 | + |
| 86 | +// objectui#5813 — the advanced tabs live in a Radix DropdownMenu; this file |
| 87 | +// measures which editor mounts, not radix's open/close machinery, so the menu |
| 88 | +// renders as plain passthroughs (same convention as DataPillar.panelGate). |
| 89 | +type Passthrough = { children?: React.ReactNode }; |
| 90 | +vi.mock('@object-ui/components', async (importOriginal) => { |
| 91 | + const mod = await importOriginal<typeof import('@object-ui/components')>(); |
| 92 | + return { |
| 93 | + ...mod, |
| 94 | + DropdownMenu: (p: Passthrough) => <div>{p.children}</div>, |
| 95 | + DropdownMenuTrigger: (p: Passthrough) => <div>{p.children}</div>, |
| 96 | + DropdownMenuContent: (p: Passthrough) => <div>{p.children}</div>, |
| 97 | + DropdownMenuItem: (p: Passthrough & { onSelect?: () => void }) => ( |
| 98 | + <button type="button" onClick={() => p.onSelect?.()}>{p.children}</button> |
| 99 | + ), |
| 100 | + }; |
| 101 | +}); |
| 102 | + |
| 103 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 104 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 105 | + return { ...mod, useAdapter: () => ({}) }; |
| 106 | +}); |
| 107 | + |
| 108 | +import { DataPillar } from './StudioDesignSurface'; |
| 109 | +import { registerBuiltinInspectors } from '../metadata-admin/inspectors'; |
| 110 | +import { listMetadataInspectorTypes, getMetadataInspector } from '../metadata-admin/inspector-registry'; |
| 111 | +import { getMetadataDefaultInspector } from '../metadata-admin/default-inspector-registry'; |
| 112 | + |
| 113 | +// The populated precondition — registered here, proved in every test below. |
| 114 | +registerBuiltinInspectors(); |
| 115 | + |
| 116 | +/** The four empty-state sentences the Data pillar's consumers can show. */ |
| 117 | +const EMPTY_STATE_SENTENCES = [ |
| 118 | + 'No field inspector is registered in this session, so this field’s properties cannot be edited here.', |
| 119 | + 'No action editor is registered in this session, so this action’s properties cannot be edited here.', |
| 120 | + 'No default object inspector registered.', |
| 121 | + 'No metadata designers are registered in this session', |
| 122 | +]; |
| 123 | + |
| 124 | +afterEach(cleanup); |
| 125 | + |
| 126 | +/** |
| 127 | + * Assert the registries really are populated — stated, not assumed, so an |
| 128 | + * assertion below cannot pass for the wrong reason (a builtin that silently |
| 129 | + * stopped registering would otherwise turn every case here into an |
| 130 | + * empty-registry case that happens to render). |
| 131 | + */ |
| 132 | +function assertRegistriesPopulated(): void { |
| 133 | + expect(listMetadataInspectorTypes()).toContain('object'); |
| 134 | + expect(getMetadataInspector('object')).toBeTypeOf('function'); |
| 135 | + expect(getMetadataDefaultInspector('object')).toBeTypeOf('function'); |
| 136 | + expect(getMetadataDefaultInspector('action')).toBeTypeOf('function'); |
| 137 | + expect(getMetadataDefaultInspector('hook')).toBeTypeOf('function'); |
| 138 | +} |
| 139 | + |
| 140 | +function expectNoEmptyStateSentence(): void { |
| 141 | + const text = document.body.textContent ?? ''; |
| 142 | + for (const sentence of EMPTY_STATE_SENTENCES) expect(text).not.toContain(sentence); |
| 143 | +} |
| 144 | + |
| 145 | +function renderPillar() { |
| 146 | + render( |
| 147 | + <MemoryRouter initialEntries={['/studio/com.example.showcase/data']}> |
| 148 | + <DataPillar packageId="com.example.showcase" /> |
| 149 | + </MemoryRouter>, |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +describe('Data pillar consumers with the registries POPULATED (#6846 contrast)', () => { |
| 154 | + it('a field click opens the real field inspector, not the missing-inspector notice', async () => { |
| 155 | + assertRegistriesPopulated(); |
| 156 | + renderPillar(); |
| 157 | + |
| 158 | + fireEvent.click(await screen.findByRole('button', { name: 'Form' })); |
| 159 | + const card = (await screen.findByText('Title')).closest('.cursor-grab') as HTMLElement; |
| 160 | + expect(card).toBeTruthy(); |
| 161 | + fireEvent.click(card); |
| 162 | + |
| 163 | + const rail = await waitFor(() => { |
| 164 | + const el = document.querySelector('aside'); |
| 165 | + expect(el).toBeTruthy(); |
| 166 | + return el as HTMLElement; |
| 167 | + }); |
| 168 | + expect(rail).toHaveTextContent('Field properties'); |
| 169 | + // `ObjectFieldInspector`'s own API-name control, carrying the field's name. |
| 170 | + expect(await within(rail).findByDisplayValue('title')).toBeInTheDocument(); |
| 171 | + expectNoEmptyStateSentence(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('Actions mounts the real action editor, not the missing-editor notice', async () => { |
| 175 | + assertRegistriesPopulated(); |
| 176 | + renderPillar(); |
| 177 | + |
| 178 | + fireEvent.click(await screen.findByRole('button', { name: 'Actions' })); |
| 179 | + // `ActionDefaultInspector`'s Name control, carrying the action's identifier. |
| 180 | + expect(await screen.findByDisplayValue('send_email')).toBeInTheDocument(); |
| 181 | + expectNoEmptyStateSentence(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('Hooks mounts the curated hook editor, not the generic form', async () => { |
| 185 | + assertRegistriesPopulated(); |
| 186 | + renderPillar(); |
| 187 | + |
| 188 | + fireEvent.click(await screen.findByRole('button', { name: 'Hooks' })); |
| 189 | + fireEvent.click(await screen.findByText('Guard')); |
| 190 | + // Controls only `HookDefaultInspector` renders… |
| 191 | + expect(await screen.findByTestId('hook-name')).toBeInTheDocument(); |
| 192 | + expect(screen.getByTestId('hook-body-source')).toBeInTheDocument(); |
| 193 | + // …and none the generic `SchemaForm` would have synthesised from the |
| 194 | + // hook's keys (the empty-registry pin asserts this one is PRESENT). |
| 195 | + expect(screen.queryByLabelText('Handler')).toBeNull(); |
| 196 | + expectNoEmptyStateSentence(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('Settings mounts the real object inspector, not the missing-inspector notice', async () => { |
| 200 | + assertRegistriesPopulated(); |
| 201 | + renderPillar(); |
| 202 | + |
| 203 | + fireEvent.click(await screen.findByRole('button', { name: 'Settings' })); |
| 204 | + // `ObjectDefaultInspector`'s Name control, carrying the object's name. |
| 205 | + expect(await screen.findByTestId('object-name-input')).toHaveValue('showcase_task'); |
| 206 | + expect(screen.getByTestId('object-access-posture')).toBeInTheDocument(); |
| 207 | + expectNoEmptyStateSentence(); |
| 208 | + }); |
| 209 | +}); |
0 commit comments