|
| 1 | +/** |
| 2 | + * A record route reads the objects of the record page it RENDERS |
| 3 | + * (objectui#13046), so the route's object scope must pick the page the way |
| 4 | + * `RecordDetailView` does through `usePageAssignment`. With app-scoped record |
| 5 | + * pages (`PageSchema.apps`, maintainer ruling A on objectstack#28979) that |
| 6 | + * pick depends on the route's app: inside an app a page names, that page; |
| 7 | + * elsewhere the object's default record page. |
| 8 | + * |
| 9 | + * Harness: `readAppObjectScope` with the app `AppContent` resolved from the |
| 10 | + * route segment, a settled page list, and a scope reader that records the |
| 11 | + * names it is asked. Each page binds one marker object, so the names asked |
| 12 | + * say which page was picked. The same matrix is asked of `usePageAssignment` |
| 13 | + * with the same app's name, and the two answers must agree. |
| 14 | + */ |
| 15 | + |
| 16 | +import React from 'react'; |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 19 | +import { MetadataCtx, usePageAssignment, type MetadataContextValue } from '@object-ui/react'; |
| 20 | +import { readAppObjectScope } from '../appObjectScope'; |
| 21 | + |
| 22 | +type Page = Record<string, unknown>; |
| 23 | + |
| 24 | +const OBJECT = 'sys_user'; |
| 25 | + |
| 26 | +/** A full record page of the user object binding one marker object. */ |
| 27 | +const userPage = (name: string, marker: string, extra: Page = {}): Page => ({ |
| 28 | + name, |
| 29 | + label: name, |
| 30 | + type: 'record', |
| 31 | + object: OBJECT, |
| 32 | + ...extra, |
| 33 | + regions: [{ name: 'main', components: [{ type: 'object-grid', objectName: marker }] }], |
| 34 | +}); |
| 35 | + |
| 36 | +const SYS_USER_DETAIL = userPage('sys_user_detail', 'marker_default', { isDefault: true }); |
| 37 | +const DULY_MEMBER = userPage('duly_member', 'marker_duly', { apps: ['duly'] }); |
| 38 | +const MARKER_OF: Record<string, string> = { sys_user_detail: 'marker_default', duly_member: 'marker_duly' }; |
| 39 | + |
| 40 | +const SETUP_APP = { name: 'setup', _packageId: 'com.objectstack.setup', navigation: [] }; |
| 41 | +const DULY_APP = { name: 'duly', _packageId: 'com.duly', navigation: [] }; |
| 42 | + |
| 43 | +/** The page `readAppObjectScope` picked, read off the marker names it asked for. */ |
| 44 | +function scopePick(app: Record<string, unknown>, pages: Page[]): string | undefined { |
| 45 | + const asked = new Set<string>(); |
| 46 | + const readObjectScope = ((q: { names?: readonly string[] }) => { |
| 47 | + for (const n of q.names ?? []) asked.add(n); |
| 48 | + return { objects: [], served: [], missing: [], settled: true }; |
| 49 | + }) as never; |
| 50 | + readAppObjectScope(readObjectScope, app, `${OBJECT}/record/r1`, '', [], undefined, () => ({ pages, settled: true })); |
| 51 | + const picked = Object.entries(MARKER_OF).filter(([, marker]) => asked.has(marker)).map(([name]) => name); |
| 52 | + expect(picked.length, `exactly one page's objects are read, got ${picked.join(', ') || 'none'}`).toBe(1); |
| 53 | + return picked[0]; |
| 54 | +} |
| 55 | + |
| 56 | +/** The page `usePageAssignment` picks for the same app's name. */ |
| 57 | +async function assignmentPick(appName: string, pages: Page[]): Promise<string | undefined> { |
| 58 | + const ctx = { |
| 59 | + apps: [], objects: [], dashboards: [], reports: [], pages, loading: false, error: null, |
| 60 | + refresh: async () => {}, invalidate: () => {}, ensureType: async () => pages, |
| 61 | + getItem: async () => null, getItemsByType: () => [], getTypeStatus: () => 'ready' as const, |
| 62 | + } as unknown as MetadataContextValue; |
| 63 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 64 | + <MetadataCtx.Provider value={ctx}>{children}</MetadataCtx.Provider> |
| 65 | + ); |
| 66 | + const { result } = renderHook(() => usePageAssignment(OBJECT, { app: appName }), { wrapper }); |
| 67 | + await waitFor(() => expect(result.current.loading).toBe(false)); |
| 68 | + return result.current.page?.name; |
| 69 | +} |
| 70 | + |
| 71 | +describe('appObjectScope — a record route reads the objects of the app-scoped page it renders (ruling A on objectstack#28979)', () => { |
| 72 | + it('inside the Duly app the route reads duly_member\'s objects; inside Setup, sys_user_detail\'s', () => { |
| 73 | + expect(scopePick(DULY_APP, [SYS_USER_DETAIL, DULY_MEMBER])).toBe('duly_member'); |
| 74 | + expect(scopePick(SETUP_APP, [DULY_MEMBER, SYS_USER_DETAIL])).toBe('sys_user_detail'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('the scope picker and usePageAssignment agree on every app and order', async () => { |
| 78 | + for (const app of [DULY_APP, SETUP_APP]) { |
| 79 | + for (const pages of [[SYS_USER_DETAIL, DULY_MEMBER], [DULY_MEMBER, SYS_USER_DETAIL]]) { |
| 80 | + expect(scopePick(app, pages)).toBe(await assignmentPick(app.name, pages)); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('CONTROL: with no `apps` on any page, every app reads the default page\'s objects', () => { |
| 86 | + const plain = [{ ...DULY_MEMBER, apps: undefined }, SYS_USER_DETAIL]; |
| 87 | + expect(scopePick(DULY_APP, plain)).toBe('sys_user_detail'); |
| 88 | + expect(scopePick(SETUP_APP, plain)).toBe('sys_user_detail'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments