|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Pin: `translateApp` localizes a navigation entry's label by the entry's |
| 5 | + * `id`, never by the label's text — the "never translated by matching its |
| 6 | + * text" half of the order `BaseNavItemSchema.label` states in `ui/app.zod.ts`. |
| 7 | + * |
| 8 | + * Every entry below carries a label EQUAL to its target's machine name, the |
| 9 | + * one shape a text-matching rule would treat as "not customised". The bundle |
| 10 | + * holds every text-keyed route such a rule could take: the target's own |
| 11 | + * translation (`objects.<object>.label`, `objects.<object>._views.<view>.label`, |
| 12 | + * `dashboards.<dashboard>.label`) and a navigation key spelled like the text. |
| 13 | + * With no key for the entry's `id`, the label must come back unchanged; with |
| 14 | + * one, the answer must be that key's value and nothing else. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import { translateApp } from './i18n-resolver'; |
| 19 | +import { TranslationBundleSchema, type TranslationBundle } from './translation.zod'; |
| 20 | +import { AppSchema } from '../ui/app.zod'; |
| 21 | + |
| 22 | +const app = { |
| 23 | + name: 'crm', |
| 24 | + label: 'CRM', |
| 25 | + navigation: [ |
| 26 | + { id: 'nav_account', type: 'object', objectName: 'account', label: 'account' }, |
| 27 | + { id: 'nav_pipeline', type: 'object', objectName: 'opportunity', viewName: 'pipeline', label: 'pipeline' }, |
| 28 | + { id: 'nav_sales_overview', type: 'dashboard', dashboardName: 'sales_overview', label: 'sales_overview' }, |
| 29 | + ], |
| 30 | +}; |
| 31 | + |
| 32 | +/** Every route a text match could take, and no key for any entry's `id`. */ |
| 33 | +const textKeyedOnly: TranslationBundle = { |
| 34 | + 'zh-CN': { |
| 35 | + objects: { |
| 36 | + account: { label: '客户' }, |
| 37 | + opportunity: { label: '商机', _views: { pipeline: { label: '销售管道' } } }, |
| 38 | + }, |
| 39 | + dashboards: { sales_overview: { label: '销售总览' } }, |
| 40 | + apps: { |
| 41 | + crm: { |
| 42 | + label: '客户关系', |
| 43 | + navigation: { |
| 44 | + account: { label: '按文本-客户' }, |
| 45 | + pipeline: { label: '按文本-管道' }, |
| 46 | + sales_overview: { label: '按文本-总览' }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +/** The same bundle plus one key per entry `id`. */ |
| 54 | +const withIdKeys: TranslationBundle = { |
| 55 | + 'zh-CN': { |
| 56 | + ...textKeyedOnly['zh-CN'], |
| 57 | + apps: { |
| 58 | + crm: { |
| 59 | + label: '客户关系', |
| 60 | + navigation: { |
| 61 | + ...textKeyedOnly['zh-CN'].apps!.crm.navigation, |
| 62 | + nav_account: { label: '我的客户' }, |
| 63 | + nav_pipeline: { label: '我的管道' }, |
| 64 | + nav_sales_overview: { label: '我的总览' }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +const labels = (out: typeof app) => out.navigation.map((n) => n.label); |
| 72 | + |
| 73 | +describe('translateApp — a nav label equal to its target name localizes only through the entry id', () => { |
| 74 | + it('both fixtures are authorable: the app and the bundles parse', () => { |
| 75 | + expect(AppSchema.safeParse(app).success).toBe(true); |
| 76 | + expect(TranslationBundleSchema.safeParse(textKeyedOnly).success).toBe(true); |
| 77 | + expect(TranslationBundleSchema.safeParse(withIdKeys).success).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('with no key for the entry id, the label comes back unchanged', () => { |
| 81 | + const out = translateApp(app, textKeyedOnly, { locale: 'zh-CN' }); |
| 82 | + expect(labels(out)).toEqual(['account', 'pipeline', 'sales_overview']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('with a key for the entry id, the label is that key and nothing else', () => { |
| 86 | + const out = translateApp(app, withIdKeys, { locale: 'zh-CN' }); |
| 87 | + expect(labels(out)).toEqual(['我的客户', '我的管道', '我的总览']); |
| 88 | + }); |
| 89 | +}); |
0 commit comments