|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * objectstack#27520 row 12 — a row's record link keeps a way back to the inbox. |
| 6 | + * |
| 7 | + * The record link stays where it was: the console's standard record route in |
| 8 | + * the inbox's own app segment (`ApprovalsInboxPage.recordLink.test.tsx` pins |
| 9 | + * the href). Inside an app whose navigation does not list the record's object, |
| 10 | + * such as the Account app, which lists the inbox, the record page had no way |
| 11 | + * back. The link now hands the record page the history-state origin every list |
| 12 | + * row hands it (`state.from`), so the page's crumb row draws a link back to the |
| 13 | + * inbox, named as the inbox's own header names it. |
| 14 | + * |
| 15 | + * The harness mounts the REAL inbox page and, on the record route, the REAL |
| 16 | + * `RecordDetailView`; only the HTTP transport, the console adapter and the |
| 17 | + * inbox's list read (`approvalsApi.listRequests`) are doubled. |
| 18 | + */ |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import { render, screen, cleanup, fireEvent, within } from '@testing-library/react'; |
| 22 | +import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom'; |
| 23 | + |
| 24 | +const { approvalsApiStub, ROW, AUTH } = vi.hoisted(() => { |
| 25 | + const ROW = { |
| 26 | + id: 'req_27520', |
| 27 | + process_name: 'flow:invoice_approval', |
| 28 | + process_label: 'Invoice Approval', |
| 29 | + object_name: 'invoice', |
| 30 | + object_label: 'Invoice', |
| 31 | + record_id: 'inv_1', |
| 32 | + record_title: 'INV-1010', |
| 33 | + status: 'pending', |
| 34 | + pending_approvers: ['u1'], |
| 35 | + submitter_id: 'u2', |
| 36 | + submitter_name: 'Sam Submitter', |
| 37 | + submitted_at: '2026-10-20T09:00:00.000Z', |
| 38 | + updated_at: '2026-10-20T09:00:00.000Z', |
| 39 | + }; |
| 40 | + const approvalsApiStub = { |
| 41 | + listRequests: vi.fn(async () => ({ data: [ROW], total: 1 })), |
| 42 | + getRequest: vi.fn(async () => ({ data: ROW })), |
| 43 | + listActions: vi.fn(async () => ({ data: [] })), |
| 44 | + approve: vi.fn(async () => ({ data: ROW, finalized: true })), |
| 45 | + reject: vi.fn(async () => ({ data: ROW, finalized: true })), |
| 46 | + }; |
| 47 | + // A STABLE singleton: a fresh user per render re-runs the inbox's load |
| 48 | + // effect forever, and the queue never leaves its skeleton. |
| 49 | + const AUTH = { user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }; |
| 50 | + return { approvalsApiStub, ROW, AUTH }; |
| 51 | +}); |
| 52 | + |
| 53 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 54 | + ...(await importOriginal<Record<string, unknown>>()), |
| 55 | + useAuth: () => AUTH, |
| 56 | +})); |
| 57 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 58 | + ...(await importOriginal<Record<string, unknown>>()), |
| 59 | + useRecordPresence: () => [], |
| 60 | + PresenceAvatars: () => null, |
| 61 | +})); |
| 62 | +vi.mock('sonner', () => ({ |
| 63 | + toast: Object.assign(vi.fn(), { |
| 64 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 65 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 66 | + }), |
| 67 | +})); |
| 68 | +vi.mock('../../services/approvalsApi', async (importOriginal) => ({ |
| 69 | + ...(await importOriginal<Record<string, unknown>>()), |
| 70 | + approvalsApi: approvalsApiStub, |
| 71 | +})); |
| 72 | + |
| 73 | +import { AdapterCtx, MetadataCtx, SchemaRendererProvider, useAdapter } from '@object-ui/react'; |
| 74 | +import { I18nProvider, type I18nConfig } from '@object-ui/i18n'; |
| 75 | +import { RecordDetailView } from '@object-ui/app-shell'; |
| 76 | +import { ApprovalsInboxPage } from './ApprovalsInboxPage'; |
| 77 | + |
| 78 | +/** The Account app's route segment: its nav lists the inbox, not invoices. */ |
| 79 | +const APP = 'account'; |
| 80 | +const INBOX = `/apps/${APP}/component/approvals/inbox`; |
| 81 | +const RECORD_PAGE = `/apps/${APP}/invoice/record/${ROW.record_id}`; |
| 82 | + |
| 83 | +const INVOICE_DEF = { |
| 84 | + name: 'invoice', |
| 85 | + label: 'Invoice', |
| 86 | + pluralLabel: 'Invoices', |
| 87 | + nameField: 'name', |
| 88 | + fields: { name: { type: 'text', label: 'Invoice Number' } }, |
| 89 | +}; |
| 90 | +const RECORD = { id: ROW.record_id, name: ROW.record_title }; |
| 91 | + |
| 92 | +function json(status: number, body: unknown): Response { |
| 93 | + return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }); |
| 94 | +} |
| 95 | +vi.stubGlobal('fetch', vi.fn(async () => json(200, { success: true, data: [] }))); |
| 96 | + |
| 97 | +const host = { |
| 98 | + // The inbox's readability probe reads `id in (…)`: the record is readable. |
| 99 | + find: vi.fn(async (_object: string, params?: Record<string, unknown>) => { |
| 100 | + const ids = (params?.$filter as { id?: { $in?: string[] } } | undefined)?.id?.$in; |
| 101 | + return ids ? { data: ids.map((id) => ({ id })), total: ids.length } : { data: [], total: 0 }; |
| 102 | + }), |
| 103 | + findOne: vi.fn(async (object: string, id: string) => (object === 'invoice' && id === RECORD.id ? RECORD : null)), |
| 104 | + create: vi.fn(), update: vi.fn(), delete: vi.fn(), |
| 105 | + getObjectSchema: vi.fn(async (name: string) => (name === 'invoice' ? INVOICE_DEF : null)), |
| 106 | +}; |
| 107 | +const metadata = { |
| 108 | + apps: [], objects: [INVOICE_DEF], dashboards: [], reports: [], |
| 109 | + pages: [], loading: false, error: null, |
| 110 | + refresh: async () => {}, invalidate: () => {}, ensureType: async () => [], |
| 111 | + getItem: vi.fn(async (type: string, name: string) => (type === 'object' && name === 'invoice' ? INVOICE_DEF : null)), |
| 112 | + getItemsByType: () => [], |
| 113 | + getTypeStatus: () => 'ready' as const, |
| 114 | +}; |
| 115 | +const EN: I18nConfig = { defaultLanguage: 'en', detectBrowserLanguage: false }; |
| 116 | + |
| 117 | +/** `DefaultAppContent`'s generic record route: the view over `useAdapter()`. */ |
| 118 | +function GenericRecordRoute() { |
| 119 | + return <RecordDetailView dataSource={useAdapter()} objects={metadata.objects as never} onEdit={() => {}} />; |
| 120 | +} |
| 121 | + |
| 122 | +function RouterProbe() { |
| 123 | + const location = useLocation(); |
| 124 | + return <output data-testid="where">{`${location.pathname}${location.search}`}</output>; |
| 125 | +} |
| 126 | + |
| 127 | +function renderAt(entry: string) { |
| 128 | + return render( |
| 129 | + <I18nProvider config={EN} persistLanguage={false}> |
| 130 | + <MemoryRouter initialEntries={[entry]}> |
| 131 | + <SchemaRendererProvider dataSource={host as never}> |
| 132 | + <MetadataCtx.Provider value={metadata as never}> |
| 133 | + <AdapterCtx.Provider value={host as never}> |
| 134 | + <RouterProbe /> |
| 135 | + <Routes> |
| 136 | + <Route path="/apps/:appName/component/approvals/inbox" element={<ApprovalsInboxPage />} /> |
| 137 | + <Route path="/apps/:appName/:objectName/record/:recordId" element={<GenericRecordRoute />} /> |
| 138 | + </Routes> |
| 139 | + </AdapterCtx.Provider> |
| 140 | + </MetadataCtx.Provider> |
| 141 | + </SchemaRendererProvider> |
| 142 | + </MemoryRouter> |
| 143 | + </I18nProvider>, |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +const where = () => screen.getByTestId('where').textContent; |
| 148 | + |
| 149 | +/** The desktop row's record link (the mobile card draws none). */ |
| 150 | +async function recordLink(): Promise<HTMLElement> { |
| 151 | + await screen.findAllByText(ROW.record_title, undefined, { timeout: 8000 }); |
| 152 | + const row = screen.getAllByRole('row').find((r) => within(r).queryAllByText(ROW.record_title).length > 0); |
| 153 | + if (!row) throw new Error('no inbox row for the request'); |
| 154 | + return within(row).findByRole('link', { name: ROW.record_title }, { timeout: 8000 }); |
| 155 | +} |
| 156 | + |
| 157 | +beforeEach(() => { |
| 158 | + for (const fn of Object.values(approvalsApiStub)) fn.mockClear(); |
| 159 | +}); |
| 160 | +afterEach(cleanup); |
| 161 | + |
| 162 | +describe('the inbox record link keeps a way back to the inbox (objectstack#27520 row 12)', () => { |
| 163 | + it('the record page it opens draws a link back to the inbox, and the link returns there', async () => { |
| 164 | + renderAt(INBOX); |
| 165 | + const link = await recordLink(); |
| 166 | + // The target app stays the inbox's own (the recordLink pin's href). |
| 167 | + expect(link).toHaveAttribute('href', RECORD_PAGE); |
| 168 | + fireEvent.click(link); |
| 169 | + expect(where()).toBe(RECORD_PAGE); |
| 170 | + |
| 171 | + const back = await screen.findByRole('link', { name: 'Approvals Inbox' }, { timeout: 8000 }); |
| 172 | + expect(back).toHaveAttribute('href', INBOX); |
| 173 | + fireEvent.click(back); |
| 174 | + expect(where()).toBe(INBOX); |
| 175 | + }); |
| 176 | + |
| 177 | + it('CONTROL: a record page reached without the inbox draws no such link', async () => { |
| 178 | + renderAt(RECORD_PAGE); |
| 179 | + await screen.findAllByText(ROW.record_title, undefined, { timeout: 8000 }); |
| 180 | + expect(screen.queryByRole('link', { name: 'Approvals Inbox' })).toBeNull(); |
| 181 | + }); |
| 182 | +}); |
0 commit comments