|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29235 — a recent dashboard, page or report row reads its item by |
| 5 | + * name, never its type's whole list. |
| 6 | + * |
| 7 | + * `useRecentItemLabel` labelled those rows from `metadata.dashboards` (through |
| 8 | + * `useNavTargetLabel`), `metadata.pages` and `metadata.reports`, so a Home with |
| 9 | + * one such recent row downloaded the whole `/meta/dashboard`, `/meta/page` or |
| 10 | + * `/meta/report` list for one label. Each row now asks its own item through |
| 11 | + * the provider's by-name read (`useReadItemsByName`), and reads the list only |
| 12 | + * once something else has loaded it. |
| 13 | + * |
| 14 | + * Real subjects: `useRecentItemLabel`, `MetadataProvider` and the i18n |
| 15 | + * provider, over a fake adapter that records each read. What the rows print is |
| 16 | + * pinned beside the reads: the same text the whole list gave, in en and in |
| 17 | + * zh-CN (an inline locale map, and a translation bundle key), and the machine |
| 18 | + * name while nothing has landed. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import type { ReactNode } from 'react'; |
| 23 | +import { renderHook, waitFor, cleanup, act } from '@testing-library/react'; |
| 24 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 25 | +import { useMetadata } from '@object-ui/react'; |
| 26 | +import { MetadataProvider } from '../../providers/MetadataProvider'; |
| 27 | +import { useRecentItemLabel } from '../useRecentItemLabel'; |
| 28 | +import type { RecentItem } from '../../context/RecentItemsProvider'; |
| 29 | + |
| 30 | +const ITEMS: Record<string, Array<Record<string, unknown>>> = { |
| 31 | + dashboard: [ |
| 32 | + { name: 'pipeline', label: 'Pipeline' }, |
| 33 | + { name: 'other_dashboard', label: 'Other' }, |
| 34 | + ], |
| 35 | + page: [ |
| 36 | + { name: 'welcome', label: { en: 'Welcome', 'zh-CN': '欢迎' } }, |
| 37 | + { name: 'other_page', label: 'Other page' }, |
| 38 | + ], |
| 39 | + report: [ |
| 40 | + { name: 'q3_results', label: 'Q3 Results' }, |
| 41 | + { name: 'other_report', label: 'Other report' }, |
| 42 | + ], |
| 43 | +}; |
| 44 | + |
| 45 | +function makeAdapter(calls: string[]) { |
| 46 | + return { |
| 47 | + clearCache: vi.fn(), |
| 48 | + invalidateCache: vi.fn(), |
| 49 | + getClient: () => ({ |
| 50 | + meta: { |
| 51 | + getItems: (type: string) => { |
| 52 | + calls.push(type); |
| 53 | + return Promise.resolve({ type, items: structuredClone(ITEMS[type] ?? []) }); |
| 54 | + }, |
| 55 | + getItem: (type: string, name: string) => { |
| 56 | + calls.push(`${type}/${name}`); |
| 57 | + const item = (ITEMS[type] ?? []).find((it) => it.name === name); |
| 58 | + if (!item) return Promise.reject(Object.assign(new Error('not found'), { status: 404, code: 'NOT_FOUND' })); |
| 59 | + return Promise.resolve({ item: structuredClone(item) }); |
| 60 | + }, |
| 61 | + }, |
| 62 | + }), |
| 63 | + } as unknown as Parameters<typeof MetadataProvider>[0]['adapter']; |
| 64 | +} |
| 65 | + |
| 66 | +function wrapperFor(calls: string[], language = 'en') { |
| 67 | + const i18n = createI18n({ |
| 68 | + defaultLanguage: language, |
| 69 | + detectBrowserLanguage: false, |
| 70 | + resources: { 'zh-CN': { showcase: { reports: { q3_results: { label: '第三季度结果' } } } } }, |
| 71 | + }); |
| 72 | + const adapter = makeAdapter(calls); |
| 73 | + return ({ children }: { children: ReactNode }) => ( |
| 74 | + <I18nProvider instance={i18n} persistLanguage={false}> |
| 75 | + <MetadataProvider adapter={adapter}>{children}</MetadataProvider> |
| 76 | + </I18nProvider> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +const at = '2026-11-02T09:00:00.000Z'; |
| 81 | +const ROWS: RecentItem[] = [ |
| 82 | + { id: 'dashboard:pipeline', type: 'dashboard', name: 'pipeline', href: '/d', visitedAt: at }, |
| 83 | + { id: 'page:welcome', type: 'page', name: 'welcome', href: '/p', visitedAt: at }, |
| 84 | + { id: 'report:q3_results', type: 'report', name: 'q3_results', href: '/r', visitedAt: at }, |
| 85 | +]; |
| 86 | + |
| 87 | +/** The whole-list reads of the three types. */ |
| 88 | +const listReads = (calls: string[]) => calls.filter((c) => c === 'dashboard' || c === 'page' || c === 'report'); |
| 89 | + |
| 90 | +const settle = () => act(() => new Promise((resolve) => setTimeout(resolve, 50))); |
| 91 | + |
| 92 | +afterEach(() => cleanup()); |
| 93 | + |
| 94 | +describe('recent dashboard / page / report rows read their item by name (objectstack#29235)', () => { |
| 95 | + it('each row asks its own item, never its type’s list, and prints what the list gave', async () => { |
| 96 | + const calls: string[] = []; |
| 97 | + const { result } = renderHook(() => useRecentItemLabel(), { wrapper: wrapperFor(calls) }); |
| 98 | + await waitFor(() => expect(ROWS.map((row) => result.current(row))).toEqual(['Pipeline', 'Welcome', 'Q3 Results'])); |
| 99 | + await settle(); |
| 100 | + expect(listReads(calls)).toEqual([]); |
| 101 | + expect(calls.filter((c) => c.includes('/')).sort()).toEqual(['dashboard/pipeline', 'page/welcome', 'report/q3_results']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('zh-CN: an inline locale map and a translation bundle key read as the list gave them', async () => { |
| 105 | + const calls: string[] = []; |
| 106 | + const { result } = renderHook(() => useRecentItemLabel(), { wrapper: wrapperFor(calls, 'zh-CN') }); |
| 107 | + await waitFor(() => expect(ROWS.map((row) => result.current(row))).toEqual(['Pipeline', '欢迎', '第三季度结果'])); |
| 108 | + expect(listReads(calls)).toEqual([]); |
| 109 | + }); |
| 110 | + |
| 111 | + it('the machine name until the item lands, and for an item the server does not have (asked once)', async () => { |
| 112 | + const calls: string[] = []; |
| 113 | + const gone: RecentItem = { id: 'report:gone', type: 'report', name: 'gone', href: '/r', visitedAt: at }; |
| 114 | + const { result, rerender } = renderHook(() => useRecentItemLabel(), { wrapper: wrapperFor(calls) }); |
| 115 | + expect(ROWS.map((row) => result.current(row))).toEqual(['pipeline', 'welcome', 'q3_results']); |
| 116 | + expect(result.current(gone)).toBe('gone'); |
| 117 | + await waitFor(() => expect(result.current(ROWS[2])).toBe('Q3 Results')); |
| 118 | + await settle(); |
| 119 | + rerender(); |
| 120 | + expect(result.current(gone)).toBe('gone'); |
| 121 | + expect(calls.filter((c) => c === 'report/gone')).toEqual(['report/gone']); |
| 122 | + expect(listReads(calls)).toEqual([]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('CONTROL: once the lists are loaded, the rows read them and ask no item', async () => { |
| 126 | + const calls: string[] = []; |
| 127 | + const { result } = renderHook( |
| 128 | + () => { |
| 129 | + const metadata = useMetadata(); |
| 130 | + return { label: useRecentItemLabel(), metadata }; |
| 131 | + }, |
| 132 | + { wrapper: wrapperFor(calls) }, |
| 133 | + ); |
| 134 | + await act(async () => { |
| 135 | + await Promise.all(['dashboard', 'page', 'report'].map((type) => result.current.metadata.ensureType(type))); |
| 136 | + }); |
| 137 | + await waitFor(() => expect(ROWS.map((row) => result.current.label(row))).toEqual(['Pipeline', 'Welcome', 'Q3 Results'])); |
| 138 | + await settle(); |
| 139 | + expect(listReads(calls).sort()).toEqual(['dashboard', 'page', 'report']); |
| 140 | + expect(calls.filter((c) => c.includes('/'))).toEqual([]); |
| 141 | + }); |
| 142 | +}); |
0 commit comments