From cf8d32f0a63a7efffd90eecba771c903273283a5 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Tue, 26 Nov 2019 22:25:33 -0300 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20some=20misisng=20t?= =?UTF-8?q?ests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/loaderQueue.ts | 2 +- src/client/stores/locale.ts | 1 + test/client/stores/dictionary.test.ts | 12 +++++------- test/client/stores/locale.test.ts | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/client/includes/loaderQueue.ts b/src/client/includes/loaderQueue.ts index 35271e1..ee7d752 100644 --- a/src/client/includes/loaderQueue.ts +++ b/src/client/includes/loaderQueue.ts @@ -63,8 +63,8 @@ export async function flushQueue(locale: string = getCurrentLocale()) { // 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 => { + removeLocaleFromQueue(locale) partials = partials.map(partial => partial.default || partial) addMessages(locale, ...partials) }) diff --git a/src/client/stores/locale.ts b/src/client/stores/locale.ts index 35cabf2..2983ba2 100644 --- a/src/client/stores/locale.ts +++ b/src/client/stores/locale.ts @@ -63,6 +63,7 @@ $locale.set = (newLocale: string): void | Promise => { return localeSet(newLocale) } +// istanbul ignore next $locale.update = (fn: (locale: string) => void | Promise) => localeSet(fn(current)) diff --git a/test/client/stores/dictionary.test.ts b/test/client/stores/dictionary.test.ts index 60fffb7..8541932 100644 --- a/test/client/stores/dictionary.test.ts +++ b/test/client/stores/dictionary.test.ts @@ -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', () => { addMessages('en', { field_1: 'name', deep: { prop1: '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', () => { addMessages('en', { field_1: 'name' }) expect(getLocaleDictionary('en')).toMatchObject({ field_1: 'name' }) diff --git a/test/client/stores/locale.test.ts b/test/client/stores/locale.test.ts index 8d5271f..ab9f635 100644 --- a/test/client/stores/locale.test.ts +++ b/test/client/stores/locale.test.ts @@ -1,3 +1,4 @@ +import { lookupMessage } from './../../../src/client/includes/lookup' import { get } from 'svelte/store' import { @@ -9,6 +10,8 @@ import { isRelatedLocale, } from '../../../src/client/stores/locale' import { getOptions, configure } from '../../../src/client/configs' +import { register } from '../../../src/client' +import { hasLocaleQueue } from '../../../src/client/includes/loaderQueue' beforeEach(() => { 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(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') +})