|
| 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, Targets 8 and 9 — the record's tab row drives the |
| 11 | + * address, through a real `RecordDetailView` mount and the browser's own |
| 12 | + * history: |
| 13 | + * |
| 14 | + * - leaving 「概览」 for a tab PUSHES one entry, `?related=object.field` |
| 15 | + * (`?related=timeline` for 动态); |
| 16 | + * - moving from tab to tab REPLACES that entry, so the history grows by one |
| 17 | + * however many tabs the reader visits; |
| 18 | + * - 「概览」, Esc and the browser's Back each return to the page in one step, |
| 19 | + * and the header with its tab row stays drawn on every face. |
| 20 | + */ |
| 21 | + |
| 22 | +import * as React from 'react'; |
| 23 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 24 | +import { render, screen, cleanup, waitFor, fireEvent, act } from '@testing-library/react'; |
| 25 | +import { BrowserRouter } from 'react-router-dom'; |
| 26 | +import { MetadataCtx } from '@object-ui/react'; |
| 27 | +import { RelatedCountStore } from '@object-ui/components'; |
| 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 | + |
| 35 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 36 | + ...(await importOriginal<Record<string, unknown>>()), |
| 37 | + useRecordPresence: () => [], |
| 38 | + PresenceAvatars: () => null, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('sonner', () => ({ |
| 42 | + toast: Object.assign(vi.fn(), { |
| 43 | + success: vi.fn(), |
| 44 | + error: vi.fn(), |
| 45 | + info: vi.fn(), |
| 46 | + warning: vi.fn(), |
| 47 | + loading: vi.fn(), |
| 48 | + dismiss: vi.fn(), |
| 49 | + }), |
| 50 | +})); |
| 51 | + |
| 52 | +// Orthogonal chrome — stubbed so the only asynchrony here is the page's own. |
| 53 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 54 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 55 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 56 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 57 | +vi.mock('./MetadataInspector', () => ({ |
| 58 | + MetadataPanel: () => null, |
| 59 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 60 | +})); |
| 61 | + |
| 62 | +import { RecordDetailView } from './RecordDetailView'; |
| 63 | + |
| 64 | +const PARENT = 'showcase_project'; |
| 65 | +const RECORD_ID = 'p-1'; |
| 66 | + |
| 67 | +const BASE_FIELDS = { |
| 68 | + id: { type: 'text', label: 'Id' }, |
| 69 | + name: { type: 'text', label: 'Name' }, |
| 70 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * The project's own text fields, each with a value: more than the header's |
| 74 | + * highlights take, so the details section holds enough of them (more than |
| 75 | + * five) for the wide tier's three-column grid. |
| 76 | + */ |
| 77 | +const PROJECT_TEXT = ['code', 'region', 'sponsor', 'phase', 'cost_center', 'tier', 'site', 'channel', 'segment', 'program', 'quarter', 'team']; |
| 78 | + |
| 79 | +const OBJECTS = [ |
| 80 | + { |
| 81 | + name: PARENT, |
| 82 | + label: 'Project', |
| 83 | + pluralLabel: 'Projects', |
| 84 | + managedBy: 'platform', |
| 85 | + // History is on for this object, so the synthesized page carries it. |
| 86 | + enable: { trackHistory: true }, |
| 87 | + // A worked object — its status options declare lifecycle kinds. Since |
| 88 | + // objectstack#27524 that no longer changes the page: every object's page |
| 89 | + // has the same two columns. |
| 90 | + fields: { |
| 91 | + ...BASE_FIELDS, |
| 92 | + ...Object.fromEntries(PROJECT_TEXT.map((name) => [name, { type: 'text', label: name }])), |
| 93 | + status: { |
| 94 | + type: 'select', |
| 95 | + label: 'Status', |
| 96 | + options: [ |
| 97 | + { value: 'active', label: 'Active', kind: 'open' }, |
| 98 | + { value: 'done', label: 'Done', kind: 'won' }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: 'showcase_task', |
| 105 | + label: 'Task', |
| 106 | + pluralLabel: 'Tasks', |
| 107 | + managedBy: 'platform', |
| 108 | + fields: { ...BASE_FIELDS, project: { type: 'master_detail', reference: PARENT, label: 'Project' } }, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: 'showcase_note', |
| 112 | + label: 'Note', |
| 113 | + pluralLabel: 'Notes', |
| 114 | + managedBy: 'platform', |
| 115 | + fields: { ...BASE_FIELDS, project: { type: 'lookup', reference: PARENT, label: 'Project' } }, |
| 116 | + }, |
| 117 | + { name: 'sys_audit_log', label: 'Audit log', managedBy: 'platform', fields: BASE_FIELDS }, |
| 118 | +]; |
| 119 | + |
| 120 | +/** The width this view's root measures; every other element measures as the environment does. */ |
| 121 | +let rootWidth = 0; |
| 122 | +let dataSource: { find: ReturnType<typeof vi.fn> } & Record<string, unknown>; |
| 123 | + |
| 124 | +function makeDataSource() { |
| 125 | + return { |
| 126 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 127 | + create: vi.fn(async (_o: string, row: unknown) => row), |
| 128 | + findOne: vi.fn(async (_o: string, recordId: string) => ({ |
| 129 | + id: recordId, |
| 130 | + name: `Project ${recordId}`, |
| 131 | + ...Object.fromEntries(PROJECT_TEXT.map((name) => [name, `${name} value`])), |
| 132 | + status: 'active', |
| 133 | + })), |
| 134 | + update: vi.fn(async () => ({})), |
| 135 | + delete: vi.fn(async () => ({})), |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +function renderPage({ width, embedded = false, search = '' }: { width: number; embedded?: boolean; search?: string }) { |
| 140 | + rootWidth = width; |
| 141 | + // The real browser history: the view reads the address bar it writes. |
| 142 | + window.history.replaceState(null, '', `/app/demo/${PARENT}/${RECORD_ID}${search}`); |
| 143 | + dataSource = makeDataSource(); |
| 144 | + const metadata = { |
| 145 | + objects: OBJECTS, |
| 146 | + pages: [], |
| 147 | + loading: false, |
| 148 | + error: null, |
| 149 | + refresh: async () => {}, |
| 150 | + invalidate: () => {}, |
| 151 | + ensureType: async () => [], |
| 152 | + getItem: async () => null, |
| 153 | + getItemsByType: () => [], |
| 154 | + } as never; |
| 155 | + return render( |
| 156 | + <BrowserRouter> |
| 157 | + <MetadataCtx.Provider value={metadata}> |
| 158 | + <RecordDetailView |
| 159 | + dataSource={dataSource as never} |
| 160 | + objects={OBJECTS} |
| 161 | + onEdit={() => {}} |
| 162 | + objectNameOverride={PARENT} |
| 163 | + recordIdOverride={RECORD_ID} |
| 164 | + embedded={embedded} |
| 165 | + /> |
| 166 | + </MetadataCtx.Provider> |
| 167 | + </BrowserRouter>, |
| 168 | + ); |
| 169 | +} |
| 170 | + |
| 171 | +const tierOf = (container: HTMLElement) => |
| 172 | + container.querySelector('[data-record-page-tier]')?.getAttribute('data-record-page-tier') ?? null; |
| 173 | +const column = (container: HTMLElement, name: string) => |
| 174 | + container.querySelector(`[data-record-column="${name}"]`) as HTMLElement | null; |
| 175 | +const auditReads = () => dataSource.find.mock.calls.filter((call) => call[0] === 'sys_audit_log').length; |
| 176 | + |
| 177 | +beforeEach(() => { |
| 178 | + cleanup(); |
| 179 | + RelatedCountStore._reset(); |
| 180 | + vi.stubGlobal( |
| 181 | + 'fetch', |
| 182 | + vi.fn(async () => |
| 183 | + new Response(JSON.stringify({ data: [] }), { |
| 184 | + status: 200, |
| 185 | + headers: { 'content-type': 'application/json' }, |
| 186 | + }), |
| 187 | + ), |
| 188 | + ); |
| 189 | + const measure = HTMLElement.prototype.getBoundingClientRect; |
| 190 | + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) { |
| 191 | + if (!this.hasAttribute('data-record-page-root')) return measure.call(this); |
| 192 | + return { x: 0, y: 0, top: 0, left: 0, right: rootWidth, bottom: 800, width: rootWidth, height: 800, toJSON: () => ({}) } as DOMRect; |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
| 196 | +afterEach(() => { |
| 197 | + vi.unstubAllGlobals(); |
| 198 | + vi.restoreAllMocks(); |
| 199 | +}); |
| 200 | + |
| 201 | +const search = () => new URLSearchParams(window.location.search).get('related'); |
| 202 | +const tab = (id: string) => document.querySelector(`[role="tab"][data-record-tab="${id}"]`) as HTMLElement; |
| 203 | +const activeTab = () => document.querySelector('[role="tab"][aria-selected="true"]')?.getAttribute('data-record-tab') ?? null; |
| 204 | +const face = () => document.querySelector('[data-related-enlarged]'); |
| 205 | + |
| 206 | +describe('RecordDetailView — the tab row drives the address (objectstack#27524, Targets 8 and 9)', () => { |
| 207 | + it('a tab pushes one entry, the next tab replaces it, 「概览」 returns in one step', async () => { |
| 208 | + renderPage({ width: 1440 }); |
| 209 | + await screen.findAllByText('Tasks', undefined, { timeout: 5000 }); |
| 210 | + const start = window.history.length; |
| 211 | + expect(activeTab()).toBe('overview'); |
| 212 | + |
| 213 | + fireEvent.click(tab('showcase_task.project')); |
| 214 | + await waitFor(() => expect(face()?.getAttribute('data-related-enlarged')).toBe('showcase_task.project')); |
| 215 | + expect(search()).toBe('showcase_task.project'); |
| 216 | + expect(window.history.length).toBe(start + 1); |
| 217 | + expect(activeTab()).toBe('showcase_task.project'); |
| 218 | + // The header is drawn once, above the row, on the enlarged face too. |
| 219 | + expect(document.querySelectorAll('h1')).toHaveLength(1); |
| 220 | + |
| 221 | + fireEvent.click(tab('timeline')); |
| 222 | + await waitFor(() => expect(face()?.getAttribute('data-related-enlarged')).toBe('timeline')); |
| 223 | + expect(search()).toBe('timeline'); |
| 224 | + fireEvent.click(tab('showcase_note.project')); |
| 225 | + await waitFor(() => expect(face()?.getAttribute('data-related-enlarged')).toBe('showcase_note.project')); |
| 226 | + expect(search()).toBe('showcase_note.project'); |
| 227 | + // Three tabs visited, one entry. |
| 228 | + expect(window.history.length).toBe(start + 1); |
| 229 | + |
| 230 | + fireEvent.click(tab('overview')); |
| 231 | + await waitFor(() => expect(face()).toBeNull()); |
| 232 | + await waitFor(() => expect(search()).toBeNull()); |
| 233 | + expect(activeTab()).toBe('overview'); |
| 234 | + expect(document.querySelector('[data-record-columns]')).not.toBeNull(); |
| 235 | + }); |
| 236 | + |
| 237 | + it('Esc on a tab returns to 「概览」, and so does the browser\'s Back', async () => { |
| 238 | + renderPage({ width: 720 }); |
| 239 | + await screen.findAllByText('Tasks', undefined, { timeout: 5000 }); |
| 240 | + fireEvent.click(tab('showcase_task.project')); |
| 241 | + fireEvent.click(await waitFor(() => tab('showcase_note.project'))); |
| 242 | + await waitFor(() => expect(search()).toBe('showcase_note.project')); |
| 243 | + fireEvent.keyDown(face()!, { key: 'Escape' }); |
| 244 | + await waitFor(() => expect(search()).toBeNull()); |
| 245 | + await waitFor(() => expect(face()).toBeNull()); |
| 246 | + |
| 247 | + fireEvent.click(tab('timeline')); |
| 248 | + await waitFor(() => expect(search()).toBe('timeline')); |
| 249 | + act(() => window.history.back()); |
| 250 | + await waitFor(() => expect(search()).toBeNull()); |
| 251 | + await waitFor(() => expect(face()).toBeNull()); |
| 252 | + expect(activeTab()).toBe('overview'); |
| 253 | + }); |
| 254 | + |
| 255 | + it('a link to a tab opens it as the active tab; 「概览」 then replaces the address, with nothing to pop', async () => { |
| 256 | + renderPage({ width: 1440, search: '?related=showcase_task.project' }); |
| 257 | + await waitFor(() => expect(face()?.getAttribute('data-related-enlarged')).toBe('showcase_task.project'), { timeout: 5000 }); |
| 258 | + expect(activeTab()).toBe('showcase_task.project'); |
| 259 | + const start = window.history.length; |
| 260 | + fireEvent.click(tab('overview')); |
| 261 | + await waitFor(() => expect(search()).toBeNull()); |
| 262 | + expect(window.history.length).toBe(start); |
| 263 | + expect(window.location.pathname).toBe(`/app/demo/${PARENT}/${RECORD_ID}`); |
| 264 | + }); |
| 265 | +}); |
0 commit comments