{i18nKey}
{components?.CtrlKey}
{components?.Key}
),
}))
`
// Act
const result = transformSource(source, 'example.spec.tsx')
const diagnostics =
ts.transpileModule(result.output, {
compilerOptions: { jsx: ts.JsxEmit.ReactJSX },
fileName: 'example.spec.tsx',
reportDiagnostics: true,
}).diagnostics ?? []
// Assert
expect(
diagnostics.filter((diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error),
).toEqual([])
expect(result.output).toContain('{components?.Key}')
expect(result.output).not.toMatch(/[ \t]+$/m)
expect(transformSource(result.output, 'example.spec.tsx')).toEqual({
changes: 0,
output: result.output,
})
})
it('should adapt only translation values configured through a referenced hoisted mock', () => {
// Arrange
const source = `import { vi } from 'vitest'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const reactI18nextMock = {
useTranslation: () => mockUseTranslation(),
}
vi.mock('react-i18next', () => reactI18nextMock)
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
const businessProps = {
t: ((key: string) => key) as never,
}
`
// Act
const result = transformSource(source, 'example.spec.ts')
// Assert
expect(result.output).toBe(`import { vi } from 'vitest'
import { withSelectorKey } from '@/test/i18n-mock'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const reactI18nextMock = {
useTranslation: () => mockUseTranslation(),
}
vi.mock('react-i18next', () => reactI18nextMock)
mockUseTranslation.mockReturnValue({
t: withSelectorKey((key: string) => key),
})
const businessProps = {
t: ((key: string) => key) as never,
}
`)
expect(result.changes).toBe(2)
expect(transformSource(result.output, 'example.spec.ts')).toEqual({
changes: 0,
output: result.output,
})
})
})
})