|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29074 — on a phone a list pushes a record page over itself, and |
| 5 | + * marks the push as a related row does (`recordPagePushState`). The record |
| 6 | + * page reads that marker on a phone (`isRecordPagePushed`) and its 「‹」 goes |
| 7 | + * back in the history, so Back after it pops cleanly instead of leaving a |
| 8 | + * second copy of the list on the stack. |
| 9 | + * |
| 10 | + * The controls: a desktop list's state is unchanged, and a phone's answer to |
| 11 | + * a drawer link (`?recordId=`), which replaces the list's entry, is not |
| 12 | + * marked, since there is no list below it to go back to. |
| 13 | + */ |
| 14 | + |
| 15 | +import * as React from 'react'; |
| 16 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 17 | +import { act, renderHook, render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react'; |
| 18 | +import { MemoryRouter, Routes, Route, useLocation, useNavigate, type Location } from 'react-router-dom'; |
| 19 | +import { MetadataCtx } from '@object-ui/react'; |
| 20 | +import { RelatedCountStore } from '@object-ui/components'; |
| 21 | + |
| 22 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 23 | + ...(await importOriginal<Record<string, unknown>>()), |
| 24 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }), |
| 25 | + createAuthenticatedFetch: () => vi.fn(), |
| 26 | +})); |
| 27 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 28 | + ...(await importOriginal<Record<string, unknown>>()), |
| 29 | + useRecordPresence: () => [], |
| 30 | + PresenceAvatars: () => null, |
| 31 | +})); |
| 32 | +vi.mock('sonner', () => ({ |
| 33 | + toast: Object.assign(vi.fn(), { success: vi.fn(), error: vi.fn(), info: vi.fn(), warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn() }), |
| 34 | +})); |
| 35 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 36 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 37 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 38 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 39 | +vi.mock('./MetadataInspector', () => ({ |
| 40 | + MetadataPanel: () => null, |
| 41 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 42 | +})); |
| 43 | + |
| 44 | +import { useObjectViewRecordNavigation } from './objectViewRecordNavigation'; |
| 45 | +import { useObjectViewPluginSchema } from './objectViewPluginSchema'; |
| 46 | +import { isRecordPagePushed, recordPagePushState, RECORD_PAGE_PUSHED_STATE } from './recordPagePush'; |
| 47 | +import { RecordDetailView } from './RecordDetailView'; |
| 48 | + |
| 49 | +const ROWS = [{ id: 'r-1' }, { id: 'r-2' }]; |
| 50 | +const LIST = { pathname: '/apps/crm/crm_lead', search: '?view=all', state: null, hash: '', key: 'k' } as Location; |
| 51 | + |
| 52 | +function hookInputs(navigate: ReturnType<typeof useNavigate>, phone: boolean, search = 'view=all', appName = 'crm') { |
| 53 | + return { |
| 54 | + activeView: { id: 'all', name: 'all', label: 'All leads' } as never, |
| 55 | + objectDef: { name: 'crm_lead', label: 'Lead', fields: {}, navigation: { mode: 'page' } } as never, |
| 56 | + objects: [{ name: 'crm_lead', label: 'Lead' }] as never, |
| 57 | + appName, |
| 58 | + viewId: undefined, |
| 59 | + searchParams: new URLSearchParams(search), |
| 60 | + setSearchParams: vi.fn(), |
| 61 | + location: { ...LIST, search: `?${search}` } as Location, |
| 62 | + navigate, |
| 63 | + routerRootHref: '/', |
| 64 | + servedViews: { views: [], objects: [] } as never, |
| 65 | + viewLabel: (_object: string, _view: string, fallback: string) => fallback, |
| 66 | + unlabelledViewText: 'Untitled view', |
| 67 | + language: 'en', |
| 68 | + recordOpenPreference: 'object' as never, |
| 69 | + phone, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 74 | + <MemoryRouter initialEntries={['/apps/crm/crm_lead?view=all']}>{children}</MemoryRouter> |
| 75 | +); |
| 76 | + |
| 77 | +const lastState = (navigate: ReturnType<typeof vi.fn>) => navigate.mock.calls.at(-1)![1].state; |
| 78 | + |
| 79 | +describe('a list marks the record page it pushes on a phone (objectstack#29074)', () => { |
| 80 | + it('a phone row click pushes the page with the list as its origin and the pushed marker', () => { |
| 81 | + const navigate = vi.fn(); |
| 82 | + const { result } = renderHook(() => useObjectViewRecordNavigation(hookInputs(navigate, true)), { wrapper }); |
| 83 | + act(() => result.current.handleListRowsChange(ROWS)); |
| 84 | + act(() => result.current.handleRowClick({ id: 'r-2' })); |
| 85 | + expect(navigate.mock.calls[0][0]).toBe('record/r-2'); |
| 86 | + expect(navigate.mock.calls[0][1].replace).toBeFalsy(); |
| 87 | + const state = lastState(navigate); |
| 88 | + expect(state.from).toEqual({ pathname: '/apps/crm/crm_lead?view=all', label: 'All leads', siblings: ['r-1', 'r-2'] }); |
| 89 | + expect(isRecordPagePushed(state)).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('control: a desktop row click carries no marker', () => { |
| 93 | + const navigate = vi.fn(); |
| 94 | + const { result } = renderHook(() => useObjectViewRecordNavigation(hookInputs(navigate, false)), { wrapper }); |
| 95 | + act(() => result.current.handleRowClick({ id: 'r-2' })); |
| 96 | + expect(lastState(navigate)).not.toHaveProperty(RECORD_PAGE_PUSHED_STATE); |
| 97 | + }); |
| 98 | + |
| 99 | + it('control: a phone answering a drawer link replaces the list entry and carries no marker', () => { |
| 100 | + const navigate = vi.fn(); |
| 101 | + renderHook(() => useObjectViewRecordNavigation(hookInputs(navigate, true, 'view=all&recordId=r-1')), { wrapper }); |
| 102 | + expect(navigate).toHaveBeenCalledTimes(1); |
| 103 | + expect(navigate.mock.calls[0][1].replace).toBe(true); |
| 104 | + expect(isRecordPagePushed(lastState(navigate))).toBe(false); |
| 105 | + }); |
| 106 | + |
| 107 | + it('a plugin view (kanban, calendar, …) marks its row push the same way', () => { |
| 108 | + const navigate = vi.fn(); |
| 109 | + const { result } = renderHook(() => |
| 110 | + useObjectViewPluginSchema({ |
| 111 | + views: [{ id: 'all', name: 'all', label: 'All leads', type: 'kanban' }] as never, |
| 112 | + activeViewId: 'all', |
| 113 | + viewDraft: null, |
| 114 | + activeView: { id: 'all', name: 'all', label: 'All leads', type: 'kanban' } as never, |
| 115 | + objectDef: { name: 'crm_lead', label: 'Lead', fields: {} } as never, |
| 116 | + onEdit: vi.fn(), |
| 117 | + isAdmin: false, |
| 118 | + location: LIST, |
| 119 | + navigate, |
| 120 | + viewId: undefined, |
| 121 | + servedViews: { views: [], objects: [] } as never, |
| 122 | + viewLabel: (_object: string, _view: string, fallback: string) => fallback, |
| 123 | + unlabelledViewText: 'Untitled view', |
| 124 | + language: 'en', |
| 125 | + }), |
| 126 | + { wrapper }, |
| 127 | + ); |
| 128 | + act(() => (result.current.objectViewSchema as { onNavigate: (id: string, mode: 'view') => void }).onNavigate('r-1', 'view')); |
| 129 | + const state = lastState(navigate); |
| 130 | + expect(state.from.pathname).toBe('/apps/crm/crm_lead?view=all'); |
| 131 | + expect(isRecordPagePushed(state)).toBe(true); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +/** |
| 136 | + * The history the marker buys, on the real record page at 390 px. Home → the |
| 137 | + * lead list → a row → the page's 「‹」 → Back: 「‹」 returns to the list entry |
| 138 | + * and Back leaves the list for home; the record is never reopened. The page |
| 139 | + * still names its way back by the object's plural (Target 3, objectstack#27058). |
| 140 | + */ |
| 141 | +const APP = 'demo'; |
| 142 | +const LEAD = 'crm_lead'; |
| 143 | +const OBJECTS = [ |
| 144 | + { |
| 145 | + name: LEAD, |
| 146 | + label: 'Lead', |
| 147 | + pluralLabel: 'Leads', |
| 148 | + fields: { id: { type: 'text', label: 'Id' }, name: { type: 'text', label: 'Name' } }, |
| 149 | + navigation: { mode: 'page' }, |
| 150 | + }, |
| 151 | +]; |
| 152 | + |
| 153 | +function ListRoute({ phone }: { phone: boolean }) { |
| 154 | + const navigate = useNavigate(); |
| 155 | + const location = useLocation(); |
| 156 | + const nav = useObjectViewRecordNavigation({ ...hookInputs(navigate, phone, 'view=all', APP), objectDef: OBJECTS[0] as never, location }); |
| 157 | + return <button type="button" data-testid="row" onClick={() => nav.handleRowClick({ id: 'r-1' })}>row</button>; |
| 158 | +} |
| 159 | + |
| 160 | +function Where() { |
| 161 | + const location = useLocation(); |
| 162 | + const navigate = useNavigate(); |
| 163 | + return ( |
| 164 | + <> |
| 165 | + <output data-testid="where">{`${location.pathname}${location.search}`}</output> |
| 166 | + <button type="button" data-testid="browser-back" onClick={() => navigate(-1)}>browser back</button> |
| 167 | + </> |
| 168 | + ); |
| 169 | +} |
| 170 | + |
| 171 | +function mountHistory(entries: Array<string | { pathname: string; search?: string; state?: unknown }>, phone = true) { |
| 172 | + const dataSource = { |
| 173 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 174 | + findOne: vi.fn(async (_object: string, id: string) => ({ id, name: id === 'L-1' ? 'Mira Costa' : 'Theo Park' })), |
| 175 | + create: vi.fn(), |
| 176 | + update: vi.fn(async () => ({})), |
| 177 | + delete: vi.fn(async () => ({})), |
| 178 | + }; |
| 179 | + const metadata = { |
| 180 | + objects: OBJECTS, pages: [], loading: false, error: null, |
| 181 | + refresh: async () => {}, invalidate: () => {}, ensureType: async () => [], getItem: async () => null, getItemsByType: () => [], |
| 182 | + } as never; |
| 183 | + render( |
| 184 | + <MemoryRouter initialEntries={entries as never} initialIndex={entries.length - 1}> |
| 185 | + <MetadataCtx.Provider value={metadata}> |
| 186 | + <Routes> |
| 187 | + <Route path="/home" element={<div />} /> |
| 188 | + <Route path={`/apps/${APP}/${LEAD}`} element={<ListRoute phone={phone} />} /> |
| 189 | + <Route |
| 190 | + path="/apps/:appName/:objectName/record/:recordId" |
| 191 | + element={<RecordDetailView dataSource={dataSource as never} objects={OBJECTS as never} onEdit={() => {}} />} |
| 192 | + /> |
| 193 | + </Routes> |
| 194 | + <Where /> |
| 195 | + </MetadataCtx.Provider> |
| 196 | + </MemoryRouter>, |
| 197 | + ); |
| 198 | +} |
| 199 | + |
| 200 | +const where = () => screen.getByTestId('where').textContent; |
| 201 | +const backLink = () => document.querySelector('[data-record-page-root] > [data-record-top-bar] [data-record-crumb-back]') as HTMLElement | null; |
| 202 | + |
| 203 | +describe('the record page a phone list pushes (objectstack#29074)', () => { |
| 204 | + beforeEach(() => { |
| 205 | + cleanup(); |
| 206 | + Object.defineProperty(window, 'innerWidth', { value: 390, configurable: true, writable: true }); |
| 207 | + RelatedCountStore._reset(); |
| 208 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ data: [] }), { status: 200, headers: { 'content-type': 'application/json' } }))); |
| 209 | + const measure = HTMLElement.prototype.getBoundingClientRect; |
| 210 | + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) { |
| 211 | + if (!this.hasAttribute('data-record-page-root')) return measure.call(this); |
| 212 | + return { x: 0, y: 0, top: 0, left: 0, right: 390, bottom: 800, width: 390, height: 800, toJSON: () => ({}) } as DOMRect; |
| 213 | + }); |
| 214 | + }); |
| 215 | + afterEach(() => { |
| 216 | + vi.unstubAllGlobals(); |
| 217 | + vi.restoreAllMocks(); |
| 218 | + Object.defineProperty(window, 'innerWidth', { value: 1440, configurable: true, writable: true }); |
| 219 | + }); |
| 220 | + |
| 221 | + it('「‹ Leads」 goes back to the list entry, and Back then leaves the list for home', async () => { |
| 222 | + mountHistory(['/home', `/apps/${APP}/${LEAD}?view=all`]); |
| 223 | + fireEvent.click(screen.getByTestId('row')); |
| 224 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}/record/r-1`), { timeout: 5000 }); |
| 225 | + await waitFor(() => expect(backLink()).toBeTruthy(), { timeout: 5000 }); |
| 226 | + expect(backLink()!.tagName).toBe('BUTTON'); |
| 227 | + expect(backLink()!.textContent).toBe('Leads'); |
| 228 | + fireEvent.click(backLink()!); |
| 229 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}?view=all`)); |
| 230 | + fireEvent.click(screen.getByTestId('browser-back')); |
| 231 | + await waitFor(() => expect(where()).toBe('/home')); |
| 232 | + }); |
| 233 | + |
| 234 | + it('control, the push unmarked (a desktop list): 「‹」 is a link that replaces the record (#29210), so the list sits twice and Back stays on it', async () => { |
| 235 | + mountHistory(['/home', `/apps/${APP}/${LEAD}?view=all`], false); |
| 236 | + fireEvent.click(screen.getByTestId('row')); |
| 237 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}/record/r-1`), { timeout: 5000 }); |
| 238 | + await waitFor(() => expect(backLink()).toBeTruthy(), { timeout: 5000 }); |
| 239 | + expect(backLink()!.tagName).toBe('A'); |
| 240 | + expect(backLink()!.textContent).toBe('Leads'); |
| 241 | + fireEvent.click(backLink()!); |
| 242 | + await waitFor(() => expect(where()).toBe(`/apps/${APP}/${LEAD}?view=all`)); |
| 243 | + // The record's entry was replaced by a second copy of the list: Back |
| 244 | + // lands on the first copy, and only a second Back reaches home. The |
| 245 | + // marked push above reaches home in one. |
| 246 | + fireEvent.click(screen.getByTestId('browser-back')); |
| 247 | + await waitFor(() => expect(screen.getByTestId('row')).toBeTruthy()); |
| 248 | + expect(where()).toBe(`/apps/${APP}/${LEAD}?view=all`); |
| 249 | + fireEvent.click(screen.getByTestId('browser-back')); |
| 250 | + await waitFor(() => expect(where()).toBe('/home')); |
| 251 | + }); |
| 252 | + |
| 253 | + it('a lead pushed from another lead\'s page still reads that record, not the plural', async () => { |
| 254 | + const parent = `/apps/${APP}/${LEAD}/record/L-1`; |
| 255 | + mountHistory([parent, { pathname: `/apps/${APP}/${LEAD}/record/r-2`, state: recordPagePushState({ pathname: parent, search: '' }, 'Mira Costa') }]); |
| 256 | + await waitFor(() => expect(backLink()).toBeTruthy(), { timeout: 5000 }); |
| 257 | + expect(backLink()!.tagName).toBe('BUTTON'); |
| 258 | + expect(backLink()!.textContent).toBe('Mira Costa'); |
| 259 | + }); |
| 260 | +}); |
0 commit comments