2019-11-28 20:17:22 +01:00
|
|
|
import { get } from 'svelte/store'
|
|
|
|
|
2019-11-27 02:00:23 +01:00
|
|
|
import {
|
|
|
|
hasLocaleQueue,
|
2019-11-29 03:35:48 +01:00
|
|
|
flush,
|
2019-11-27 02:00:23 +01:00
|
|
|
registerLocaleLoader,
|
|
|
|
resetQueues,
|
2020-01-23 14:11:04 +01:00
|
|
|
} from '../../../src/runtime/includes/loaderQueue'
|
|
|
|
import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary'
|
|
|
|
import { $isLoading } from '../../../src/runtime/stores/loading'
|
|
|
|
import { getOptions } from '../../../src/runtime/configs'
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
resetQueues()
|
|
|
|
})
|
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
const loader = (content: any) => () => new Promise(res => res(content))
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
test('registers a locale loader', () => {
|
|
|
|
expect(hasLocaleQueue('pt-BR')).toBe(false)
|
|
|
|
registerLocaleLoader('pt-BR', loader({ message: 'Mensagem' }))
|
|
|
|
expect(hasLocaleQueue('pt-BR')).toBe(true)
|
|
|
|
})
|
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
test('checks if exist queues of locale and its fallbacks', () => {
|
2019-11-27 02:00:23 +01:00
|
|
|
registerLocaleLoader('en', loader({ field: 'Name' }))
|
|
|
|
expect(hasLocaleQueue('en-US')).toBe(true)
|
|
|
|
})
|
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
test("does nothing if there's no queue for a locale", () => {
|
2019-11-29 03:35:48 +01:00
|
|
|
expect(flush('foo')).toBe(undefined)
|
2019-11-28 20:17:22 +01:00
|
|
|
})
|
|
|
|
|
2019-11-27 02:00:23 +01:00
|
|
|
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' }))
|
|
|
|
|
2019-11-29 03:35:48 +01:00
|
|
|
await flush('en-US')
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
test('consecutive flushes return the same promise', async () => {
|
|
|
|
registerLocaleLoader('en', async () => ({}))
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2019-11-29 03:35:48 +01:00
|
|
|
const flushA = flush('en')
|
|
|
|
const flushB = flush('en')
|
|
|
|
const flushC = flush('en')
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
expect(flushB).toStrictEqual(flushA)
|
2019-11-28 20:17:22 +01:00
|
|
|
expect(flushC).toStrictEqual(flushA)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should set loading to true if passed min delay and false after loading', () => {
|
|
|
|
registerLocaleLoader(
|
|
|
|
'en',
|
|
|
|
() =>
|
|
|
|
new Promise(res =>
|
|
|
|
setTimeout(() => res({}), getOptions().loadingDelay * 2)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-11-29 03:40:57 +01:00
|
|
|
const flushPromise = flush('en')
|
2019-11-28 20:17:22 +01:00
|
|
|
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (get($isLoading) === true) return res()
|
|
|
|
return rej('$isLoading should be "true"')
|
|
|
|
}, getOptions().loadingDelay)
|
|
|
|
}).then(() => {
|
2019-11-29 03:40:57 +01:00
|
|
|
flushPromise.then(
|
2019-11-28 20:17:22 +01:00
|
|
|
() =>
|
|
|
|
new Promise((res, rej) => {
|
|
|
|
if (get($isLoading) === false) return res()
|
|
|
|
return rej('$isLoading should be "false" after loading')
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
2019-11-27 02:00:23 +01:00
|
|
|
})
|