|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #25901 — on a `/apps/APP/OBJECT/view/NAME` route, a view whose `label` is an |
| 5 | + * inline locale map is titled in the viewer's language. |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * `ListViewSchema` types a view's `label` as `I18nLabel`: a string or an |
| 10 | + * inline locale map. The server overlays only catalog text |
| 11 | + * (`translateObjectListViews`), so a map with no `_views.KEY.label` catalog |
| 12 | + * entry is served as authored. `viewTabLabel` handed it on untouched — drawn |
| 13 | + * as given on the served branch, offered to the bundle as the fallback on the |
| 14 | + * other — and the header rendered the object as a React child. The header is |
| 15 | + * mounted outside the route's error boundary, so the throw unmounted the whole |
| 16 | + * console. Measured before the change with this file's harness: both cells |
| 17 | + * below threw `Objects are not valid as a React child (found: object with keys |
| 18 | + * {en, zh-CN})` and left the body empty. |
| 19 | + * |
| 20 | + * ## The rule |
| 21 | + * |
| 22 | + * `viewTabLabel` resolves the label with `metadataText` in the viewer's |
| 23 | + * language before either branch, so every caller — this header, the tab bar, |
| 24 | + * the mobile switcher, the record page's way back — receives a string. A map |
| 25 | + * that resolves to no text counts as no label: the object's plural label. |
| 26 | + * |
| 27 | + * Harness: `AppHeader.labelLessViewTitle-12805`'s, with an empty translation |
| 28 | + * bundle so no catalog entry can stand in for the map. |
| 29 | + * |
| 30 | + * Directions, written before the run: the map cells RED before the change (the |
| 31 | + * throw), GREEN after; the empty-map cell draws the plural label. |
| 32 | + */ |
| 33 | +import '@testing-library/jest-dom/vitest'; |
| 34 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 35 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 36 | + |
| 37 | +const route = vi.hoisted(() => ({ pathname: '/apps/showcase_app/recheck_gate/view/default' })); |
| 38 | +const meta = vi.hoisted(() => ({ value: null as any })); |
| 39 | + |
| 40 | +vi.mock('react-router-dom', () => ({ |
| 41 | + useLocation: () => ({ pathname: route.pathname, search: '', hash: '', state: null, key: 't' }), |
| 42 | + useParams: () => ({ appName: 'showcase_app' }), |
| 43 | + useNavigate: () => vi.fn(), |
| 44 | + useSearchParams: () => [new URLSearchParams(), vi.fn()] as const, |
| 45 | + Link: ({ children, to, ...p }: any) => <a href={String(to)} {...p}>{children}</a>, |
| 46 | +})); |
| 47 | + |
| 48 | +vi.mock('@object-ui/components', async (importOriginal) => { |
| 49 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 50 | + const stripProps = (p: any) => { |
| 51 | + const { asChild, variant, size, align, sideOffset, onOpenChange, ...rest } = p ?? {}; |
| 52 | + return rest; |
| 53 | + }; |
| 54 | + const Pass = ({ children, ...p }: any) => <div {...stripProps(p)}>{children}</div>; |
| 55 | + return { |
| 56 | + ...actual, |
| 57 | + Button: ({ children, asChild, variant, size, ...p }: any) => ( |
| 58 | + <button type="button" {...p}>{children}</button> |
| 59 | + ), |
| 60 | + DropdownMenu: Pass, |
| 61 | + DropdownMenuTrigger: Pass, |
| 62 | + DropdownMenuContent: () => null, |
| 63 | + DropdownMenuItem: () => null, |
| 64 | + DropdownMenuLabel: () => null, |
| 65 | + DropdownMenuSeparator: () => null, |
| 66 | + DropdownMenuGroup: Pass, |
| 67 | + Avatar: Pass, |
| 68 | + AvatarImage: () => null, |
| 69 | + AvatarFallback: Pass, |
| 70 | + Popover: Pass, |
| 71 | + PopoverTrigger: Pass, |
| 72 | + PopoverContent: () => null, |
| 73 | + Tabs: Pass, |
| 74 | + TabsList: Pass, |
| 75 | + TabsTrigger: ({ children }: any) => <button type="button">{children}</button>, |
| 76 | + TabsContent: Pass, |
| 77 | + cn: (...c: any[]) => c.filter(Boolean).join(' '), |
| 78 | + }; |
| 79 | +}); |
| 80 | + |
| 81 | +vi.mock('lucide-react', () => { |
| 82 | + const Icon = () => <span />; |
| 83 | + return new Proxy({ __esModule: true } as Record<string | symbol, unknown>, { |
| 84 | + get: (target, prop) => { |
| 85 | + if (prop === 'then' || prop === '__esModule' || typeof prop === 'symbol') return target[prop]; |
| 86 | + return Icon; |
| 87 | + }, |
| 88 | + has: (_target, prop) => prop !== 'then', |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 93 | + ...(await importOriginal<Record<string, unknown>>()), |
| 94 | + useOffline: () => ({ isOnline: true }), |
| 95 | +})); |
| 96 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 97 | + ...(await importOriginal<Record<string, unknown>>()), |
| 98 | + PresenceAvatars: () => null, |
| 99 | + useTenantPresence: () => [], |
| 100 | +})); |
| 101 | +vi.mock('../ModeToggle', () => ({ ModeToggle: () => null })); |
| 102 | +vi.mock('../LocaleSwitcher', () => ({ LocaleSwitcher: () => null })); |
| 103 | +vi.mock('../ConnectionStatus', () => ({ ConnectionStatus: () => null })); |
| 104 | +vi.mock('../AppSwitcher', () => ({ AppSwitcher: () => null })); |
| 105 | +vi.mock('../LocalizedSidebarTrigger', () => ({ LocalizedSidebarTrigger: () => null })); |
| 106 | +vi.mock('../PreviewBadge', () => ({ PreviewBadge: () => null })); |
| 107 | +vi.mock('../WorkspaceSwitcher', () => ({ WorkspaceSwitcher: () => null })); |
| 108 | + |
| 109 | +// ONE module-scope fetch double, never torn down: the header's pollers fire and |
| 110 | +// forget, and may read after a test body returns (see the sibling suites). |
| 111 | +vi.stubGlobal( |
| 112 | + 'fetch', |
| 113 | + vi.fn(async () => |
| 114 | + new Response(JSON.stringify({ data: [], items: [], agents: [], requests: [] }), { |
| 115 | + status: 200, |
| 116 | + headers: { 'Content-Type': 'application/json' }, |
| 117 | + }), |
| 118 | + ), |
| 119 | +); |
| 120 | + |
| 121 | +// The header reads its lists through this module; the served-view question goes |
| 122 | +// to the same context through `@object-ui/react` (provided below) — one value. |
| 123 | +vi.mock('../../providers/MetadataProvider', () => ({ useMetadata: () => meta.value })); |
| 124 | +vi.mock('../../context/NavigationContext.js', () => ({ |
| 125 | + useNavigationContext: () => ({ currentAppName: 'showcase_app', recordTitle: undefined }), |
| 126 | +})); |
| 127 | +vi.mock('@object-ui/auth', async (importOriginal) => { |
| 128 | + // ONE stable identity — a fresh closure per render re-arms every |
| 129 | + // `[getAuthConfig]` effect forever (see the sibling suites). |
| 130 | + const getAuthConfig = () => Promise.resolve({ features: { multiOrgEnabled: false } }); |
| 131 | + return { |
| 132 | + ...(await importOriginal<Record<string, unknown>>()), |
| 133 | + useAuth: () => ({ |
| 134 | + user: { id: 'u1', name: 'Zhang San', email: 'zs@example.com' }, |
| 135 | + signOut: vi.fn(), |
| 136 | + isAuthEnabled: true, |
| 137 | + organizations: [], |
| 138 | + activeOrganization: null, |
| 139 | + isOrganizationsLoading: false, |
| 140 | + switchOrganization: vi.fn(), |
| 141 | + getAuthConfig, |
| 142 | + }), |
| 143 | + getUserInitials: () => 'ZS', |
| 144 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 145 | + }; |
| 146 | +}); |
| 147 | +const fakeAdapter = { find: () => Promise.resolve({ data: [] }), getClient: () => undefined }; |
| 148 | +vi.mock('../../providers/AdapterProvider', () => ({ useAdapter: () => fakeAdapter })); |
| 149 | + |
| 150 | +import { I18nProvider } from '@object-ui/i18n'; |
| 151 | +import { MetadataCtx } from '@object-ui/react'; |
| 152 | +import { AppHeader } from '../AppHeader'; |
| 153 | + |
| 154 | +afterEach(cleanup); |
| 155 | + |
| 156 | +const OBJECT_NAME = 'recheck_gate'; |
| 157 | +const PLURAL = 'QA Gates'; |
| 158 | +const SERVED_LABEL = { en: 'My gates', 'zh-CN': '我的关卡' } as const; |
| 159 | +const DERIVED_LABEL = { en: 'Derived gates', 'zh-CN': '派生关卡' } as const; |
| 160 | + |
| 161 | +/** No catalog entry for any view: the map is the only text there is. */ |
| 162 | +const BUNDLE = { en: {}, 'zh-CN': {} }; |
| 163 | + |
| 164 | +/** |
| 165 | + * The `/meta/object` answer embeds `served_map` (a served view) and |
| 166 | + * `empty_map`; the object as `MetadataProvider` merges it also carries |
| 167 | + * `derived_map`, which no server read served. |
| 168 | + */ |
| 169 | +function world() { |
| 170 | + const doc = { |
| 171 | + name: OBJECT_NAME, |
| 172 | + label: 'QA Gate', |
| 173 | + pluralLabel: PLURAL, |
| 174 | + listViews: { |
| 175 | + served_map: { type: 'grid', label: SERVED_LABEL }, |
| 176 | + empty_map: { type: 'grid', label: {} }, |
| 177 | + }, |
| 178 | + }; |
| 179 | + const objects = [{ ...doc, listViews: { ...doc.listViews, derived_map: { type: 'grid', label: DERIVED_LABEL } } }]; |
| 180 | + const objectAnswer = [doc]; |
| 181 | + meta.value = { |
| 182 | + apps: [], |
| 183 | + objects, |
| 184 | + dashboards: [], |
| 185 | + reports: [], |
| 186 | + pages: [], |
| 187 | + loading: false, |
| 188 | + error: null, |
| 189 | + refresh: async () => {}, |
| 190 | + invalidate: () => {}, |
| 191 | + ensureType: async () => [], |
| 192 | + getItem: async () => null, |
| 193 | + getItemsByType: (type: string) => (type === 'object' ? objectAnswer : []), |
| 194 | + getTypeStatus: () => 'ready', |
| 195 | + }; |
| 196 | + return objects; |
| 197 | +} |
| 198 | + |
| 199 | +function renderAt(pathname: string, language: 'en' | 'zh-CN', objects: any[]) { |
| 200 | + route.pathname = pathname; |
| 201 | + return render( |
| 202 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false, resources: BUNDLE }} persistLanguage={false}> |
| 203 | + <MetadataCtx.Provider value={meta.value}> |
| 204 | + <AppHeader variant="app" appName="showcase_app" activeAppName="showcase_app" objects={objects} /> |
| 205 | + </MetadataCtx.Provider> |
| 206 | + </I18nProvider>, |
| 207 | + ); |
| 208 | +} |
| 209 | + |
| 210 | +const LANGUAGES = ['en', 'zh-CN'] as const; |
| 211 | + |
| 212 | +describe('AppHeader — a view labelled with an inline locale map is titled in the viewer\'s language (#25901)', () => { |
| 213 | + it.each(LANGUAGES)('%s: a served view', (language) => { |
| 214 | + renderAt(`/apps/showcase_app/${OBJECT_NAME}/view/served_map`, language, world()); |
| 215 | + |
| 216 | + expect(screen.getAllByText(SERVED_LABEL[language]).length).toBeGreaterThan(0); |
| 217 | + }); |
| 218 | + |
| 219 | + it.each(LANGUAGES)('%s: a view no server read served', (language) => { |
| 220 | + renderAt(`/apps/showcase_app/${OBJECT_NAME}/view/derived_map`, language, world()); |
| 221 | + |
| 222 | + expect(screen.getAllByText(DERIVED_LABEL[language]).length).toBeGreaterThan(0); |
| 223 | + }); |
| 224 | + |
| 225 | + it('a map that resolves to no text is titled by the object\'s plural label', () => { |
| 226 | + renderAt(`/apps/showcase_app/${OBJECT_NAME}/view/empty_map`, 'en', world()); |
| 227 | + |
| 228 | + expect(screen.getAllByText(PLURAL).length).toBeGreaterThan(0); |
| 229 | + }); |
| 230 | +}); |
0 commit comments