2020-01-23 14:11:04 +01:00
|
|
|
import { lookup, lookupCache } from '../../../src/runtime/includes/lookup'
|
|
|
|
import { $dictionary, addMessages } from '../../../src/runtime/stores/dictionary'
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
$dictionary.set({})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns null if no locale was passed', () => {
|
2020-01-08 15:28:44 +01:00
|
|
|
expect(lookup('message.id', undefined)).toBe(null)
|
|
|
|
expect(lookup('message.id', null)).toBe(null)
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets a shallow message of a locale dictionary', () => {
|
|
|
|
addMessages('en', { field: 'name' })
|
2020-01-08 15:28:44 +01:00
|
|
|
|
|
|
|
expect(lookup('field', 'en')).toBe('name')
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets a deep message of a locale dictionary', () => {
|
|
|
|
addMessages('en', { deep: { field: 'lastname' } })
|
2020-01-08 15:28:44 +01:00
|
|
|
expect(lookup('deep.field', 'en')).toBe('lastname')
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets a message from the fallback dictionary', () => {
|
|
|
|
addMessages('en', { field: 'name' })
|
2020-01-08 15:28:44 +01:00
|
|
|
|
|
|
|
expect(lookup('field', 'en-US')).toBe('name')
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('caches found messages by locale', () => {
|
|
|
|
addMessages('en', { field: 'name' })
|
|
|
|
addMessages('pt', { field: 'nome' })
|
2020-01-08 15:28:44 +01:00
|
|
|
lookup('field', 'en-US')
|
|
|
|
lookup('field', 'pt')
|
|
|
|
|
2019-11-27 02:00:23 +01:00
|
|
|
expect(lookupCache).toMatchObject({
|
|
|
|
'en-US': { field: 'name' },
|
2020-01-08 15:28:44 +01:00
|
|
|
pt: { field: 'nome' },
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test("doesn't cache falsy messages", () => {
|
|
|
|
addMessages('en', { field: 'name' })
|
|
|
|
addMessages('pt', { field: 'nome' })
|
2020-01-08 15:28:44 +01:00
|
|
|
lookup('field_2', 'en-US')
|
|
|
|
lookup('field_2', 'pt')
|
2019-11-27 02:00:23 +01:00
|
|
|
expect(lookupCache).not.toMatchObject({
|
|
|
|
'en-US': { field_2: 'name' },
|
2020-01-08 15:28:44 +01:00
|
|
|
pt: { field_2: 'nome' },
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|
|
|
|
})
|