|
| 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#26678 — the synthesized record page is one page at two widths. |
| 11 | + * |
| 12 | + * Measured from a real `RecordDetailView` mount (the `relatedListTitlePlural` |
| 13 | + * harness). The page takes its tier from the width of the container it |
| 14 | + * occupies — this view's root — and never from the viewport, so the root's |
| 15 | + * measured width is the one input varied here: every other element measures |
| 16 | + * as the test environment measures it. |
| 17 | + * |
| 18 | + * - 1440 px: three resident columns, no tab strip; the related lists sit in |
| 19 | + * the related column, and History is resident, so its read goes out at open. |
| 20 | + * - 1100 px: two columns and the related toggle; the related lists are not |
| 21 | + * mounted until the rail opens. |
| 22 | + * - 900 px, and an embedded view at any width: the tab strip as before. |
| 23 | + * - `?tab=` addresses the strip, so a column tier ignores it. |
| 24 | + */ |
| 25 | + |
| 26 | +import * as React from 'react'; |
| 27 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 28 | +import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react'; |
| 29 | +import { MemoryRouter } from 'react-router-dom'; |
| 30 | +import { MetadataCtx } from '@object-ui/react'; |
| 31 | +import { RelatedCountStore } from '@object-ui/components'; |
| 32 | + |
| 33 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 34 | + ...(await importOriginal<Record<string, unknown>>()), |
| 35 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }), |
| 36 | + createAuthenticatedFetch: () => vi.fn(), |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 40 | + ...(await importOriginal<Record<string, unknown>>()), |
| 41 | + useRecordPresence: () => [], |
| 42 | + PresenceAvatars: () => null, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock('sonner', () => ({ |
| 46 | + toast: Object.assign(vi.fn(), { |
| 47 | + success: vi.fn(), |
| 48 | + error: vi.fn(), |
| 49 | + info: vi.fn(), |
| 50 | + warning: vi.fn(), |
| 51 | + loading: vi.fn(), |
| 52 | + dismiss: vi.fn(), |
| 53 | + }), |
| 54 | +})); |
| 55 | + |
| 56 | +// Orthogonal chrome — stubbed so the only asynchrony here is the page's own. |
| 57 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 58 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 59 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 60 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 61 | +vi.mock('./MetadataInspector', () => ({ |
| 62 | + MetadataPanel: () => null, |
| 63 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 64 | +})); |
| 65 | + |
| 66 | +import { RecordDetailView } from './RecordDetailView'; |
| 67 | + |
| 68 | +const PARENT = 'showcase_project'; |
| 69 | +const RECORD_ID = 'p-1'; |
| 70 | + |
| 71 | +const BASE_FIELDS = { |
| 72 | + id: { type: 'text', label: 'Id' }, |
| 73 | + name: { type: 'text', label: 'Name' }, |
| 74 | +}; |
| 75 | + |
| 76 | +const OBJECTS = [ |
| 77 | + { |
| 78 | + name: PARENT, |
| 79 | + label: 'Project', |
| 80 | + pluralLabel: 'Projects', |
| 81 | + managedBy: 'platform', |
| 82 | + // History is on for this object, so the synthesized page carries it. |
| 83 | + enable: { trackHistory: true }, |
| 84 | + fields: BASE_FIELDS, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'showcase_task', |
| 88 | + label: 'Task', |
| 89 | + pluralLabel: 'Tasks', |
| 90 | + managedBy: 'platform', |
| 91 | + fields: { ...BASE_FIELDS, project: { type: 'master_detail', reference: PARENT, label: 'Project' } }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: 'showcase_note', |
| 95 | + label: 'Note', |
| 96 | + pluralLabel: 'Notes', |
| 97 | + managedBy: 'platform', |
| 98 | + fields: { ...BASE_FIELDS, project: { type: 'lookup', reference: PARENT, label: 'Project' } }, |
| 99 | + }, |
| 100 | + { name: 'sys_audit_log', label: 'Audit log', managedBy: 'platform', fields: BASE_FIELDS }, |
| 101 | +]; |
| 102 | + |
| 103 | +/** The width this view's root measures; every other element measures as the environment does. */ |
| 104 | +let rootWidth = 0; |
| 105 | +let dataSource: { find: ReturnType<typeof vi.fn> } & Record<string, unknown>; |
| 106 | + |
| 107 | +function makeDataSource() { |
| 108 | + return { |
| 109 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 110 | + create: vi.fn(async (_o: string, row: unknown) => row), |
| 111 | + findOne: vi.fn(async (_o: string, recordId: string) => ({ id: recordId, name: `Project ${recordId}` })), |
| 112 | + update: vi.fn(async () => ({})), |
| 113 | + delete: vi.fn(async () => ({})), |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +function renderPage({ width, embedded = false, search = '' }: { width: number; embedded?: boolean; search?: string }) { |
| 118 | + rootWidth = width; |
| 119 | + dataSource = makeDataSource(); |
| 120 | + const metadata = { |
| 121 | + objects: OBJECTS, |
| 122 | + pages: [], |
| 123 | + loading: false, |
| 124 | + error: null, |
| 125 | + refresh: async () => {}, |
| 126 | + invalidate: () => {}, |
| 127 | + ensureType: async () => [], |
| 128 | + getItem: async () => null, |
| 129 | + getItemsByType: () => [], |
| 130 | + } as never; |
| 131 | + return render( |
| 132 | + <MemoryRouter initialEntries={[`/app/demo/${PARENT}/${RECORD_ID}${search}`]}> |
| 133 | + <MetadataCtx.Provider value={metadata}> |
| 134 | + <RecordDetailView |
| 135 | + dataSource={dataSource as never} |
| 136 | + objects={OBJECTS} |
| 137 | + onEdit={() => {}} |
| 138 | + objectNameOverride={PARENT} |
| 139 | + recordIdOverride={RECORD_ID} |
| 140 | + embedded={embedded} |
| 141 | + /> |
| 142 | + </MetadataCtx.Provider> |
| 143 | + </MemoryRouter>, |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +const tierOf = (container: HTMLElement) => |
| 148 | + container.querySelector('[data-record-page-tier]')?.getAttribute('data-record-page-tier') ?? null; |
| 149 | +const column = (container: HTMLElement, name: string) => |
| 150 | + container.querySelector(`[data-record-column="${name}"]`) as HTMLElement | null; |
| 151 | +const auditReads = () => dataSource.find.mock.calls.filter((call) => call[0] === 'sys_audit_log').length; |
| 152 | + |
| 153 | +beforeEach(() => { |
| 154 | + cleanup(); |
| 155 | + RelatedCountStore._reset(); |
| 156 | + vi.stubGlobal( |
| 157 | + 'fetch', |
| 158 | + vi.fn(async () => |
| 159 | + new Response(JSON.stringify({ data: [] }), { |
| 160 | + status: 200, |
| 161 | + headers: { 'content-type': 'application/json' }, |
| 162 | + }), |
| 163 | + ), |
| 164 | + ); |
| 165 | + const measure = HTMLElement.prototype.getBoundingClientRect; |
| 166 | + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) { |
| 167 | + if (!this.hasAttribute('data-record-page-root')) return measure.call(this); |
| 168 | + return { x: 0, y: 0, top: 0, left: 0, right: rootWidth, bottom: 800, width: rootWidth, height: 800, toJSON: () => ({}) } as DOMRect; |
| 169 | + }); |
| 170 | +}); |
| 171 | + |
| 172 | +afterEach(() => { |
| 173 | + vi.unstubAllGlobals(); |
| 174 | + vi.restoreAllMocks(); |
| 175 | +}); |
| 176 | + |
| 177 | +describe('RecordDetailView — one record page at two widths (objectstack#26678)', () => { |
| 178 | + it('1440 px: three resident columns and no tab strip; History is read at open', async () => { |
| 179 | + const { container } = renderPage({ width: 1440 }); |
| 180 | + await screen.findAllByText('Tasks', undefined, { timeout: 5000 }); |
| 181 | + expect(tierOf(container)).toBe('three-column'); |
| 182 | + expect(screen.queryByRole('tablist')).toBeNull(); |
| 183 | + expect(column(container, 'details')).not.toBeNull(); |
| 184 | + expect(column(container, 'timeline')).not.toBeNull(); |
| 185 | + const related = column(container, 'related')!; |
| 186 | + expect(related.textContent).toContain('Tasks'); |
| 187 | + expect(related.textContent).toContain('Notes'); |
| 188 | + // The header is drawn once. |
| 189 | + expect(container.querySelectorAll('h1')).toHaveLength(1); |
| 190 | + await waitFor(() => expect(auditReads()).toBeGreaterThan(0)); |
| 191 | + }); |
| 192 | + |
| 193 | + it('1100 px: two columns and the related toggle; the lists mount when the rail opens', async () => { |
| 194 | + const { container } = renderPage({ width: 1100 }); |
| 195 | + const toggle = await screen.findByRole('button', { name: /Related/ }, { timeout: 5000 }); |
| 196 | + expect(tierOf(container)).toBe('two-column'); |
| 197 | + expect(screen.queryByRole('tablist')).toBeNull(); |
| 198 | + expect(column(container, 'details')).not.toBeNull(); |
| 199 | + expect(column(container, 'timeline')).not.toBeNull(); |
| 200 | + expect(column(container, 'related')).toBeNull(); |
| 201 | + expect(screen.queryByText('Tasks')).toBeNull(); |
| 202 | + fireEvent.click(toggle); |
| 203 | + expect(await screen.findAllByText('Tasks', undefined, { timeout: 5000 })).not.toHaveLength(0); |
| 204 | + expect(column(container, 'related')!.textContent).toContain('Notes'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('900 px: the tab strip, as before; History waits for its tab', async () => { |
| 208 | + const { container } = renderPage({ width: 900 }); |
| 209 | + expect(await screen.findByRole('tablist', undefined, { timeout: 5000 })).toBeTruthy(); |
| 210 | + expect(screen.getByRole('tab', { name: /^Details/ })).toBeTruthy(); |
| 211 | + expect(screen.getByRole('tab', { name: /^Related/ })).toBeTruthy(); |
| 212 | + expect(tierOf(container)).toBeNull(); |
| 213 | + expect(auditReads()).toBe(0); |
| 214 | + }); |
| 215 | + |
| 216 | + it('embedded (drawer / split pane): the tab strip at any width', async () => { |
| 217 | + const { container } = renderPage({ width: 1440, embedded: true }); |
| 218 | + expect(await screen.findByRole('tablist', undefined, { timeout: 5000 })).toBeTruthy(); |
| 219 | + expect(tierOf(container)).toBeNull(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('`?tab=` keeps working in the tabbed tier and is ignored in the column tiers', async () => { |
| 223 | + const tabbed = renderPage({ width: 900, search: '?tab=related' }); |
| 224 | + const relatedTab = await screen.findByRole('tab', { name: /^Related/ }, { timeout: 5000 }); |
| 225 | + expect(relatedTab.getAttribute('aria-selected')).toBe('true'); |
| 226 | + tabbed.unmount(); |
| 227 | + |
| 228 | + const { container } = renderPage({ width: 1440, search: '?tab=related' }); |
| 229 | + await screen.findAllByText('Tasks', undefined, { timeout: 5000 }); |
| 230 | + expect(tierOf(container)).toBe('three-column'); |
| 231 | + expect(screen.queryByRole('tablist')).toBeNull(); |
| 232 | + expect(column(container, 'details')).not.toBeNull(); |
| 233 | + }); |
| 234 | +}); |
0 commit comments