mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 09:59:58 +01:00
test: 💍 add more tests for loaderQueue
This commit is contained in:
parent
7dee09b668
commit
60ba73682f
@ -46,7 +46,7 @@ export function hasLocaleQueue(locale: string) {
|
||||
}
|
||||
|
||||
const activeLocaleFlushes: { [key: string]: Promise<void> } = {}
|
||||
export async function flushQueue(locale: string = getCurrentLocale()) {
|
||||
export function flushQueue(locale: string = getCurrentLocale()) {
|
||||
if (!hasLocaleQueue(locale)) return
|
||||
if (locale in activeLocaleFlushes) return activeLocaleFlushes[locale]
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
import {
|
||||
hasLocaleQueue,
|
||||
flushQueue,
|
||||
@ -5,12 +7,14 @@ import {
|
||||
resetQueues,
|
||||
} from '../../../src/client/includes/loaderQueue'
|
||||
import { getMessageFromDictionary } from '../../../src/client/stores/dictionary'
|
||||
import { $isLoading } from '../../../src/client/stores/loading'
|
||||
import { getOptions } from '../../../src/client/configs'
|
||||
|
||||
beforeEach(() => {
|
||||
resetQueues()
|
||||
})
|
||||
|
||||
const loader = (content: any) => () => new Promise((res, rej) => res(content))
|
||||
const loader = (content: any) => () => new Promise(res => res(content))
|
||||
|
||||
test('registers a locale loader', () => {
|
||||
expect(hasLocaleQueue('pt-BR')).toBe(false)
|
||||
@ -18,11 +22,15 @@ test('registers a locale loader', () => {
|
||||
expect(hasLocaleQueue('pt-BR')).toBe(true)
|
||||
})
|
||||
|
||||
test('checks queues of locale and its fallbacks', () => {
|
||||
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(flushQueue('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' }))
|
||||
@ -36,12 +44,40 @@ test('flushes the queue of a locale and its fallbacks and merge the result with
|
||||
expect(hasLocaleQueue('en-US')).toBe(false)
|
||||
})
|
||||
|
||||
test('consecutive flushes return the same promise', () => {
|
||||
// const promiseA = () => new Promise((res, rej) => setTimeout(res, 50))
|
||||
registerLocaleLoader('en', async () => {})
|
||||
test('consecutive flushes return the same promise', async () => {
|
||||
registerLocaleLoader('en', async () => ({}))
|
||||
|
||||
const flushA = flushQueue('en')
|
||||
const flushB = flushQueue('en')
|
||||
const flushC = flushQueue('en')
|
||||
|
||||
expect(flushB).toStrictEqual(flushA)
|
||||
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')
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user