Skip to content

Commit

Permalink
Merge pull request #1065 from aralroca/add-tests-flat-keys
Browse files Browse the repository at this point in the history
add tests about flat keys when keySeparator=false
  • Loading branch information
aralroca authored Jun 9, 2023
2 parents 5bed0eb + 5a65f2a commit 5a5f2d2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
17 changes: 17 additions & 0 deletions __tests__/Trans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ describe('Trans', () => {
expect(container.textContent).toContain(expected)
})

test('should work with flat keys', () => {
const i18nKey = 'this.is.a.flat.key'
const expected = 'The number is 42'
const ns = {
'this.is.a.flat.key': 'The number is 42',
}

const config = { keySeparator: false }

const { container } = render(
<I18nProvider lang="en" namespaces={{ ns }} config={config}>
<Trans i18nKey={i18nKey} ns="ns" />
</I18nProvider>
)
expect(container.textContent).toContain(expected)
})

test('should work the same way than useTranslate with default value', () => {
console.warn = jest.fn()
const i18nKey = 'ns:number'
Expand Down
44 changes: 28 additions & 16 deletions __tests__/getT.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import getT from '../src/getT'

const mockLoadLocaleFrom = jest.fn()

global.i18nConfig = {
keySeparator: false,
loadLocaleFrom: (...args) => mockLoadLocaleFrom(...args),
}

describe('getT', () => {
beforeAll(() => {
global.i18nConfig = {
loadLocaleFrom: (__lang, ns) => {
if (ns === 'ns1') {
return Promise.resolve({
key_ns1: 'message from ns1',
})
}
if (ns === 'ns2') {
return Promise.resolve({
key_ns2: 'message from ns2',
})
}
},
}
beforeEach(() => {
mockLoadLocaleFrom.mockImplementation((__lang, ns) => {
if (ns === 'ns1') {
return Promise.resolve({
key_ns1: 'message from ns1',
})
}
if (ns === 'ns2') {
return Promise.resolve({
key_ns2: 'message from ns2',
})
}
})
})

test('should load one namespace and translate + warning', async () => {
console.warn = jest.fn()
const t = await getT('en', 'ns1')
Expand All @@ -30,6 +34,14 @@ describe('getT', () => {
expect(console.warn).toBeCalledWith(expectedWarning)
})

test('should work with flat keys', async () => {
mockLoadLocaleFrom.mockImplementationOnce(async (__lang, ns) => ({
'this.is.a.flat.key': 'works',
}))
const t = await getT('en', 'common')
expect(t('this.is.a.flat.key')).toEqual('works')
})

test('should load multiple namespaces and translate', async () => {
const t = await getT('en', ['ns1', 'ns2'])
expect(typeof t).toBe('function')
Expand Down
12 changes: 12 additions & 0 deletions __tests__/transCore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ describe('transCore', () => {
).toEqual(nsRootKeys)
})

test('should allow flat keys when keySeparator=false', async () => {
const t = transCore({
config: {
keySeparator: false,
},
allNamespaces: { common: { 'example.flat.key': 'works' } },
lang: 'en',
})

expect(t('common:example.flat.key')).toEqual('works')
})

test('should return an object of nested keys', async () => {
const t = transCore({
config: {},
Expand Down
24 changes: 24 additions & 0 deletions __tests__/useTranslation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ describe('useTranslation', () => {
expect(container.textContent).toBe(expected)
})

test('should work with flat key', () => {
const Inner = () => {
const { t } = useTranslation('a')
return t`this.is.a.flat.key`
}

const ns = {
a: { 'this.is.a.flat.key': 'works' },
}

const expected = 'works'

const { container } = render(
<I18nProvider
lang="en"
namespaces={ns}
config={{ keySeparator: false }}
>
<Inner />
</I18nProvider>
)
expect(container.textContent).toBe(expected)
})

test('should work with a defined default namespace | t as function', () => {
const Inner = () => {
const { t } = useTranslation('a')
Expand Down

0 comments on commit 5a5f2d2

Please sign in to comment.