|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// A field spells its help text `inlineHelpText` (`FieldSchema`), so that is the |
| 4 | +// authored default-locale text of `objects.<o>.fields.<f>.help` — the same |
| 5 | +// rule an authored label follows. Before, the key's inline source read only |
| 6 | +// `help ?? description`: an app authoring `inlineHelpText` and translating |
| 7 | +// `help` into one locale had the DEFAULT locale reported missing. |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { collectExpectedEntries } from '../src/utils/i18n-extract'; |
| 11 | +import { computeI18nCoverage } from '../src/utils/i18n-coverage'; |
| 12 | + |
| 13 | +const HELP_KEY = 'objects.kpi.fields.score.help'; |
| 14 | + |
| 15 | +const stack = (field: Record<string, unknown>, translations: any[]): any => ({ |
| 16 | + i18n: { defaultLocale: 'zh-CN', supportedLocales: ['zh-CN', 'en'] }, |
| 17 | + objects: [{ name: 'kpi', label: '指标', fields: { score: { label: '分数', ...field } } }], |
| 18 | + translations, |
| 19 | +}); |
| 20 | + |
| 21 | +const helpIssues = (report: ReturnType<typeof computeI18nCoverage>, locale: string) => |
| 22 | + report.issues.filter((i) => i.key === HELP_KEY && i.locale === locale); |
| 23 | + |
| 24 | +const enHelp = { en: { objects: { kpi: { fields: { score: { help: 'Full score' } } } } } }; |
| 25 | + |
| 26 | +describe('the field help key takes `inlineHelpText` as its default-locale source', () => { |
| 27 | + it('emits the key inline, from inlineHelpText', () => { |
| 28 | + const entry = collectExpectedEntries(stack({ inlineHelpText: '满分' }, [])).find((e) => e.path.join('.') === HELP_KEY); |
| 29 | + expect(entry?.inline).toBe('满分'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('orders the sources help, then inlineHelpText, then description', () => { |
| 33 | + const inlineOf = (field: Record<string, unknown>) => |
| 34 | + collectExpectedEntries(stack(field, [])).find((e) => e.path.join('.') === HELP_KEY)?.inline; |
| 35 | + expect(inlineOf({ help: 'H', inlineHelpText: 'I', description: 'D' })).toBe('H'); |
| 36 | + expect(inlineOf({ inlineHelpText: 'I', description: 'D' })).toBe('I'); |
| 37 | + expect(inlineOf({ description: 'D' })).toBe('D'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('does not report the default locale when an en bundle carries help and no zh-CN entry exists', () => { |
| 41 | + const report = computeI18nCoverage(stack({ inlineHelpText: '满分' }, [enHelp]), { strict: true }); |
| 42 | + expect(helpIssues(report, 'zh-CN')).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('still reports a non-default locale that carries no help', () => { |
| 46 | + const report = computeI18nCoverage(stack({ inlineHelpText: '满分' }, [{ 'zh-CN': {} }, enHelp]), { |
| 47 | + strict: true, |
| 48 | + defaultLocale: 'en', |
| 49 | + }); |
| 50 | + // `en` is the default here: zh-CN is the non-default locale and owes help. |
| 51 | + expect(helpIssues(report, 'zh-CN').length).toBe(1); |
| 52 | + expect(helpIssues(report, 'en')).toEqual([]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keeps the description-only case unchanged: description still seeds the default locale', () => { |
| 56 | + const report = computeI18nCoverage(stack({ description: '说明' }, [enHelp]), { strict: true }); |
| 57 | + expect(helpIssues(report, 'zh-CN')).toEqual([]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('keeps the no-inline-source case unchanged: the default locale is still reported', () => { |
| 61 | + const report = computeI18nCoverage(stack({}, [enHelp]), { strict: true }); |
| 62 | + expect(helpIssues(report, 'zh-CN').length).toBe(1); |
| 63 | + }); |
| 64 | +}); |
0 commit comments