|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29569 — an `object_extension` row names, links and marks the |
| 5 | + * object it extends. |
| 6 | + * |
| 7 | + * Measured on the card: the pair `com.example.showcase.custom` extended |
| 8 | + * `showcase_task`, and the list read served the item as stored, |
| 9 | + * `{ extend: 'showcase_task', fields, _packageId }`, with no `name` |
| 10 | + * (`ObjectExtensionSchema` refuses one; the row is stored under the target's |
| 11 | + * name). The list read `name`, so Studio's All metadata area drew the row as |
| 12 | + * "—" and linked it to `…/object_extension/(unnamed-0)`, an empty page. The |
| 13 | + * type now declares `extend` as its identity key (`default-schemas.ts`). |
| 14 | + * |
| 15 | + * Pinned, each against a control: |
| 16 | + * |
| 17 | + * - in the cross-package leg the row shows `showcase_task` under an "Object" |
| 18 | + * header and links to that extension in its own package; |
| 19 | + * - in the base's area the row is found by `extend`, carries the pair's |
| 20 | + * "Added" mark, and its link addresses the extension, never `(unnamed-N)`; |
| 21 | + * - the mark is looked up by `extend`: a read listing the extension as an |
| 22 | + * override marks the row "Customized"; |
| 23 | + * - the search box matches `extend`; |
| 24 | + * - the Label column shows the extension's own label; |
| 25 | + * - CONTROL: a row of another type (`page`) still names and links by `name`. |
| 26 | + */ |
| 27 | + |
| 28 | +import '@testing-library/jest-dom/vitest'; |
| 29 | +import * as React from 'react'; |
| 30 | +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; |
| 31 | +import { render, screen, cleanup, within, fireEvent } from '@testing-library/react'; |
| 32 | +import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom'; |
| 33 | +// The switch column waits on `audienceAnchorsOf`'s dynamic import of this exact |
| 34 | +// specifier (see `ResourceListPage.environmentScope-7611.test.tsx`). |
| 35 | +import '@objectstack/spec/identity'; |
| 36 | +import { MetadataAreaHostContext, type MetadataAreaHost } from './metadataAreaHost'; |
| 37 | + |
| 38 | +const BASE = 'com.example.showcase'; |
| 39 | +const PAIR = 'com.example.showcase.custom'; |
| 40 | + |
| 41 | +/** What the list read serves: the extension body as stored, no `name`. */ |
| 42 | +const LISTS: Record<string, Record<string, unknown>[]> = { |
| 43 | + object_extension: [ |
| 44 | + { |
| 45 | + extend: 'showcase_task', |
| 46 | + label: 'Work items', |
| 47 | + fields: { custom_note__c: { type: 'text', label: 'Custom note' } }, |
| 48 | + _packageId: PAIR, |
| 49 | + }, |
| 50 | + { extend: 'showcase_project', fields: { budget_code__c: { type: 'text' } }, _packageId: PAIR }, |
| 51 | + ], |
| 52 | + page: [{ name: 'task_board', label: 'Task board', _packageId: PAIR }], |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * The package list as served: the code-provided base and its writable pair, a |
| 57 | + * project package, which the cross-package leg's list scopes to. |
| 58 | + */ |
| 59 | +const PACKAGES = [ |
| 60 | + { manifest: { id: BASE, name: 'Showcase', scope: 'installed' } }, |
| 61 | + { manifest: { id: PAIR, name: 'Showcase (custom)' } }, |
| 62 | +]; |
| 63 | + |
| 64 | +/** Stable singleton — the page's load effect keys on the client identity. */ |
| 65 | +const CLIENT = { |
| 66 | + list: async (type: string) => (type === 'package' ? PACKAGES : (LISTS[type] ?? [])), |
| 67 | +}; |
| 68 | + |
| 69 | +const entry = (type: string, label: string) => ({ |
| 70 | + type, |
| 71 | + label, |
| 72 | + domain: 'data', |
| 73 | + allowEnvironmentOverlay: false, |
| 74 | + allowRuntimeCreate: true, |
| 75 | +}); |
| 76 | +const ENTRIES = [entry('object_extension', 'Object Extension'), entry('page', 'Page')]; |
| 77 | + |
| 78 | +vi.mock('./useMetadata', async (importOriginal) => ({ |
| 79 | + ...(await importOriginal<Record<string, unknown>>()), |
| 80 | + useMetadataClient: () => CLIENT, |
| 81 | + useMetadataTypes: () => ({ loading: false, error: null, entries: ENTRIES }), |
| 82 | +})); |
| 83 | +vi.mock('./i18n', async (importOriginal) => ({ |
| 84 | + ...(await importOriginal<Record<string, unknown>>()), |
| 85 | + useMetadataLocale: () => 'en-US', |
| 86 | +})); |
| 87 | +vi.mock('../../hooks/useCanAuthorMetadata', async (importOriginal) => ({ |
| 88 | + ...(await importOriginal<Record<string, unknown>>()), |
| 89 | + useCanAuthorMetadata: () => true, |
| 90 | +})); |
| 91 | + |
| 92 | +import { MetadataResourceListPage } from './ResourceListPage'; |
| 93 | +import { registerDefaultMetadataSchemas } from './default-schemas'; |
| 94 | +import { t } from './i18n'; |
| 95 | + |
| 96 | +registerDefaultMetadataSchemas(); |
| 97 | + |
| 98 | +const EMPTY_COUNTS = { overrides: 0, extensions: 0, switchedOff: 0, contributions: 0 }; |
| 99 | +/** The base's read as served on the card: the extension's field, no override. */ |
| 100 | +const SERVED_READ = { |
| 101 | + packageId: BASE, |
| 102 | + overrides: [], |
| 103 | + extensions: [{ object: 'showcase_task', kind: 'field', name: 'custom_note__c' }], |
| 104 | + switchedOff: [], |
| 105 | + contributions: [], |
| 106 | + counts: { ...EMPTY_COUNTS, extensions: 1 }, |
| 107 | +}; |
| 108 | +let read: Record<string, unknown> = SERVED_READ; |
| 109 | + |
| 110 | +const fetchMock = vi.fn(async (url: string) => { |
| 111 | + const json = (status: number, body: unknown) => |
| 112 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 113 | + const u = String(url); |
| 114 | + if (u === '/api/v1/packages') { |
| 115 | + return json(200, { success: true, data: { packages: [{ manifest: { id: BASE, name: 'Showcase', scope: 'installed' }, writable: false }] } }); |
| 116 | + } |
| 117 | + if (/^\/api\/v1\/packages\/[^/]+\/customizations$/.test(u)) return json(200, { success: true, data: read }); |
| 118 | + if (u.startsWith('/api/v1/data/sys_metadata_activation')) { |
| 119 | + return json(200, { object: 'sys_metadata_activation', records: [], hasMore: false }); |
| 120 | + } |
| 121 | + return json(404, { success: false }); |
| 122 | +}); |
| 123 | + |
| 124 | +beforeEach(() => { |
| 125 | + read = SERVED_READ; |
| 126 | + fetchMock.mockClear(); |
| 127 | + vi.stubGlobal('fetch', fetchMock); |
| 128 | +}); |
| 129 | +afterEach(() => { |
| 130 | + cleanup(); |
| 131 | + vi.unstubAllGlobals(); |
| 132 | +}); |
| 133 | + |
| 134 | +const area = (packageId: string | null): MetadataAreaHost => ({ |
| 135 | + packageId, |
| 136 | + readOnly: packageId !== null, |
| 137 | + basePath: `/studio/${packageId ?? '~all'}/metadata`, |
| 138 | +}); |
| 139 | + |
| 140 | +function Where() { |
| 141 | + const loc = useLocation(); |
| 142 | + return <div data-testid="where">{loc.pathname + loc.search}</div>; |
| 143 | +} |
| 144 | + |
| 145 | +function mountList(host: MetadataAreaHost, type: string, search = '') { |
| 146 | + return render( |
| 147 | + <MetadataAreaHostContext.Provider value={host}> |
| 148 | + <MemoryRouter initialEntries={[`${host.basePath}/${type}${search}`]}> |
| 149 | + <Routes> |
| 150 | + <Route path={`${host.basePath}/${type}`} element={<MetadataResourceListPage type={type} />} /> |
| 151 | + <Route path="*" element={<Where />} /> |
| 152 | + </Routes> |
| 153 | + </MemoryRouter> |
| 154 | + </MetadataAreaHostContext.Provider>, |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +const linkOf = (row: HTMLElement) => within(row).getAllByRole('link')[0]!; |
| 159 | +const marksOf = (row: HTMLElement) => |
| 160 | + within(row) |
| 161 | + .queryAllByTestId('customization-mark') |
| 162 | + .map((m) => m.getAttribute('data-kind')); |
| 163 | + |
| 164 | +describe('an object_extension row names, links and marks the object it extends (objectstack#29569)', () => { |
| 165 | + it('the cross-package leg shows `extend` under an "Object" header and links to that extension', async () => { |
| 166 | + mountList(area(null), 'object_extension'); |
| 167 | + const row = await screen.findByTestId('metadata-row-showcase_task'); |
| 168 | + |
| 169 | + const cells = within(row).getAllByRole('cell'); |
| 170 | + expect(cells[0]).toHaveTextContent('showcase_task'); |
| 171 | + expect(cells[0]).not.toHaveTextContent('—'); |
| 172 | + expect(screen.queryByTestId(/^metadata-row-\(unnamed-/)).toBeNull(); |
| 173 | + const headers = screen.getAllByRole('columnheader').map((h) => h.textContent?.trim()); |
| 174 | + expect(headers[0]).toBe(t('engine.list.col.extend', 'en-US')); |
| 175 | + expect(t('engine.list.col.extend', 'en-US')).toBe('Object'); |
| 176 | + |
| 177 | + const href = linkOf(row).getAttribute('href') ?? ''; |
| 178 | + expect(href).toBe(`/studio/~all/metadata/object_extension/showcase_task?package=${encodeURIComponent(PAIR)}`); |
| 179 | + fireEvent.click(linkOf(row)); |
| 180 | + expect(screen.getByTestId('where')).toHaveTextContent( |
| 181 | + `/studio/~all/metadata/object_extension/showcase_task?package=${PAIR}`, |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + it("in the base's area the pair's row is found by `extend`, marked Added, and opens that extension", async () => { |
| 186 | + mountList(area(BASE), 'object_extension', `?package=${BASE}`); |
| 187 | + const row = await screen.findByTestId('metadata-row-showcase_task'); |
| 188 | + await within(row).findByTestId('customization-mark'); |
| 189 | + |
| 190 | + expect(within(row).getAllByRole('cell')[0]).toHaveTextContent('showcase_task'); |
| 191 | + expect(marksOf(row)).toEqual(['added']); |
| 192 | + expect(linkOf(row).getAttribute('href')).toBe( |
| 193 | + `/studio/${BASE}/metadata/object_extension/showcase_task?package=${encodeURIComponent(PAIR)}`, |
| 194 | + ); |
| 195 | + }); |
| 196 | + |
| 197 | + it('the mark is looked up by `extend`: a read listing the extension as an override marks it Customized', async () => { |
| 198 | + read = { |
| 199 | + ...SERVED_READ, |
| 200 | + overrides: [{ type: 'object_extension', name: 'showcase_task' }], |
| 201 | + counts: { ...EMPTY_COUNTS, overrides: 1, extensions: 1 }, |
| 202 | + }; |
| 203 | + mountList(area(BASE), 'object_extension', `?package=${BASE}`); |
| 204 | + const row = await screen.findByTestId('metadata-row-showcase_task'); |
| 205 | + await within(row).findByTestId('customization-mark'); |
| 206 | + |
| 207 | + expect(marksOf(row)).toEqual(['customized']); |
| 208 | + // CONTROL: the other extension the read does not list stays "Added". |
| 209 | + expect(marksOf(screen.getByTestId('metadata-row-showcase_project'))).toEqual(['added']); |
| 210 | + }); |
| 211 | + |
| 212 | + it('the search box matches `extend`', async () => { |
| 213 | + mountList(area(null), 'object_extension'); |
| 214 | + await screen.findByTestId('metadata-row-showcase_task'); |
| 215 | + |
| 216 | + fireEvent.change(screen.getByRole('textbox'), { target: { value: 'project' } }); |
| 217 | + expect(screen.getByTestId('metadata-row-showcase_project')).toBeInTheDocument(); |
| 218 | + expect(screen.queryByTestId('metadata-row-showcase_task')).toBeNull(); |
| 219 | + }); |
| 220 | + |
| 221 | + it("the Label column shows the extension's own label", async () => { |
| 222 | + mountList(area(null), 'object_extension'); |
| 223 | + const row = await screen.findByTestId('metadata-row-showcase_task'); |
| 224 | + |
| 225 | + expect(within(row).getAllByRole('cell')[1]).toHaveTextContent('Work items'); |
| 226 | + expect(within(screen.getByTestId('metadata-row-showcase_project')).getAllByRole('cell')[1]).toHaveTextContent('—'); |
| 227 | + }); |
| 228 | + |
| 229 | + it('CONTROL: a row of another type still names and links by `name`', async () => { |
| 230 | + mountList(area(null), 'page'); |
| 231 | + const row = await screen.findByTestId('metadata-row-task_board'); |
| 232 | + |
| 233 | + expect(within(row).getAllByRole('cell')[0]).toHaveTextContent('task_board'); |
| 234 | + expect(linkOf(row).getAttribute('href')).toBe( |
| 235 | + `/studio/~all/metadata/page/task_board?package=${encodeURIComponent(PAIR)}`, |
| 236 | + ); |
| 237 | + const headers = screen.getAllByRole('columnheader').map((h) => h.textContent?.trim()); |
| 238 | + expect(headers[0]).toBe(t('engine.list.col.name', 'en-US')); |
| 239 | + }); |
| 240 | +}); |
0 commit comments