|
| 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#26681 — the console object page's Display menu writes the |
| 11 | + * reader's record-open preference, and the name cell's ↗ opens the record as |
| 12 | + * the full page. |
| 13 | + * |
| 14 | + * End to end through `ObjectView`, the host that resolves the mode and keeps |
| 15 | + * the preference (`utils/recordOpenPreference.ts`): |
| 16 | + * |
| 17 | + * - a drawer object: the Open records section draws Drawer | Split; choosing |
| 18 | + * Split keeps `split` under `os:record-open-mode`, and the next row click |
| 19 | + * draws the record BESIDE the list (the split face: the list's record-count |
| 20 | + * footer and the pane's close control) — no reload, no remount; |
| 21 | + * - a view that declares `page`: the read-only line, no segments, and no ↗ |
| 22 | + * (its rows already open the page); |
| 23 | + * - the ↗ beside a drawer row's record link is a real anchor to the record route, |
| 24 | + * and a plain click on it lands there, leaving the preference as it was. |
| 25 | + * |
| 26 | + * The record body is not what this file reads, so `RecordDetailView` is a |
| 27 | + * null sink. |
| 28 | + */ |
| 29 | + |
| 30 | +import * as React from 'react'; |
| 31 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 32 | +import { render, cleanup, screen, fireEvent, waitFor, within } from '@testing-library/react'; |
| 33 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 34 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 35 | + |
| 36 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 37 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 38 | + // Stable identities — `ListView` names `perms` in its fetch dependencies. |
| 39 | + const perms = { |
| 40 | + check: () => ({ allowed: true }), |
| 41 | + checkField: () => true, |
| 42 | + getFieldPermissions: () => [], |
| 43 | + getRowFilter: () => undefined, |
| 44 | + getObjectApiOperations: () => undefined, |
| 45 | + roles: [], |
| 46 | + isLoaded: false, |
| 47 | + hasCapabilities: () => true, |
| 48 | + can: () => true, |
| 49 | + cannot: () => false, |
| 50 | + }; |
| 51 | + const fieldPerms = { canRead: () => true, canWrite: () => true, permissions: [] }; |
| 52 | + return { ...actual, usePermissions: () => perms, useFieldPermissions: () => fieldPerms }; |
| 53 | +}); |
| 54 | + |
| 55 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 56 | + ...(await importOriginal<Record<string, unknown>>()), |
| 57 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null }), |
| 58 | + useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }), |
| 59 | + createAuthenticatedFetch: () => vi.fn(), |
| 60 | +})); |
| 61 | + |
| 62 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 63 | + ...(await importOriginal<Record<string, unknown>>()), |
| 64 | + useRealtimeSubscription: () => ({ lastMessage: null }), |
| 65 | + useConflictResolution: () => ({ hasConflicts: false, resolveAllConflicts: () => {} }), |
| 66 | +})); |
| 67 | + |
| 68 | +vi.mock('sonner', () => ({ |
| 69 | + toast: Object.assign(vi.fn(), { |
| 70 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 71 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 72 | + }), |
| 73 | +})); |
| 74 | + |
| 75 | +vi.mock('./MetadataInspector', () => ({ |
| 76 | + MetadataPanel: () => null, |
| 77 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 78 | +})); |
| 79 | +// The record body: not what this file reads. |
| 80 | +vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 81 | + |
| 82 | +vi.mock('./metadata-admin/useMetadata', async (importOriginal) => ({ |
| 83 | + ...(await importOriginal<Record<string, unknown>>()), |
| 84 | + useMetadataClient: () => ({ save: vi.fn(async () => ({})), get: vi.fn(async () => null) }), |
| 85 | +})); |
| 86 | + |
| 87 | +import { ObjectView } from './ObjectView'; |
| 88 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 89 | +import { RECORD_OPEN_PREFERENCE_KEY } from '../utils/recordOpenPreference'; |
| 90 | + |
| 91 | +const OBJECT_NAME = 'duly_task'; |
| 92 | +const VIEW_ID = `${OBJECT_NAME}.all`; |
| 93 | +const ROWS = [{ id: 'r1', name: 'Alpha' }]; |
| 94 | + |
| 95 | +const objectsWith = (navigation?: Record<string, unknown>) => [ |
| 96 | + { |
| 97 | + name: OBJECT_NAME, |
| 98 | + label: 'Task', |
| 99 | + fields: { |
| 100 | + id: { type: 'text', label: 'Id' }, |
| 101 | + name: { type: 'text', label: 'Name' }, |
| 102 | + }, |
| 103 | + listViews: { |
| 104 | + [VIEW_ID]: { |
| 105 | + name: VIEW_ID, |
| 106 | + label: 'Everything', |
| 107 | + type: 'grid', |
| 108 | + columns: ['name'], |
| 109 | + ...(navigation ? { navigation } : {}), |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | +]; |
| 114 | + |
| 115 | +beforeEach(() => { |
| 116 | + vi.stubGlobal( |
| 117 | + 'fetch', |
| 118 | + vi.fn(async () => |
| 119 | + new Response(JSON.stringify({ data: [] }), { |
| 120 | + status: 200, |
| 121 | + headers: { 'content-type': 'application/json' }, |
| 122 | + }), |
| 123 | + ), |
| 124 | + ); |
| 125 | +}); |
| 126 | + |
| 127 | +afterEach(() => { |
| 128 | + cleanup(); |
| 129 | + vi.unstubAllGlobals(); |
| 130 | + window.localStorage.removeItem(RECORD_OPEN_PREFERENCE_KEY); |
| 131 | + window.history.replaceState(null, '', '/'); |
| 132 | +}); |
| 133 | + |
| 134 | +/** Mount the object list, its one row drawn. */ |
| 135 | +async function openList(objects: ReturnType<typeof objectsWith>) { |
| 136 | + const dataSource = { |
| 137 | + find: vi.fn(async () => ({ data: ROWS, total: ROWS.length })), |
| 138 | + findOne: vi.fn(async () => null), |
| 139 | + create: vi.fn(async () => ({})), |
| 140 | + update: vi.fn(async () => ({})), |
| 141 | + delete: vi.fn(async () => ({})), |
| 142 | + getObjectSchema: vi.fn(async (name: string) => objects.find((o) => o.name === name) ?? null), |
| 143 | + } as never; |
| 144 | + // The list's own record href is built from the DOCUMENT's path |
| 145 | + // (`listRecordDetailUrl`), which a `MemoryRouter` does not move. |
| 146 | + window.history.replaceState(null, '', `/apps/demo/${OBJECT_NAME}/view/${VIEW_ID}`); |
| 147 | + render( |
| 148 | + <I18nProvider instance={createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false })}> |
| 149 | + <ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'user' }}> |
| 150 | + <MemoryRouter initialEntries={[`/apps/demo/${OBJECT_NAME}/view/${VIEW_ID}`]}> |
| 151 | + <Routes> |
| 152 | + <Route |
| 153 | + path="/apps/:appName/:objectName/view/:viewId" |
| 154 | + element={<ObjectView dataSource={dataSource} objects={objects} onEdit={() => {}} />} |
| 155 | + /> |
| 156 | + <Route |
| 157 | + path="/apps/:appName/:objectName/record/:recordId" |
| 158 | + element={<output data-testid="record-page-route">record page</output>} |
| 159 | + /> |
| 160 | + </Routes> |
| 161 | + </MemoryRouter> |
| 162 | + </ExpressionProvider> |
| 163 | + </I18nProvider>, |
| 164 | + ); |
| 165 | + await screen.findByTestId('primary-field-link', undefined, { timeout: 5000 }); |
| 166 | +} |
| 167 | + |
| 168 | +function openDisplay() { |
| 169 | + fireEvent.click(screen.getByTestId('toolbar-display-trigger')); |
| 170 | +} |
| 171 | + |
| 172 | +describe('objectstack#26681 — Open records in the Display menu', () => { |
| 173 | + it('a drawer object: choosing Split keeps the preference, and the next row click opens the record beside the list', async () => { |
| 174 | + await openList(objectsWith()); |
| 175 | + openDisplay(); |
| 176 | + const drawerSegment = await screen.findByRole('radio', { name: 'Drawer' }); |
| 177 | + expect(drawerSegment.getAttribute('aria-checked')).toBe('true'); |
| 178 | + fireEvent.click(screen.getByRole('radio', { name: 'Split' })); |
| 179 | + |
| 180 | + expect(window.localStorage.getItem(RECORD_OPEN_PREFERENCE_KEY)).toBe('split'); |
| 181 | + // The section reads the live value back: no reload, no remount. |
| 182 | + await waitFor(() => |
| 183 | + expect(screen.getByRole('radio', { name: 'Split' }).getAttribute('aria-checked')).toBe('true'), |
| 184 | + ); |
| 185 | + |
| 186 | + fireEvent.keyDown(document.activeElement ?? document.body, { key: 'Escape' }); |
| 187 | + fireEvent.click(screen.getByTestId('primary-field-link')); |
| 188 | + expect(await screen.findByTestId('record-count-footer', undefined, { timeout: 5000 })).toBeTruthy(); |
| 189 | + expect(screen.getByRole('button', { name: 'Close panel' })).toBeTruthy(); |
| 190 | + expect(screen.queryByRole('dialog')).toBeNull(); |
| 191 | + }); |
| 192 | + |
| 193 | + it('a drawer object with no choice made: the row opens the drawer', async () => { |
| 194 | + await openList(objectsWith()); |
| 195 | + fireEvent.click(screen.getByTestId('primary-field-link')); |
| 196 | + expect(await screen.findByRole('dialog', undefined, { timeout: 5000 })).toBeTruthy(); |
| 197 | + expect(screen.queryByTestId('record-count-footer')).toBeNull(); |
| 198 | + }); |
| 199 | + |
| 200 | + it('a view that declares `page`: the read-only line, no segments, and no ↗', async () => { |
| 201 | + window.localStorage.setItem(RECORD_OPEN_PREFERENCE_KEY, 'split'); |
| 202 | + await openList(objectsWith({ mode: 'page' })); |
| 203 | + expect(screen.queryByTestId('record-open-as-page')).toBeNull(); |
| 204 | + openDisplay(); |
| 205 | + expect(await screen.findByTestId('display-record-open-declared')).toBeTruthy(); |
| 206 | + expect(within(screen.getByTestId('display-record-open')).queryByRole('radio')).toBeNull(); |
| 207 | + }); |
| 208 | + |
| 209 | + it('the ↗ beside a drawer row is an anchor to the record route, and a click lands there without changing the preference', async () => { |
| 210 | + await openList(objectsWith()); |
| 211 | + const arrow = await screen.findByTestId('record-open-as-page'); |
| 212 | + expect(arrow.tagName).toBe('A'); |
| 213 | + expect(arrow.getAttribute('href')).toBe(`/apps/demo/${OBJECT_NAME}/record/r1`); |
| 214 | + expect(arrow.getAttribute('aria-label')).toBe('Open as full page'); |
| 215 | + expect(arrow.getAttribute('title')).toBe('Open as full page'); |
| 216 | + // Beside the row's record link, never inside it. |
| 217 | + const pair = arrow.closest('[data-testid="link-cell-trailing"]') as HTMLElement; |
| 218 | + expect(pair.querySelector('[data-testid="primary-field-link"]')).not.toBeNull(); |
| 219 | + expect(arrow.parentElement?.closest('a')).toBeNull(); |
| 220 | + |
| 221 | + fireEvent.click(arrow); |
| 222 | + expect(await screen.findByTestId('record-page-route')).toBeTruthy(); |
| 223 | + // Neither the drawer nor the pane opened on the way, and nothing was kept. |
| 224 | + expect(screen.queryByRole('dialog')).toBeNull(); |
| 225 | + expect(window.localStorage.getItem(RECORD_OPEN_PREFERENCE_KEY)).toBeNull(); |
| 226 | + }); |
| 227 | +}); |
0 commit comments