test: 💍 add some misisng tests

This commit is contained in:
Christian Kaisermann 2019-11-26 22:25:33 -03:00
parent b3098ba67b
commit cf8d32f0a6
4 changed files with 24 additions and 8 deletions

View File

@ -63,8 +63,8 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
// TODO what happens if some loader fails // TODO what happens if some loader fails
activeLocaleFlushes[locale] = Promise.all( activeLocaleFlushes[locale] = Promise.all(
queues.map(([locale, queue]) => { queues.map(([locale, queue]) => {
removeLocaleFromQueue(locale)
return Promise.all(queue.map(loader => loader())).then(partials => { return Promise.all(queue.map(loader => loader())).then(partials => {
removeLocaleFromQueue(locale)
partials = partials.map(partial => partial.default || partial) partials = partials.map(partial => partial.default || partial)
addMessages(locale, ...partials) addMessages(locale, ...partials)
}) })

View File

@ -63,6 +63,7 @@ $locale.set = (newLocale: string): void | Promise<void> => {
return localeSet(newLocale) return localeSet(newLocale)
} }
// istanbul ignore next
$locale.update = (fn: (locale: string) => void | Promise<void>) => $locale.update = (fn: (locale: string) => void | Promise<void>) =>
localeSet(fn(current)) localeSet(fn(current))

View File

@ -24,6 +24,11 @@ test('adds a new dictionary to a locale', () => {
}) })
}) })
test('gets the whole current dictionary', () => {
addMessages('en', { field_1: 'name' })
expect(getDictionary()).toMatchObject(get($dictionary))
})
test('merges the existing dictionaries with new ones', () => { test('merges the existing dictionaries with new ones', () => {
addMessages('en', { field_1: 'name', deep: { prop1: 'foo' } }) addMessages('en', { field_1: 'name', deep: { prop1: 'foo' } })
addMessages('en', { field_2: 'lastname', deep: { prop2: 'foo' } }) addMessages('en', { field_2: 'lastname', deep: { prop2: 'foo' } })
@ -44,13 +49,6 @@ test('merges the existing dictionaries with new ones', () => {
}) })
}) })
test('gets the whole current dictionary', () => {
addMessages('en', { field_1: 'name' })
expect(getDictionary()).toMatchObject({
en: { field_1: 'name' },
})
})
test('gets the dictionary of a locale', () => { test('gets the dictionary of a locale', () => {
addMessages('en', { field_1: 'name' }) addMessages('en', { field_1: 'name' })
expect(getLocaleDictionary('en')).toMatchObject({ field_1: 'name' }) expect(getLocaleDictionary('en')).toMatchObject({ field_1: 'name' })

View File

@ -1,3 +1,4 @@
import { lookupMessage } from './../../../src/client/includes/lookup'
import { get } from 'svelte/store' import { get } from 'svelte/store'
import { import {
@ -9,6 +10,8 @@ import {
isRelatedLocale, isRelatedLocale,
} from '../../../src/client/stores/locale' } from '../../../src/client/stores/locale'
import { getOptions, configure } from '../../../src/client/configs' import { getOptions, configure } from '../../../src/client/configs'
import { register } from '../../../src/client'
import { hasLocaleQueue } from '../../../src/client/includes/loaderQueue'
beforeEach(() => { beforeEach(() => {
configure({ fallbackLocale: undefined }) configure({ fallbackLocale: undefined })
@ -117,3 +120,17 @@ test('if no initial locale was found, set to the fallback locale', () => {
expect(get($locale)).toBe('en') expect(get($locale)).toBe('en')
expect(getOptions().fallbackLocale).toBe('en') expect(getOptions().fallbackLocale).toBe('en')
}) })
test('should flush the queue of the locale when changing the store value', async () => {
register(
'en',
() => new Promise(res => setTimeout(() => res({ foo: 'Foo' }), 50))
)
expect(hasLocaleQueue('en')).toBe(true)
await $locale.set('en')
expect(hasLocaleQueue('en')).toBe(false)
expect(lookupMessage('foo', 'en')).toBe('Foo')
})