|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#13488 — the phone bar's title on a page route, and on a dashboard |
| 5 | + * route, names a page or dashboard whose `label` is the spec's inline locale |
| 6 | + * map (`I18nLabel`, `{ 'zh-CN', en }`) in the viewer's language. |
| 7 | + * |
| 8 | + * `PageSchema.label` and `DashboardSchema.label` are `string | I18nLabel`. The |
| 9 | + * page branch handed the served label to `pageLabel` as its fallback, which |
| 10 | + * returns an unmatched fallback untouched, and the dashboard branch drew the |
| 11 | + * served label as given: either way the map reached JSX as the phone bar's |
| 12 | + * title and React refused it (error #31), so the bar, and the route under it, |
| 13 | + * went blank. It is the page-route twin of the report crumb |
| 14 | + * (`AppHeader.reportCrumbInlineLocale-13470`). |
| 15 | + * |
| 16 | + * Harness: `AppHeader.reportCrumbInlineLocale-13470`'s, with `useObjectLabel` |
| 17 | + * REAL: the bundle carries no `pages` or `dashboards` key, so the title is the |
| 18 | + * resolved served label. The zh and en legs read different strings out of one |
| 19 | + * map; `PLAIN_STRING_CONTROL` is a page and a dashboard labelled with a plain |
| 20 | + * string, drawn unchanged. |
| 21 | + */ |
| 22 | +import '@testing-library/jest-dom/vitest'; |
| 23 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 24 | +import { render, cleanup } from '@testing-library/react'; |
| 25 | +import React from 'react'; |
| 26 | + |
| 27 | +const route = vi.hoisted(() => ({ pathname: '/apps/srm/page/welcome' })); |
| 28 | + |
| 29 | +vi.mock('react-router-dom', () => ({ |
| 30 | + useLocation: () => ({ pathname: route.pathname, search: '', hash: '', state: null, key: 't' }), |
| 31 | + useParams: () => ({ appName: 'srm' }), |
| 32 | + useNavigate: () => vi.fn(), |
| 33 | + useSearchParams: () => [new URLSearchParams(), vi.fn()] as const, |
| 34 | + Link: ({ children, to, ...p }: Record<string, unknown> & { children?: React.ReactNode }) => ( |
| 35 | + <a href={String(to)} {...p}>{children}</a> |
| 36 | + ), |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock('@object-ui/components', async (importOriginal) => { |
| 40 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 41 | + const strip = (p: Record<string, unknown> | undefined) => { |
| 42 | + const { asChild: _a, variant: _v, size: _s, align: _al, sideOffset: _so, onOpenChange: _o, ...rest } = p ?? {}; |
| 43 | + return rest; |
| 44 | + }; |
| 45 | + const Pass = ({ children, ...p }: Record<string, unknown> & { children?: React.ReactNode }) => ( |
| 46 | + <div {...strip(p)}>{children}</div> |
| 47 | + ); |
| 48 | + return { |
| 49 | + ...actual, |
| 50 | + Button: ({ children, asChild: _a, variant: _v, size: _s, ...p }: Record<string, unknown> & { children?: React.ReactNode }) => ( |
| 51 | + <button type="button" {...p}>{children}</button> |
| 52 | + ), |
| 53 | + DropdownMenu: Pass, |
| 54 | + DropdownMenuTrigger: Pass, |
| 55 | + DropdownMenuContent: () => null, |
| 56 | + DropdownMenuItem: () => null, |
| 57 | + DropdownMenuLabel: () => null, |
| 58 | + DropdownMenuSeparator: () => null, |
| 59 | + DropdownMenuGroup: Pass, |
| 60 | + Avatar: Pass, |
| 61 | + AvatarImage: () => null, |
| 62 | + AvatarFallback: Pass, |
| 63 | + Popover: Pass, |
| 64 | + PopoverTrigger: Pass, |
| 65 | + PopoverContent: () => null, |
| 66 | + cn: (...c: unknown[]) => c.filter(Boolean).join(' '), |
| 67 | + }; |
| 68 | +}); |
| 69 | + |
| 70 | +vi.mock('lucide-react', () => { |
| 71 | + const Icon = () => <span />; |
| 72 | + return new Proxy({ __esModule: true } as Record<string | symbol, unknown>, { |
| 73 | + get: (target, prop) => { |
| 74 | + if (prop === 'then' || prop === '__esModule' || typeof prop === 'symbol') return target[prop]; |
| 75 | + return Icon; |
| 76 | + }, |
| 77 | + has: (_target, prop) => prop !== 'then', |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 82 | + ...(await importOriginal<Record<string, unknown>>()), |
| 83 | + useOffline: () => ({ isOnline: true }), |
| 84 | +})); |
| 85 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 86 | + ...(await importOriginal<Record<string, unknown>>()), |
| 87 | + PresenceAvatars: () => null, |
| 88 | + useTenantPresence: () => [], |
| 89 | +})); |
| 90 | +vi.mock('../ModeToggle', () => ({ ModeToggle: () => null })); |
| 91 | +vi.mock('../LocaleSwitcher', () => ({ LocaleSwitcher: () => null })); |
| 92 | +vi.mock('../ConnectionStatus', () => ({ ConnectionStatus: () => null })); |
| 93 | +vi.mock('../AppSwitcher', () => ({ AppSwitcher: () => null })); |
| 94 | +vi.mock('../LocalizedSidebarTrigger', () => ({ LocalizedSidebarTrigger: () => null })); |
| 95 | +vi.mock('../PreviewBadge', () => ({ PreviewBadge: () => null })); |
| 96 | +vi.mock('../WorkspaceSwitcher', () => ({ WorkspaceSwitcher: () => null })); |
| 97 | + |
| 98 | +// ONE module-scope fetch double, never torn down: the header's pollers fire and |
| 99 | +// forget, and may read after a test body returns (see the sibling suites). |
| 100 | +vi.stubGlobal( |
| 101 | + 'fetch', |
| 102 | + vi.fn(async () => |
| 103 | + new Response(JSON.stringify({ data: [], items: [], agents: [], requests: [] }), { |
| 104 | + status: 200, |
| 105 | + headers: { 'Content-Type': 'application/json' }, |
| 106 | + }), |
| 107 | + ), |
| 108 | +); |
| 109 | + |
| 110 | +/** The page's inline map, and what each viewer must read out of it. */ |
| 111 | +const PAGE_LABEL_MAP = { 'zh-CN': '供应商门户首页', en: 'Supplier Portal Home' }; |
| 112 | +/** The dashboard's inline map. */ |
| 113 | +const DASHBOARD_LABEL_MAP = { 'zh-CN': '供应商绩效看板', en: 'Supplier Performance Board' }; |
| 114 | +const PLAIN_PAGE_LABEL = 'Welcome'; |
| 115 | +const PLAIN_DASHBOARD_LABEL = 'Ops Board'; |
| 116 | + |
| 117 | +vi.mock('../../providers/MetadataProvider', () => ({ |
| 118 | + useMetadata: () => ({ |
| 119 | + apps: [{ name: 'srm', label: 'SRM' }], |
| 120 | + dashboards: [ |
| 121 | + { name: 'srm_performance', label: DASHBOARD_LABEL_MAP }, |
| 122 | + { name: 'ops_board', label: PLAIN_DASHBOARD_LABEL }, |
| 123 | + ], |
| 124 | + pages: [ |
| 125 | + { name: 'welcome', type: 'app', label: PAGE_LABEL_MAP }, |
| 126 | + { name: 'welcome_plain', type: 'app', label: PLAIN_PAGE_LABEL }, |
| 127 | + ], |
| 128 | + reports: [], |
| 129 | + }), |
| 130 | +})); |
| 131 | +vi.mock('../../context/NavigationContext.js', () => ({ |
| 132 | + useNavigationContext: () => ({ currentAppName: 'srm', recordTitle: undefined }), |
| 133 | +})); |
| 134 | +vi.mock('@object-ui/auth', async (importOriginal) => { |
| 135 | + // ONE stable identity — a fresh closure per render re-arms every |
| 136 | + // `[getAuthConfig]` effect forever (see the sibling suites). |
| 137 | + const getAuthConfig = () => Promise.resolve({ features: { multiOrgEnabled: false } }); |
| 138 | + return { |
| 139 | + ...(await importOriginal<Record<string, unknown>>()), |
| 140 | + useAuth: () => ({ |
| 141 | + user: { id: 'u1', name: 'Zhang San', email: 'zs@example.com' }, |
| 142 | + signOut: vi.fn(), |
| 143 | + isAuthEnabled: true, |
| 144 | + organizations: [], |
| 145 | + activeOrganization: null, |
| 146 | + isOrganizationsLoading: false, |
| 147 | + switchOrganization: vi.fn(), |
| 148 | + getAuthConfig, |
| 149 | + }), |
| 150 | + getUserInitials: () => 'ZS', |
| 151 | + useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }), |
| 152 | + }; |
| 153 | +}); |
| 154 | +const fakeAdapter = { find: () => Promise.resolve({ data: [] }), getClient: () => undefined }; |
| 155 | +vi.mock('../../providers/AdapterProvider', () => ({ useAdapter: () => fakeAdapter })); |
| 156 | + |
| 157 | +import { I18nProvider } from '@object-ui/i18n'; |
| 158 | +import { AppHeader } from '../AppHeader'; |
| 159 | + |
| 160 | +afterEach(cleanup); |
| 161 | + |
| 162 | +type Lang = 'en' | 'zh-CN'; |
| 163 | + |
| 164 | +function renderAt(pathname: string, lang: Lang) { |
| 165 | + route.pathname = pathname; |
| 166 | + return render( |
| 167 | + <I18nProvider config={{ defaultLanguage: lang, detectBrowserLanguage: false }} persistLanguage={false}> |
| 168 | + <AppHeader variant="app" appName="SRM" activeAppName="srm" objects={[]} /> |
| 169 | + </I18nProvider>, |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +/** The phone bar's compact title: the bar's one `sm:hidden` title span. */ |
| 174 | +function phoneTitles(): string[] { |
| 175 | + return [...document.querySelectorAll('span.sm\\:hidden.truncate')].map((s) => s.textContent?.trim() ?? ''); |
| 176 | +} |
| 177 | + |
| 178 | +describe('a page route\'s phone title resolves an inline locale label (objectui#13488)', () => { |
| 179 | + it.each<[Lang, string]>([ |
| 180 | + ['en', PAGE_LABEL_MAP.en], |
| 181 | + ['zh-CN', PAGE_LABEL_MAP['zh-CN']], |
| 182 | + ])('PAGE_LABEL_MAP_RESOLVES — %s: the title is the viewer\'s label', (lang, expected) => { |
| 183 | + renderAt('/apps/srm/page/welcome', lang); |
| 184 | + expect(phoneTitles()).toEqual([expected]); |
| 185 | + }); |
| 186 | + |
| 187 | + it.each<Lang>(['en', 'zh-CN'])('PLAIN_STRING_CONTROL — %s: a plain-string page label draws unchanged', (lang) => { |
| 188 | + renderAt('/apps/srm/page/welcome_plain', lang); |
| 189 | + expect(phoneTitles()).toEqual([PLAIN_PAGE_LABEL]); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +describe('a dashboard route\'s phone title resolves an inline locale label (objectui#13488)', () => { |
| 194 | + it.each<[Lang, string]>([ |
| 195 | + ['en', DASHBOARD_LABEL_MAP.en], |
| 196 | + ['zh-CN', DASHBOARD_LABEL_MAP['zh-CN']], |
| 197 | + ])('DASHBOARD_LABEL_MAP_RESOLVES — %s: the title is the viewer\'s label', (lang, expected) => { |
| 198 | + renderAt('/apps/srm/dashboard/srm_performance', lang); |
| 199 | + expect(phoneTitles()).toEqual([expected]); |
| 200 | + }); |
| 201 | + |
| 202 | + it.each<Lang>(['en', 'zh-CN'])('PLAIN_STRING_CONTROL — %s: a plain-string dashboard label draws unchanged', (lang) => { |
| 203 | + renderAt('/apps/srm/dashboard/ops_board', lang); |
| 204 | + expect(phoneTitles()).toEqual([PLAIN_DASHBOARD_LABEL]); |
| 205 | + }); |
| 206 | +}); |
0 commit comments