|
| 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 | + * objectstack#27520 row 14 — what the record page appends after an authored |
| 11 | + * page lines up with that page's cards. |
| 12 | + * |
| 13 | + * Measured on the showcase project page: the Attachments card the host appends |
| 14 | + * (the object declares `enable.files`, the page places no `record:attachments`) |
| 15 | + * drew 24 px wider on each side than the page's own cards, and the provenance |
| 16 | + * footer with it. The page renderer insets its body (`p-3 md:p-4 lg:p-6`) and |
| 17 | + * the appends sit beside the page, not inside it. They now take the same |
| 18 | + * horizontal inset. |
| 19 | + * |
| 20 | + * Mounted through `RecordDetailView` itself on an ASSIGNED record page, so the |
| 21 | + * inset is read off the page root the real page renderer draws, not copied. |
| 22 | + */ |
| 23 | +import * as React from 'react'; |
| 24 | +import { describe, it, expect, vi, beforeEach, afterEach, beforeAll } from 'vitest'; |
| 25 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
| 26 | +import { MemoryRouter } from 'react-router-dom'; |
| 27 | +import { MetadataCtx } from '@object-ui/react'; |
| 28 | + |
| 29 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 30 | + ...(await importOriginal<Record<string, unknown>>()), |
| 31 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }), |
| 32 | + createAuthenticatedFetch: () => vi.fn(), |
| 33 | +})); |
| 34 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 35 | + ...(await importOriginal<Record<string, unknown>>()), |
| 36 | + useRecordPresence: () => [], |
| 37 | + PresenceAvatars: () => null, |
| 38 | +})); |
| 39 | +vi.mock('sonner', () => ({ |
| 40 | + toast: Object.assign(vi.fn(), { |
| 41 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 42 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 43 | + }), |
| 44 | +})); |
| 45 | +// Orthogonal chrome, stubbed. |
| 46 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 47 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 48 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 49 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 50 | +vi.mock('./MetadataInspector', () => ({ |
| 51 | + MetadataPanel: () => null, |
| 52 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 53 | +})); |
| 54 | + |
| 55 | +import { RecordDetailView } from './RecordDetailView'; |
| 56 | + |
| 57 | +const OBJECT = 'os_27520_project'; |
| 58 | +const REC = 'prj-1'; |
| 59 | + |
| 60 | +const objectDef = { |
| 61 | + name: OBJECT, |
| 62 | + label: 'Project', |
| 63 | + enable: { files: true }, |
| 64 | + highlightFields: ['name'], |
| 65 | + fields: { |
| 66 | + id: { label: 'Id', type: 'text' }, |
| 67 | + name: { label: 'Name', type: 'text' }, |
| 68 | + owner_email: { label: 'Owner Email', type: 'text' }, |
| 69 | + }, |
| 70 | +}; |
| 71 | + |
| 72 | +const RECORD = { |
| 73 | + id: REC, |
| 74 | + name: 'Website Relaunch', |
| 75 | + owner_email: 'auditor@example.com', |
| 76 | + created_at: '2026-01-02T03:04:05.000Z', |
| 77 | + updated_at: '2026-02-03T04:05:06.000Z', |
| 78 | +}; |
| 79 | + |
| 80 | +/** An authored page that places no `record:attachments`, so the host appends the panel. */ |
| 81 | +const ASSIGNED_PAGE = { |
| 82 | + name: 'os_27520_project_detail', |
| 83 | + label: 'Project', |
| 84 | + type: 'record', |
| 85 | + object: OBJECT, |
| 86 | + regions: [ |
| 87 | + { |
| 88 | + name: 'main', |
| 89 | + components: [ |
| 90 | + { type: 'record:details', id: 'overview', properties: { sections: [{ label: 'Overview', fields: ['owner_email'] }] } }, |
| 91 | + ], |
| 92 | + }, |
| 93 | + ], |
| 94 | +}; |
| 95 | + |
| 96 | +function makeDataSource() { |
| 97 | + return { |
| 98 | + find: vi.fn(async () => ({ data: [], total: 0, hasMore: false, pageSize: 50 })), |
| 99 | + findOne: vi.fn(async (name: string, id: string) => (name === OBJECT ? { ...RECORD, id } : null)), |
| 100 | + create: vi.fn(async (_o: string, row: any) => row), |
| 101 | + update: vi.fn(async () => ({})), |
| 102 | + delete: vi.fn(async () => ({})), |
| 103 | + getObjectSchema: async (name: string) => ({ name, fields: objectDef.fields }), |
| 104 | + } as any; |
| 105 | +} |
| 106 | + |
| 107 | +function makeMetadata(pages: any[]) { |
| 108 | + return { |
| 109 | + objects: [], pages, loading: false, error: null, |
| 110 | + refresh: async () => {}, invalidate: () => {}, |
| 111 | + ensureType: async () => pages, getItem: async () => null, |
| 112 | + getItemsByType: () => pages, |
| 113 | + } as any; |
| 114 | +} |
| 115 | + |
| 116 | +function renderRecordPage(pages: any[]): HTMLElement { |
| 117 | + const { container } = render( |
| 118 | + <MemoryRouter initialEntries={[`/app/demo/${OBJECT}/${REC}`]}> |
| 119 | + <MetadataCtx.Provider value={makeMetadata(pages)}> |
| 120 | + <RecordDetailView |
| 121 | + dataSource={makeDataSource()} |
| 122 | + objects={[objectDef]} |
| 123 | + onEdit={() => {}} |
| 124 | + objectNameOverride={OBJECT} |
| 125 | + recordIdOverride={REC} |
| 126 | + embedded |
| 127 | + /> |
| 128 | + </MetadataCtx.Provider> |
| 129 | + </MemoryRouter>, |
| 130 | + ); |
| 131 | + return container; |
| 132 | +} |
| 133 | + |
| 134 | +/** The page root's padding utilities, each as the horizontal inset an append must carry. */ |
| 135 | +function pageInsetOf(root: ParentNode): string[] { |
| 136 | + const page = root.querySelector('[data-page-type="record"]'); |
| 137 | + expect(page, 'the page renderer must draw the record page root').toBeTruthy(); |
| 138 | + const padding = Array.from(page!.classList).filter((c) => /^(?:[a-z]+:)?p-\d+$/.test(c)); |
| 139 | + // Control: the page does inset its body; an inset-free page would make |
| 140 | + // every assertion below vacuous. |
| 141 | + expect(padding.length).toBeGreaterThan(0); |
| 142 | + return padding.map((c) => c.replace(/p-(\d+)$/, 'px-$1')); |
| 143 | +} |
| 144 | + |
| 145 | +beforeAll(() => { |
| 146 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 147 | +}); |
| 148 | + |
| 149 | +beforeEach(() => { |
| 150 | + cleanup(); |
| 151 | + vi.stubGlobal('fetch', vi.fn(async () => |
| 152 | + new Response(JSON.stringify({ data: [] }), { |
| 153 | + status: 200, headers: { 'content-type': 'application/json' }, |
| 154 | + }))); |
| 155 | +}); |
| 156 | + |
| 157 | +afterEach(() => { vi.unstubAllGlobals(); vi.clearAllMocks(); }); |
| 158 | + |
| 159 | +describe('record page appends line up with the page cards (objectstack#27520 row 14)', () => { |
| 160 | + it('the appended Attachments panel takes the page body inset', async () => { |
| 161 | + const container = renderRecordPage([ASSIGNED_PAGE]); |
| 162 | + await waitFor(() => expect(container.textContent).toContain('auditor@example.com')); |
| 163 | + const fallback = await waitFor(() => { |
| 164 | + const el = container.querySelector('[data-record-attachments-fallback]'); |
| 165 | + expect(el, 'the host appends the Attachments panel for an authored page that omits it').toBeTruthy(); |
| 166 | + return el as HTMLElement; |
| 167 | + }); |
| 168 | + const inset = pageInsetOf(container); |
| 169 | + for (const cls of inset) expect(fallback.classList).toContain(cls); |
| 170 | + // It is an append, not part of the page tree. |
| 171 | + expect(container.querySelector('[data-page-type="record"]')!.contains(fallback)).toBe(false); |
| 172 | + }); |
| 173 | + |
| 174 | + it('the provenance footer takes the same inset', async () => { |
| 175 | + const container = renderRecordPage([ASSIGNED_PAGE]); |
| 176 | + const footer = await waitFor(() => { |
| 177 | + const el = container.querySelector('[data-testid="record-meta-footer"]'); |
| 178 | + expect(el).toBeTruthy(); |
| 179 | + return el as HTMLElement; |
| 180 | + }); |
| 181 | + for (const cls of pageInsetOf(container)) expect(footer.classList).toContain(cls); |
| 182 | + }); |
| 183 | +}); |
0 commit comments