mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 09:59:58 +01:00
test: 💍 add test for lookup and loader queue
This commit is contained in:
parent
21afa3d6b1
commit
b3098ba67b
@ -6,11 +6,17 @@ import {
|
||||
} from '../stores/dictionary'
|
||||
import { getCurrentLocale, getRelatedLocalesOf } from '../stores/locale'
|
||||
import { $isLoading } from '../stores/loading'
|
||||
import { getLoadingDelay } from '../configs'
|
||||
import { getOptions } from '../configs'
|
||||
|
||||
type Queue = Set<MessagesLoader>
|
||||
const loaderQueue: Record<string, Queue> = {}
|
||||
|
||||
export function resetQueues() {
|
||||
Object.keys(loaderQueue).forEach(key => {
|
||||
delete loaderQueue[key]
|
||||
})
|
||||
}
|
||||
|
||||
function createLocaleQueue(locale: string) {
|
||||
loaderQueue[locale] = new Set()
|
||||
}
|
||||
@ -39,10 +45,6 @@ export function hasLocaleQueue(locale: string) {
|
||||
.some(getLocaleQueue)
|
||||
}
|
||||
|
||||
export function addLoaderToQueue(locale: string, loader: MessagesLoader) {
|
||||
loaderQueue[locale].add(loader)
|
||||
}
|
||||
|
||||
const activeLocaleFlushes: { [key: string]: Promise<void> } = {}
|
||||
export async function flushQueue(locale: string = getCurrentLocale()) {
|
||||
if (!hasLocaleQueue(locale)) return
|
||||
@ -50,14 +52,18 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
|
||||
|
||||
// get queue of XX-YY and XX locales
|
||||
const queues = getLocalesQueues(locale)
|
||||
// istanbul ignore if
|
||||
if (queues.length === 0) return
|
||||
|
||||
removeLocaleFromQueue(locale)
|
||||
const loadingDelay = setTimeout(() => $isLoading.set(true), getLoadingDelay())
|
||||
const loadingDelay = setTimeout(
|
||||
() => $isLoading.set(true),
|
||||
getOptions().loadingDelay
|
||||
)
|
||||
|
||||
// TODO what happens if some loader fails
|
||||
activeLocaleFlushes[locale] = Promise.all(
|
||||
queues.map(([locale, queue]) => {
|
||||
removeLocaleFromQueue(locale)
|
||||
return Promise.all(queue.map(loader => loader())).then(partials => {
|
||||
partials = partials.map(partial => partial.default || partial)
|
||||
addMessages(locale, ...partials)
|
||||
@ -76,6 +82,7 @@ export function registerLocaleLoader(locale: string, loader: MessagesLoader) {
|
||||
if (!getLocaleQueue(locale)) createLocaleQueue(locale)
|
||||
|
||||
const queue = getLocaleQueue(locale)
|
||||
// istanbul ignore if
|
||||
if (getLocaleQueue(locale).has(loader)) return
|
||||
|
||||
if (!hasLocaleDictionary(locale)) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { getMessageFromDictionary } from '../stores/dictionary'
|
||||
import { getFallbackOf } from '../stores/locale'
|
||||
|
||||
const lookupCache: Record<string, Record<string, string>> = {}
|
||||
export const lookupCache: Record<string, Record<string, string>> = {}
|
||||
|
||||
const addToCache = (path: string, locale: string, message: string) => {
|
||||
if (!message) return message
|
||||
|
47
test/client/includes/loaderQueue.test.ts
Normal file
47
test/client/includes/loaderQueue.test.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import {
|
||||
hasLocaleQueue,
|
||||
flushQueue,
|
||||
registerLocaleLoader,
|
||||
resetQueues,
|
||||
} from '../../../src/client/includes/loaderQueue'
|
||||
import { getMessageFromDictionary } from '../../../src/client/stores/dictionary'
|
||||
|
||||
beforeEach(() => {
|
||||
resetQueues()
|
||||
})
|
||||
|
||||
const loader = (content: any) => () => new Promise((res, rej) => 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 queues of locale and its fallbacks', () => {
|
||||
registerLocaleLoader('en', loader({ field: 'Name' }))
|
||||
expect(hasLocaleQueue('en-US')).toBe(true)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
test('consecutive flushes return the same promise', () => {
|
||||
// const promiseA = () => new Promise((res, rej) => setTimeout(res, 50))
|
||||
registerLocaleLoader('en', async () => {})
|
||||
|
||||
const flushA = flushQueue('en')
|
||||
const flushB = flushQueue('en')
|
||||
|
||||
expect(flushB).toStrictEqual(flushA)
|
||||
})
|
48
test/client/includes/lookup.test.ts
Normal file
48
test/client/includes/lookup.test.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { lookupMessage, lookupCache } from '../../../src/client/includes/lookup'
|
||||
import { $dictionary, addMessages } from '../../../src/client/stores/dictionary'
|
||||
|
||||
beforeEach(() => {
|
||||
$dictionary.set({})
|
||||
})
|
||||
|
||||
test('returns null if no locale was passed', () => {
|
||||
expect(lookupMessage('message.id', undefined)).toBe(null)
|
||||
expect(lookupMessage('message.id', null)).toBe(null)
|
||||
})
|
||||
|
||||
test('gets a shallow message of a locale dictionary', () => {
|
||||
addMessages('en', { field: 'name' })
|
||||
expect(lookupMessage('field', 'en')).toBe('name')
|
||||
})
|
||||
|
||||
test('gets a deep message of a locale dictionary', () => {
|
||||
addMessages('en', { deep: { field: 'lastname' } })
|
||||
expect(lookupMessage('deep.field', 'en')).toBe('lastname')
|
||||
})
|
||||
|
||||
test('gets a message from the fallback dictionary', () => {
|
||||
addMessages('en', { field: 'name' })
|
||||
expect(lookupMessage('field', 'en-US')).toBe('name')
|
||||
})
|
||||
|
||||
test('caches found messages by locale', () => {
|
||||
addMessages('en', { field: 'name' })
|
||||
addMessages('pt', { field: 'nome' })
|
||||
lookupMessage('field', 'en-US')
|
||||
lookupMessage('field', 'pt-BR')
|
||||
expect(lookupCache).toMatchObject({
|
||||
'en-US': { field: 'name' },
|
||||
'pt-BR': { field: 'nome' },
|
||||
})
|
||||
})
|
||||
|
||||
test("doesn't cache falsy messages", () => {
|
||||
addMessages('en', { field: 'name' })
|
||||
addMessages('pt', { field: 'nome' })
|
||||
lookupMessage('field_2', 'en-US')
|
||||
lookupMessage('field_2', 'pt-BR')
|
||||
expect(lookupCache).not.toMatchObject({
|
||||
'en-US': { field_2: 'name' },
|
||||
'pt-BR': { field_2: 'nome' },
|
||||
})
|
||||
})
|
@ -4,7 +4,7 @@ import {
|
||||
title,
|
||||
upper,
|
||||
lower,
|
||||
} from '../../src/client/includes/utils'
|
||||
} from '../../../src/client/includes/utils'
|
||||
|
||||
describe('getting client locale', () => {
|
||||
beforeEach(() => {
|
@ -7,7 +7,7 @@ import {
|
||||
$dictionary,
|
||||
$locales,
|
||||
getLocaleDictionary,
|
||||
} from '../../src/client/stores/dictionary'
|
||||
} from '../../../src/client/stores/dictionary'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
beforeEach(() => {
|
@ -7,8 +7,8 @@ import {
|
||||
getCurrentLocale,
|
||||
$locale,
|
||||
isRelatedLocale,
|
||||
} from '../../src/client/stores/locale'
|
||||
import { getOptions, configure } from '../../src/client/configs'
|
||||
} from '../../../src/client/stores/locale'
|
||||
import { getOptions, configure } from '../../../src/client/configs'
|
||||
|
||||
beforeEach(() => {
|
||||
configure({ fallbackLocale: undefined })
|
Loading…
Reference in New Issue
Block a user