2019-11-28 20:17:22 +01:00
|
|
|
import { get } from 'svelte/store'
|
|
|
|
|
2019-11-27 02:00:23 +01:00
|
|
|
import {
|
|
|
|
hasLocaleQueue,
|
|
|
|
flushQueue,
|
|
|
|
registerLocaleLoader,
|
|
|
|
resetQueues,
|
|
|
|
} from '../../../src/client/includes/loaderQueue'
|
|
|
|
import { getMessageFromDictionary } from '../../../src/client/stores/dictionary'
|
2019-11-28 20:17:22 +01:00
|
|
|
import { $isLoading } from '../../../src/client/stores/loading'
|
|
|
|
import { getOptions } from '../../../src/client/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", () => {
|
|
|
|
expect(flushQueue('foo')).toBe(undefined)
|
|
|
|
})
|
|
|
|
|
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' }))
|
|
|
|
|
|
|
|
await flushQueue('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)
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
|
|
const flushA = flushQueue('en')
|
|
|
|
const flushB = flushQueue('en')
|
2019-11-28 20:17:22 +01:00
|
|
|
const flushC = flushQueue('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)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
const flush = flushQueue('en')
|
|
|
|
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (get($isLoading) === true) return res()
|
|
|
|
return rej('$isLoading should be "true"')
|
|
|
|
}, getOptions().loadingDelay)
|
|
|
|
}).then(() => {
|
|
|
|
flush.then(
|
|
|
|
() =>
|
|
|
|
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
|
|
|
})
|