|
| 1 | +// Lock the SPA chrome i18n behaviour (#630): |
| 2 | +// 1. `t(en)` returns the English source string when the catalog |
| 3 | +// has no entry for it (graceful degradation; consumers see |
| 4 | +// legible text mid-migration as keys land). |
| 5 | +// 2. `loadCatalog(lang)` swaps the active catalog by exact code, |
| 6 | +// then by language stem (`es-AR` → `es`), then no-ops on |
| 7 | +// unknown codes (keeps English). |
| 8 | +// 3. The bundled non-English catalogs actually translate the |
| 9 | +// sample keys we expect — sanity check that the JSON files |
| 10 | +// aren't empty / corrupted in the build pipeline. |
| 11 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 12 | + |
| 13 | +import { |
| 14 | + _activeCatalogForTests, |
| 15 | + _setActiveCatalogForTests, |
| 16 | + loadCatalog, |
| 17 | + t, |
| 18 | +} from './i18n'; |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + // Reset to the bundled English catalog before each test so cases |
| 22 | + // don't leak state through the shared module-level binding. |
| 23 | + loadCatalog('en'); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('t (source-as-key translation lookup)', () => { |
| 27 | + it('returns the English source when the active catalog has no entry', () => { |
| 28 | + _setActiveCatalogForTests({}); |
| 29 | + expect(t('Brand new string the catalog has never seen')).toBe( |
| 30 | + 'Brand new string the catalog has never seen', |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns the translation when the catalog has an entry', () => { |
| 35 | + _setActiveCatalogForTests({ Add: 'Añadir' }); |
| 36 | + expect(t('Add')).toBe('Añadir'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('falls back to the English source for a partial catalog', () => { |
| 40 | + _setActiveCatalogForTests({ Add: 'Añadir' }); |
| 41 | + expect(t('Add')).toBe('Añadir'); // translated |
| 42 | + expect(t('Search')).toBe('Search'); // not in this fixture → English |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('loadCatalog (language → catalog selection)', () => { |
| 47 | + it('matches an exact language code', () => { |
| 48 | + loadCatalog('es'); |
| 49 | + expect(t('Add')).toBe('Añadir'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('matches by language stem when the full code is unknown (es-AR → es)', () => { |
| 53 | + loadCatalog('es-AR'); |
| 54 | + expect(t('Add')).toBe('Añadir'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('matches case-insensitively (ES → es)', () => { |
| 58 | + loadCatalog('ES'); |
| 59 | + expect(t('Add')).toBe('Añadir'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('matches pt-br to the pt catalog', () => { |
| 63 | + loadCatalog('pt-br'); |
| 64 | + expect(t('Save')).toBe('Salvar'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('falls back to English (no-op) on unknown codes', () => { |
| 68 | + _setActiveCatalogForTests({ Add: 'Añadir' }); // start non-English |
| 69 | + loadCatalog('zz-ZZ'); // unknown — leaves the previous catalog active |
| 70 | + expect(t('Add')).toBe('Añadir'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('is a no-op when language is null / empty', () => { |
| 74 | + loadCatalog('es'); |
| 75 | + loadCatalog(null); |
| 76 | + expect(t('Add')).toBe('Añadir'); // still Spanish |
| 77 | + loadCatalog(''); |
| 78 | + expect(t('Add')).toBe('Añadir'); // still Spanish |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('bundled catalog sanity (#630 ship-set)', () => { |
| 83 | + it('Spanish translates the common chrome strings', () => { |
| 84 | + loadCatalog('es'); |
| 85 | + expect(t('Add')).toBe('Añadir'); |
| 86 | + expect(t('Save')).toBe('Guardar'); |
| 87 | + expect(t('Cancel')).toBe('Cancelar'); |
| 88 | + expect(t('Loading…')).toBe('Cargando…'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('Portuguese translates the common chrome strings', () => { |
| 92 | + loadCatalog('pt'); |
| 93 | + expect(t('Add')).toBe('Adicionar'); |
| 94 | + expect(t('Save')).toBe('Salvar'); |
| 95 | + expect(t('Delete')).toBe('Excluir'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('French translates the common chrome strings', () => { |
| 99 | + loadCatalog('fr'); |
| 100 | + expect(t('Add')).toBe('Ajouter'); |
| 101 | + expect(t('Save')).toBe('Enregistrer'); |
| 102 | + expect(t('Delete')).toBe('Supprimer'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('English keeps the source strings unchanged (source-as-key)', () => { |
| 106 | + loadCatalog('en'); |
| 107 | + // The English catalog only has the _comment; every real string |
| 108 | + // round-trips through the source-as-key fallback. |
| 109 | + expect(t('Add')).toBe('Add'); |
| 110 | + expect(t('Anything not in the catalog')).toBe('Anything not in the catalog'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('exposes the active catalog for debugging tests', () => { |
| 114 | + loadCatalog('es'); |
| 115 | + const catalog = _activeCatalogForTests(); |
| 116 | + expect(catalog.Add).toBe('Añadir'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments