|
| 1 | +import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import { vol } from 'memfs' |
| 3 | +import { resolve } from 'node:path' |
| 4 | + |
| 5 | +import { runTypesGenerator } from '../src/types-generator' |
| 6 | + |
| 7 | +// Mock fs/promises to use memfs |
| 8 | +vi.mock('fs/promises', async () => { |
| 9 | + const memfs = await vi.importActual<typeof import('memfs')>('memfs') |
| 10 | + return memfs.fs.promises |
| 11 | +}) |
| 12 | + |
| 13 | +// Mock glob so we control which files are discovered |
| 14 | +vi.mock('glob', () => ({ glob: vi.fn() })) |
| 15 | + |
| 16 | +describe('types-generator with merged namespaces (single file per language)', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + vol.reset() |
| 19 | + vi.clearAllMocks() |
| 20 | + vi.spyOn(process, 'cwd').mockReturnValue('/project') |
| 21 | + |
| 22 | + const { glob } = await import('glob') as any |
| 23 | + // ensure the types.input glob resolves to our single merged file |
| 24 | + (glob as any).mockResolvedValue(['localization/translations/en.json']) |
| 25 | + |
| 26 | + // Create a merged translations file with the default namespace "translation" |
| 27 | + vol.fromJSON({ |
| 28 | + '/project/localization/translations/en.json': JSON.stringify({ |
| 29 | + translation: { |
| 30 | + addTerm: { |
| 31 | + tabTitle: 'Add clue' |
| 32 | + } |
| 33 | + } |
| 34 | + }, null, 2), |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + vi.restoreAllMocks() |
| 40 | + }) |
| 41 | + |
| 42 | + it('should treat merged-language-file contents as namespaces (use "translation" namespace) when generating types', async () => { |
| 43 | + const config: any = { |
| 44 | + locales: ['en'], |
| 45 | + extract: { |
| 46 | + mergeNamespaces: true, |
| 47 | + output: 'localization/translations/{{language}}.json', |
| 48 | + }, |
| 49 | + types: { |
| 50 | + enableSelector: true, |
| 51 | + input: ['localization/translations/en.json'], |
| 52 | + output: 'localization/types/i18next.d.ts', |
| 53 | + resourcesFile: 'localization/types/resources.d.ts', |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + await runTypesGenerator(config) |
| 58 | + |
| 59 | + const resourcesPath = resolve('/project', config.types.resourcesFile) |
| 60 | + const content = await vol.promises.readFile(resourcesPath, 'utf8') |
| 61 | + expect(content).toEqual(`// This file is automatically generated by i18next-cli. Do not edit manually. |
| 62 | +interface Resources { |
| 63 | + "translation": { |
| 64 | + "addTerm": { |
| 65 | + "tabTitle": "Add clue" |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +
|
| 70 | +export default Resources; |
| 71 | +`) |
| 72 | + }) |
| 73 | +}) |
0 commit comments