svelte-i18n/test/runtime/includes/loaderQueue.test.ts
Christian Kaisermann 9d82a98e8d fix: 🐛 ignore loadingDelay for the initialLocale
Fixes #53

 Closes: Closes #53
2020-03-06 14:11:51 -03:00

53 lines
1.5 KiB
TypeScript

import {
hasLocaleQueue,
flush,
registerLocaleLoader,
resetQueues,
} from '../../../src/runtime/includes/loaderQueue'
import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary'
beforeEach(() => {
resetQueues()
})
const loader = (content: any) => () => new Promise(res => res(content))
test('registers a locale loader', () => {
expect(hasLocaleQueue('pt-BR')).toBe(false)
registerLocaleLoader('pt-BR', loader({ message: 'Mensagem' }))
expect(hasLocaleQueue('pt-BR')).toBe(true)
})
test('checks if exist queues of locale and its fallbacks', () => {
registerLocaleLoader('en', loader({ field: 'Name' }))
expect(hasLocaleQueue('en-US')).toBe(true)
})
test("does nothing if there's no queue for a locale", () => {
expect(flush('foo')).toBe(undefined)
})
test('flushes the queue of a locale and its fallbacks and merge the result with the dictionary', async () => {
registerLocaleLoader('en', loader({ field: 'Name' }))
registerLocaleLoader('en-US', loader({ field_2: 'Lastname' }))
await flush('en-US')
expect(getMessageFromDictionary('en', 'field')).toBe('Name')
expect(getMessageFromDictionary('en-US', 'field_2')).toBe('Lastname')
expect(hasLocaleQueue('en')).toBe(false)
expect(hasLocaleQueue('en-US')).toBe(false)
})
test('consecutive flushes return the same promise', async () => {
registerLocaleLoader('en', async () => ({}))
const flushA = flush('en')
const flushB = flush('en')
const flushC = flush('en')
expect(flushB).toStrictEqual(flushA)
expect(flushC).toStrictEqual(flushA)
})