|
| 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#33719 (ruling A) — on a phone, an authored "no navigation" holds. |
| 11 | + * |
| 12 | + * The phone rule (objectstack#27058) decides HOW a record opens — as a page, |
| 13 | + * since a phone has no drawer — never WHETHER it opens. A list whose view |
| 14 | + * authors `navigation: { mode: 'none' }` or `navigation: { preventNavigation: |
| 15 | + * true }` therefore resolves to `none` on a phone as on a desktop: its phone |
| 16 | + * card draws no pointer and no hover wash (`rowOpensRecord`), a tap on it |
| 17 | + * opens nothing, and a `?recordId=` link to one of its rows opens no record |
| 18 | + * page (the desktop draws no pane for it). |
| 19 | + * |
| 20 | + * Driven at 375 px through the real `ObjectView`, `ListView`, grid and |
| 21 | + * `MePermissionsProvider`: one differential per spelling, the same object and |
| 22 | + * rows, only the view's `navigation` differs. CONTROL: a list with no authored |
| 23 | + * navigation keeps its pointer, and both its tap and its `?recordId=` link |
| 24 | + * open the record page. |
| 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 } from '@testing-library/react'; |
| 33 | +import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom'; |
| 34 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 35 | +import { MePermissionsProvider, type MePermissionsAuthenticatedResponse } from '@object-ui/permissions'; |
| 36 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 37 | + |
| 38 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 39 | + ...(await importOriginal<Record<string, unknown>>()), |
| 40 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null }), |
| 41 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 42 | + createAuthenticatedFetch: () => vi.fn(), |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 46 | + ...(await importOriginal<Record<string, unknown>>()), |
| 47 | + useRealtimeSubscription: () => ({ lastMessage: null }), |
| 48 | + useConflictResolution: () => ({ hasConflicts: false, resolveAllConflicts: () => {} }), |
| 49 | +})); |
| 50 | + |
| 51 | +vi.mock('sonner', () => ({ |
| 52 | + toast: Object.assign(vi.fn(), { |
| 53 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 54 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 55 | + }), |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock('./MetadataInspector', () => ({ |
| 59 | + MetadataPanel: () => null, |
| 60 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 61 | +})); |
| 62 | +// The record body: not what this file reads. |
| 63 | +vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 64 | + |
| 65 | +vi.mock('./metadata-admin/useMetadata', async (importOriginal) => ({ |
| 66 | + ...(await importOriginal<Record<string, unknown>>()), |
| 67 | + useMetadataClient: () => ({ save: vi.fn(async () => ({})), get: vi.fn(async () => null) }), |
| 68 | +})); |
| 69 | + |
| 70 | +// `ObjectView` reaches a dynamic `import('@objectstack/lint')`: pay its load at |
| 71 | +// collection, never inside a case's clocked window (`check:test-source-alias`). |
| 72 | +import '@objectstack/lint'; |
| 73 | +import { ObjectView } from './ObjectView'; |
| 74 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 75 | + |
| 76 | +const OBJECT_NAME = 'demo_credential'; |
| 77 | +const VIEW_ID = 'all'; |
| 78 | +const LIST_PATH = `/apps/setup/${OBJECT_NAME}/view/${VIEW_ID}`; |
| 79 | +const PHONE_WIDTH = 375; |
| 80 | + |
| 81 | +const ROWS = [ |
| 82 | + { id: 'c1', name: 'Okta staging', connection: 'okta-staging' }, |
| 83 | + { id: 'c2', name: 'Azure prod', connection: 'azure-prod' }, |
| 84 | +]; |
| 85 | + |
| 86 | +type Navigation = { mode?: 'none'; preventNavigation?: boolean }; |
| 87 | + |
| 88 | +/** The two authored spellings of "rows open nothing". */ |
| 89 | +const NO_NAVIGATION: Array<[string, Navigation]> = [ |
| 90 | + ['`navigation.mode: none`', { mode: 'none' }], |
| 91 | + ['`navigation.preventNavigation: true`', { preventNavigation: true }], |
| 92 | +]; |
| 93 | + |
| 94 | +/** The object and its one grid view, carrying `navigation` when given. */ |
| 95 | +function objectsWith(navigation?: Navigation) { |
| 96 | + return [ |
| 97 | + { |
| 98 | + name: OBJECT_NAME, |
| 99 | + label: 'Credential', |
| 100 | + pluralLabel: 'Credentials', |
| 101 | + nameField: 'name', |
| 102 | + fields: { |
| 103 | + id: { type: 'text', label: 'Id' }, |
| 104 | + name: { type: 'text', label: 'Name' }, |
| 105 | + connection: { type: 'text', label: 'Connection' }, |
| 106 | + }, |
| 107 | + listViews: { |
| 108 | + [VIEW_ID]: { |
| 109 | + name: VIEW_ID, |
| 110 | + label: 'All', |
| 111 | + type: 'grid', |
| 112 | + columns: ['name', 'connection'], |
| 113 | + ...(navigation ? { navigation } : {}), |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + ]; |
| 118 | +} |
| 119 | + |
| 120 | +/** The caller's `/me/permissions` answer: a served object it may read. */ |
| 121 | +const ANSWER = { |
| 122 | + authenticated: true, |
| 123 | + userId: 'u1', |
| 124 | + tenantId: null, |
| 125 | + positions: [], |
| 126 | + hasAnyQueue: false, |
| 127 | + objectsUsingQueues: [], |
| 128 | + permissionSets: ['admin'], |
| 129 | + systemPermissions: [], |
| 130 | + objects: { |
| 131 | + [OBJECT_NAME]: { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true }, |
| 132 | + }, |
| 133 | + fields: {}, |
| 134 | +} as MePermissionsAuthenticatedResponse; |
| 135 | + |
| 136 | +let openSpy: ReturnType<typeof vi.fn>; |
| 137 | +const DESKTOP_WIDTH = window.innerWidth; |
| 138 | + |
| 139 | +beforeEach(() => { |
| 140 | + vi.spyOn(HTMLElement.prototype, 'scrollIntoView').mockImplementation(() => {}); |
| 141 | + vi.stubGlobal( |
| 142 | + 'fetch', |
| 143 | + vi.fn(async () => |
| 144 | + new Response(JSON.stringify({ data: [] }), { |
| 145 | + status: 200, |
| 146 | + headers: { 'content-type': 'application/json' }, |
| 147 | + }), |
| 148 | + ), |
| 149 | + ); |
| 150 | + openSpy = vi.fn(() => null); |
| 151 | + vi.stubGlobal('open', openSpy); |
| 152 | + Object.defineProperty(window, 'innerWidth', { value: PHONE_WIDTH, configurable: true, writable: true }); |
| 153 | +}); |
| 154 | + |
| 155 | +afterEach(() => { |
| 156 | + cleanup(); |
| 157 | + vi.unstubAllGlobals(); |
| 158 | + vi.restoreAllMocks(); |
| 159 | + Object.defineProperty(window, 'innerWidth', { value: DESKTOP_WIDTH, configurable: true, writable: true }); |
| 160 | + window.history.replaceState(null, '', '/'); |
| 161 | +}); |
| 162 | + |
| 163 | +/** Where the console stands: its path and its query. */ |
| 164 | +function LocationProbe() { |
| 165 | + const location = useLocation(); |
| 166 | + return <div data-testid="location">{location.pathname + location.search}</div>; |
| 167 | +} |
| 168 | + |
| 169 | +/** Mounts the list at phone width, at `LIST_PATH` plus `search`. */ |
| 170 | +function mountList(navigation: Navigation | undefined, search = '') { |
| 171 | + const objects = objectsWith(navigation); |
| 172 | + const dataSource = { |
| 173 | + find: vi.fn(async () => ({ data: ROWS, total: ROWS.length })), |
| 174 | + findOne: vi.fn(async () => null), |
| 175 | + create: vi.fn(async () => ({})), |
| 176 | + update: vi.fn(async () => ({})), |
| 177 | + delete: vi.fn(async () => ({})), |
| 178 | + getObjectSchema: vi.fn(async (name: string) => objects.find((o) => o.name === name) ?? null), |
| 179 | + } as never; |
| 180 | + window.history.replaceState(null, '', LIST_PATH + search); |
| 181 | + render( |
| 182 | + <SchemaRendererProvider dataSource={dataSource}> |
| 183 | + <MePermissionsProvider initialPermissions={ANSWER}> |
| 184 | + <I18nProvider instance={createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false })}> |
| 185 | + <ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'admin' }}> |
| 186 | + <MemoryRouter initialEntries={[LIST_PATH + search]}> |
| 187 | + <Routes> |
| 188 | + <Route |
| 189 | + path="/apps/:appName/:objectName/view/:viewId" |
| 190 | + element={ |
| 191 | + <> |
| 192 | + <ObjectView dataSource={dataSource} objects={objects} onEdit={() => {}} /> |
| 193 | + <LocationProbe /> |
| 194 | + </> |
| 195 | + } |
| 196 | + /> |
| 197 | + <Route path="*" element={<LocationProbe />} /> |
| 198 | + </Routes> |
| 199 | + </MemoryRouter> |
| 200 | + </ExpressionProvider> |
| 201 | + </I18nProvider> |
| 202 | + </MePermissionsProvider> |
| 203 | + </SchemaRendererProvider>, |
| 204 | + ); |
| 205 | +} |
| 206 | + |
| 207 | +const location = () => screen.getByTestId('location').textContent; |
| 208 | + |
| 209 | +/** The phone card showing `title`. */ |
| 210 | +async function phoneCard(title: string): Promise<HTMLElement> { |
| 211 | + const cards = await screen.findAllByTestId('grid-mobile-card', undefined, { timeout: 5000 }); |
| 212 | + const card = cards.find((c) => (c.textContent ?? '').includes(title)); |
| 213 | + expect(card).toBeTruthy(); |
| 214 | + return card as HTMLElement; |
| 215 | +} |
| 216 | + |
| 217 | +/** Whether `el` wears a clickable face: the pointer, or a hover wash. */ |
| 218 | +function looksClickable(el: HTMLElement): boolean { |
| 219 | + return /(^|\s)(cursor-pointer|hover:bg-accent\/50)(\s|$)/.test(el.className); |
| 220 | +} |
| 221 | + |
| 222 | +describe('objectstack#33719 — on a phone, a list authored to open nothing opens nothing', () => { |
| 223 | + it.each(NO_NAVIGATION)('%s: the card is not drawn tappable and a tap opens nothing', async (_name, navigation) => { |
| 224 | + mountList(navigation); |
| 225 | + const card = await phoneCard('Okta staging'); |
| 226 | + expect(looksClickable(card)).toBe(false); |
| 227 | + const before = location(); |
| 228 | + fireEvent.click(card); |
| 229 | + expect(location()).toBe(before); |
| 230 | + expect(location()).not.toContain('/record/'); |
| 231 | + expect(openSpy).not.toHaveBeenCalled(); |
| 232 | + }); |
| 233 | + |
| 234 | + it.each(NO_NAVIGATION)('%s: a `?recordId=` link opens no record page, as it opens no pane on a desktop', async (_name, navigation) => { |
| 235 | + mountList(navigation, '?recordId=c1'); |
| 236 | + // The list is drawn: the URL-sync effect has run by now. |
| 237 | + await phoneCard('Okta staging'); |
| 238 | + expect(location()).toBe(`${LIST_PATH}?recordId=c1`); |
| 239 | + expect(screen.queryByTestId('record-drawer-crumb')).toBeNull(); |
| 240 | + }); |
| 241 | + |
| 242 | + it('CONTROL — a list with no authored navigation: the card wears the pointer and a tap opens the record page', async () => { |
| 243 | + mountList(undefined); |
| 244 | + const card = await phoneCard('Okta staging'); |
| 245 | + expect(card.className).toMatch(/(^|\s)cursor-pointer(\s|$)/); |
| 246 | + fireEvent.click(card); |
| 247 | + await waitFor(() => expect(location()).toContain('/record/c1')); |
| 248 | + }); |
| 249 | + |
| 250 | + it('CONTROL — a list with no authored navigation: a `?recordId=` link opens the record page', async () => { |
| 251 | + mountList(undefined, '?recordId=c1'); |
| 252 | + await waitFor(() => expect(location()).toContain('/record/c1'), { timeout: 5000 }); |
| 253 | + }); |
| 254 | +}); |
0 commit comments