mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
bb8c68f2eb
It was only caching lookups of fallback locales. If a message was found in the passed locale, it wouldn't be cached.
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { lookup, lookupCache } from '../../../src/client/includes/lookup'
|
|
import { $dictionary, addMessages } from '../../../src/client/stores/dictionary'
|
|
|
|
beforeEach(() => {
|
|
$dictionary.set({})
|
|
})
|
|
|
|
test('returns null if no locale was passed', () => {
|
|
expect(lookup('message.id', undefined)).toBe(null)
|
|
expect(lookup('message.id', null)).toBe(null)
|
|
})
|
|
|
|
test('gets a shallow message of a locale dictionary', () => {
|
|
addMessages('en', { field: 'name' })
|
|
|
|
expect(lookup('field', 'en')).toBe('name')
|
|
})
|
|
|
|
test('gets a deep message of a locale dictionary', () => {
|
|
addMessages('en', { deep: { field: 'lastname' } })
|
|
expect(lookup('deep.field', 'en')).toBe('lastname')
|
|
})
|
|
|
|
test('gets a message from the fallback dictionary', () => {
|
|
addMessages('en', { field: 'name' })
|
|
|
|
expect(lookup('field', 'en-US')).toBe('name')
|
|
})
|
|
|
|
test('caches found messages by locale', () => {
|
|
addMessages('en', { field: 'name' })
|
|
addMessages('pt', { field: 'nome' })
|
|
lookup('field', 'en-US')
|
|
lookup('field', 'pt')
|
|
|
|
expect(lookupCache).toMatchObject({
|
|
'en-US': { field: 'name' },
|
|
pt: { field: 'nome' },
|
|
})
|
|
})
|
|
|
|
test("doesn't cache falsy messages", () => {
|
|
addMessages('en', { field: 'name' })
|
|
addMessages('pt', { field: 'nome' })
|
|
lookup('field_2', 'en-US')
|
|
lookup('field_2', 'pt')
|
|
expect(lookupCache).not.toMatchObject({
|
|
'en-US': { field_2: 'name' },
|
|
pt: { field_2: 'nome' },
|
|
})
|
|
})
|