|
| 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#27524, Target 7 — the record's top bar on each face, from a real |
| 11 | + * `RecordDetailView` mount (the `panelToggles-27052` harness). |
| 12 | + * |
| 13 | + * - the page opened directly: the breadcrumb 「对象 › 记录名」 (the object goes |
| 14 | + * to the object's list), ⊟ where the tier has a right column, nothing else |
| 15 | + * — no counter, no ↓ ↑, no ⤡; the crumb row is gone; |
| 16 | + * - the page the list row's ↗ opened: the counter (its tooltip naming the |
| 17 | + * list's view), ↓ ↑ and `j` / `k` over the list's rows — a step replaces the |
| 18 | + * page with the sibling's — ⤡ 「Collapse to drawer」 back to the list with |
| 19 | + * the record stepped to open, and the object crumb back to the list; |
| 20 | + * - the page under a `?from=` trail: ← and the path, each ancestor a link; |
| 21 | + * - the record in a pane: the pane's bar names it at the root, and its ⊟ sits |
| 22 | + * in that bar, not in the header's action row. |
| 23 | + */ |
| 24 | + |
| 25 | +import * as React from 'react'; |
| 26 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 27 | +import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react'; |
| 28 | +import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'; |
| 29 | +import { MetadataCtx } from '@object-ui/react'; |
| 30 | +import { RelatedCountStore } from '@object-ui/components'; |
| 31 | + |
| 32 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<Record<string, unknown>>()), |
| 34 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }), |
| 35 | + createAuthenticatedFetch: () => vi.fn(), |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 39 | + ...(await importOriginal<Record<string, unknown>>()), |
| 40 | + useRecordPresence: () => [], |
| 41 | + PresenceAvatars: () => null, |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock('sonner', () => ({ |
| 45 | + toast: Object.assign(vi.fn(), { |
| 46 | + success: vi.fn(), |
| 47 | + error: vi.fn(), |
| 48 | + info: vi.fn(), |
| 49 | + warning: vi.fn(), |
| 50 | + loading: vi.fn(), |
| 51 | + dismiss: vi.fn(), |
| 52 | + }), |
| 53 | +})); |
| 54 | + |
| 55 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 56 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 57 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 58 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 59 | +vi.mock('./MetadataInspector', () => ({ |
| 60 | + MetadataPanel: () => null, |
| 61 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 62 | +})); |
| 63 | + |
| 64 | +import { RecordDetailView } from './RecordDetailView'; |
| 65 | +import { RecordStackDrawer, recordDrawerReturnState, useRecordDrawerStack } from './recordDrawerStack'; |
| 66 | + |
| 67 | +const APP = 'demo'; |
| 68 | +const LEAD = 'crm_lead'; |
| 69 | +const BASE_FIELDS = { id: { type: 'text', label: 'Id' }, name: { type: 'text', label: 'Name' } }; |
| 70 | +const OBJECTS = [ |
| 71 | + { name: LEAD, label: 'Lead', pluralLabel: 'Leads', fields: BASE_FIELDS }, |
| 72 | + { name: 'crm_task', label: 'Task', pluralLabel: 'Tasks', fields: { ...BASE_FIELDS, lead: { type: 'lookup', reference: LEAD, label: 'Lead' } } }, |
| 73 | +]; |
| 74 | +const ROWS = ['r-1', 'r-2', 'r-3']; |
| 75 | + |
| 76 | +let rootWidth = 1440; |
| 77 | + |
| 78 | +function makeDataSource() { |
| 79 | + return { |
| 80 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 81 | + create: vi.fn(async (_o: string, row: unknown) => row), |
| 82 | + findOne: vi.fn(async (_o: string, recordId: string) => ({ id: recordId, name: `Record ${recordId}` })), |
| 83 | + update: vi.fn(async () => ({})), |
| 84 | + delete: vi.fn(async () => ({})), |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +const metadata = { |
| 89 | + objects: OBJECTS, |
| 90 | + pages: [], |
| 91 | + loading: false, |
| 92 | + error: null, |
| 93 | + refresh: async () => {}, |
| 94 | + invalidate: () => {}, |
| 95 | + ensureType: async () => [], |
| 96 | + getItem: async () => null, |
| 97 | + getItemsByType: () => [], |
| 98 | +} as never; |
| 99 | + |
| 100 | +function Where() { |
| 101 | + const location = useLocation(); |
| 102 | + return ( |
| 103 | + <> |
| 104 | + <output data-testid="where">{`${location.pathname}${location.search}`}</output> |
| 105 | + <output data-testid="where-state">{JSON.stringify(location.state ?? null)}</output> |
| 106 | + </> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +function mountPage(entry: string | { pathname: string; search?: string; state?: unknown }) { |
| 111 | + const dataSource = makeDataSource(); |
| 112 | + render( |
| 113 | + <MemoryRouter initialEntries={[entry as never]}> |
| 114 | + <MetadataCtx.Provider value={metadata}> |
| 115 | + <Routes> |
| 116 | + <Route |
| 117 | + path="/apps/:appName/:objectName/record/:recordId" |
| 118 | + element={<RecordDetailView dataSource={dataSource as never} objects={OBJECTS} onEdit={() => {}} />} |
| 119 | + /> |
| 120 | + <Route path="/apps/:appName/:objectName" element={<div data-testid="list" />} /> |
| 121 | + </Routes> |
| 122 | + <Where /> |
| 123 | + </MetadataCtx.Provider> |
| 124 | + </MemoryRouter>, |
| 125 | + ); |
| 126 | + return dataSource; |
| 127 | +} |
| 128 | + |
| 129 | +const where = () => screen.getByTestId('where').textContent; |
| 130 | +const pageBar = () => document.querySelector('[data-record-page-root] > [data-record-top-bar]') as HTMLElement | null; |
| 131 | + |
| 132 | +beforeEach(() => { |
| 133 | + cleanup(); |
| 134 | + rootWidth = 1440; |
| 135 | + window.localStorage.clear(); |
| 136 | + RelatedCountStore._reset(); |
| 137 | + vi.stubGlobal( |
| 138 | + 'fetch', |
| 139 | + vi.fn(async () => new Response(JSON.stringify({ data: [] }), { status: 200, headers: { 'content-type': 'application/json' } })), |
| 140 | + ); |
| 141 | + const measure = HTMLElement.prototype.getBoundingClientRect; |
| 142 | + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) { |
| 143 | + if (!this.hasAttribute('data-record-page-root')) return measure.call(this); |
| 144 | + const width = rootWidth; |
| 145 | + return { x: 0, y: 0, top: 0, left: 0, right: width, bottom: 800, width, height: 800, toJSON: () => ({}) } as DOMRect; |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +afterEach(() => { |
| 150 | + vi.unstubAllGlobals(); |
| 151 | + vi.restoreAllMocks(); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('the page\'s top bar (objectstack#27524, Target 7)', () => { |
| 155 | + it('opened directly: 「对象 › 记录名」 and ⊟, nothing else; the crumb row is gone', async () => { |
| 156 | + mountPage(`/apps/${APP}/${LEAD}/record/r-1`); |
| 157 | + await screen.findByRole('group', { name: 'Panels' }, { timeout: 5000 }); |
| 158 | + const bar = pageBar()!; |
| 159 | + expect(bar).not.toBeNull(); |
| 160 | + await waitFor(() => expect(screen.getByTestId('record-page-crumb').textContent).toBe('LeadsRecord r-1')); |
| 161 | + const object = bar.querySelector('[data-record-crumb-object]') as HTMLAnchorElement; |
| 162 | + expect(object.getAttribute('href')).toBe(`/apps/${APP}/${LEAD}`); |
| 163 | + expect(bar.querySelector('[data-record-top-bar-step]')).toBeNull(); |
| 164 | + expect(bar.querySelector('[data-record-top-bar-position]')).toBeNull(); |
| 165 | + expect(screen.queryByTestId('record-drawer-return')).toBeNull(); |
| 166 | + expect(bar.querySelector('[role="group"][aria-label="Panels"]')).not.toBeNull(); |
| 167 | + expect(document.querySelector('[data-record-crumb-row]')).toBeNull(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('opened from the list row\'s ↗: the counter, ↓ ↑ and j / k over the list\'s rows, ⤡ and the object crumb back to the list', async () => { |
| 171 | + const back = recordDrawerReturnState( |
| 172 | + { pathname: `/apps/${APP}/${LEAD}`, search: '?recordId=r-2', state: null }, |
| 173 | + [{ objectName: LEAD, recordId: 'r-2', siblings: ROWS }], |
| 174 | + ); |
| 175 | + mountPage({ |
| 176 | + pathname: `/apps/${APP}/${LEAD}/record/r-2`, |
| 177 | + state: { ...back, from: { pathname: `/apps/${APP}/${LEAD}`, label: 'All leads' } }, |
| 178 | + }); |
| 179 | + await screen.findByRole('group', { name: 'Panels' }, { timeout: 5000 }); |
| 180 | + const position = () => pageBar()!.querySelector('[data-record-top-bar-position]'); |
| 181 | + expect(position()?.textContent).toBe('2 / 3'); |
| 182 | + expect(position()?.getAttribute('title')).toBe('2 of 3 in All leads'); |
| 183 | + // ⊟ after the step group, ⤡ at the end. |
| 184 | + const controls = Array.from(pageBar()!.querySelector('[data-record-top-bar-controls]')!.children); |
| 185 | + expect(controls.at(-1)?.getAttribute('data-testid')).toBe('record-drawer-return'); |
| 186 | + |
| 187 | + fireEvent.click(screen.getByRole('button', { name: 'Next record' })); |
| 188 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}/record/r-3`)); |
| 189 | + await waitFor(() => expect(position()?.textContent).toBe('3 / 3')); |
| 190 | + expect((screen.getByRole('button', { name: 'Next record' }) as HTMLButtonElement).disabled).toBe(true); |
| 191 | + // `k` steps back; the arrow keys are the page's scroll, not the bar's. |
| 192 | + fireEvent.keyDown(document.body, { key: 'ArrowUp' }); |
| 193 | + expect(where()).toBe(`/apps/${APP}/${LEAD}/record/r-3`); |
| 194 | + fireEvent.keyDown(document.body, { key: 'k' }); |
| 195 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}/record/r-2`)); |
| 196 | + |
| 197 | + // ⤡ returns to the list with the record stepped to open in its pane. |
| 198 | + fireEvent.click(screen.getByTestId('record-drawer-return')); |
| 199 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}?recordId=r-2`)); |
| 200 | + expect(screen.getByTestId('where-state').textContent).toContain('"recordDrawerRestore":[{"objectName":"crm_lead","recordId":"r-2"'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('the object crumb of a ↗ page returns to the list with no record open', async () => { |
| 204 | + const back = recordDrawerReturnState( |
| 205 | + { pathname: `/apps/${APP}/${LEAD}`, search: '?view=all&recordId=r-1', state: null }, |
| 206 | + [{ objectName: LEAD, recordId: 'r-1', siblings: ROWS }], |
| 207 | + ); |
| 208 | + mountPage({ pathname: `/apps/${APP}/${LEAD}/record/r-1`, state: { ...back, from: { pathname: `/apps/${APP}/${LEAD}?view=all`, label: 'All leads' } } }); |
| 209 | + const object = await waitFor(() => { |
| 210 | + const el = pageBar()?.querySelector('[data-record-crumb-object]'); |
| 211 | + expect(el).toBeTruthy(); |
| 212 | + return el as HTMLElement; |
| 213 | + }); |
| 214 | + expect(object.textContent).toBe('Leads'); |
| 215 | + fireEvent.click(object); |
| 216 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}?view=all`)); |
| 217 | + }); |
| 218 | + |
| 219 | + it('under a ?from= trail: ← and the path, each ancestor a link to its page', async () => { |
| 220 | + const trail = encodeURIComponent(JSON.stringify([{ o: LEAD, i: 'L-1', t: 'Mira Costa' }])); |
| 221 | + mountPage(`/apps/${APP}/crm_task/record/t-1?from=${trail}`); |
| 222 | + const path = await screen.findByTestId('record-page-trail', undefined, { timeout: 5000 }); |
| 223 | + expect(path.textContent).toBe('Lead Mira CostaTask'); |
| 224 | + const ancestor = path.querySelector('a')!; |
| 225 | + expect(ancestor.getAttribute('href')).toBe(`/apps/${APP}/${LEAD}/record/L-1`); |
| 226 | + const backLink = pageBar()!.querySelector('[data-record-crumb-back]')!; |
| 227 | + expect(backLink.getAttribute('aria-label')).toBe('Go back'); |
| 228 | + expect(screen.queryByTestId('record-page-crumb')).toBeNull(); |
| 229 | + }); |
| 230 | +}); |
| 231 | + |
| 232 | +/** The list host's pane, drawing the record embedded (`objectViewRecordOverlay.tsx`'s shape). */ |
| 233 | +function PaneHost({ dataSource }: { dataSource: unknown }) { |
| 234 | + const [recordId, setRecordId] = React.useState('r-1'); |
| 235 | + const root = React.useMemo(() => ({ objectName: LEAD, recordId, siblings: ROWS }), [recordId]); |
| 236 | + const stack = useRecordDrawerStack({ root, onRootStep: setRecordId }); |
| 237 | + return ( |
| 238 | + <RecordStackDrawer |
| 239 | + stack={stack} |
| 240 | + open |
| 241 | + mode="split" |
| 242 | + mainContent={<div data-testid="list" />} |
| 243 | + objects={OBJECTS} |
| 244 | + stepScope="All leads" |
| 245 | + onClose={() => {}} |
| 246 | + renderRecord={(level) => ( |
| 247 | + <RecordDetailView |
| 248 | + dataSource={dataSource as never} |
| 249 | + objects={OBJECTS} |
| 250 | + onEdit={() => {}} |
| 251 | + objectNameOverride={level.objectName} |
| 252 | + recordIdOverride={level.recordId} |
| 253 | + embedded |
| 254 | + /> |
| 255 | + )} |
| 256 | + /> |
| 257 | + ); |
| 258 | +} |
| 259 | + |
| 260 | +describe('the pane\'s top bar holds the record\'s ⊟ and names it (objectstack#27524)', () => { |
| 261 | + it('the record reports its title to the breadcrumb, and its ⊟ sits in the pane\'s bar, not the header', async () => { |
| 262 | + const dataSource = makeDataSource(); |
| 263 | + render( |
| 264 | + <MemoryRouter initialEntries={[`/apps/${APP}/${LEAD}?recordId=r-1`]}> |
| 265 | + <MetadataCtx.Provider value={metadata}> |
| 266 | + <PaneHost dataSource={dataSource} /> |
| 267 | + </MetadataCtx.Provider> |
| 268 | + </MemoryRouter>, |
| 269 | + ); |
| 270 | + await waitFor(() => expect(screen.getByTestId('record-drawer-crumb').textContent).toBe('LeadsRecord r-1'), { timeout: 5000 }); |
| 271 | + const group = await screen.findByRole('group', { name: 'Panels' }, { timeout: 5000 }); |
| 272 | + expect(group.closest('[data-record-pane-toggles]')).not.toBeNull(); |
| 273 | + expect(group.closest('[data-record-top-bar]')).not.toBeNull(); |
| 274 | + expect(group.closest('[data-record-page-root]')).toBeNull(); |
| 275 | + expect(document.querySelector('[data-page-header-trailing]')).toBeNull(); |
| 276 | + // The pane draws its own bar; the record inside draws none. |
| 277 | + expect(document.querySelectorAll('[data-record-top-bar]')).toHaveLength(1); |
| 278 | + expect(document.querySelector('[data-record-top-bar-position]')?.getAttribute('title')).toBe('1 of 3 in All leads'); |
| 279 | + |
| 280 | + // A step names the next record once it reports its title. |
| 281 | + fireEvent.click(screen.getByRole('button', { name: 'Next record' })); |
| 282 | + await waitFor(() => expect(screen.getByTestId('record-drawer-crumb').textContent).toBe('LeadsRecord r-2'), { timeout: 5000 }); |
| 283 | + }); |
| 284 | +}); |
0 commit comments