From b3098ba67b5dbae6d42873fc7a082b1834be802f Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Tue, 26 Nov 2019 22:00:23 -0300 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20test=20for=20looku?= =?UTF-8?q?p=20and=20loader=20queue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/loaderQueue.ts | 21 ++++++--- src/client/includes/lookup.ts | 2 +- test/client/includes/loaderQueue.test.ts | 47 ++++++++++++++++++++ test/client/includes/lookup.test.ts | 48 +++++++++++++++++++++ test/client/{ => includes}/utils.test.ts | 2 +- test/client/{ => stores}/dictionary.test.ts | 2 +- test/client/{ => stores}/locale.test.ts | 4 +- 7 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 test/client/includes/loaderQueue.test.ts create mode 100644 test/client/includes/lookup.test.ts rename test/client/{ => includes}/utils.test.ts (98%) rename test/client/{ => stores}/dictionary.test.ts (98%) rename test/client/{ => stores}/locale.test.ts (96%) diff --git a/src/client/includes/loaderQueue.ts b/src/client/includes/loaderQueue.ts index 710aa8a..35271e1 100644 --- a/src/client/includes/loaderQueue.ts +++ b/src/client/includes/loaderQueue.ts @@ -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 const loaderQueue: Record = {} +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 } = {} 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)) { diff --git a/src/client/includes/lookup.ts b/src/client/includes/lookup.ts index 44085fb..0860ac1 100644 --- a/src/client/includes/lookup.ts +++ b/src/client/includes/lookup.ts @@ -1,7 +1,7 @@ import { getMessageFromDictionary } from '../stores/dictionary' import { getFallbackOf } from '../stores/locale' -const lookupCache: Record> = {} +export const lookupCache: Record> = {} const addToCache = (path: string, locale: string, message: string) => { if (!message) return message diff --git a/test/client/includes/loaderQueue.test.ts b/test/client/includes/loaderQueue.test.ts new file mode 100644 index 0000000..8c7673c --- /dev/null +++ b/test/client/includes/loaderQueue.test.ts @@ -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) +}) diff --git a/test/client/includes/lookup.test.ts b/test/client/includes/lookup.test.ts new file mode 100644 index 0000000..273ee79 --- /dev/null +++ b/test/client/includes/lookup.test.ts @@ -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' }, + }) +}) diff --git a/test/client/utils.test.ts b/test/client/includes/utils.test.ts similarity index 98% rename from test/client/utils.test.ts rename to test/client/includes/utils.test.ts index 43e385e..6d07581 100644 --- a/test/client/utils.test.ts +++ b/test/client/includes/utils.test.ts @@ -4,7 +4,7 @@ import { title, upper, lower, -} from '../../src/client/includes/utils' +} from '../../../src/client/includes/utils' describe('getting client locale', () => { beforeEach(() => { diff --git a/test/client/dictionary.test.ts b/test/client/stores/dictionary.test.ts similarity index 98% rename from test/client/dictionary.test.ts rename to test/client/stores/dictionary.test.ts index 750272a..60fffb7 100644 --- a/test/client/dictionary.test.ts +++ b/test/client/stores/dictionary.test.ts @@ -7,7 +7,7 @@ import { $dictionary, $locales, getLocaleDictionary, -} from '../../src/client/stores/dictionary' +} from '../../../src/client/stores/dictionary' import { get } from 'svelte/store' beforeEach(() => { diff --git a/test/client/locale.test.ts b/test/client/stores/locale.test.ts similarity index 96% rename from test/client/locale.test.ts rename to test/client/stores/locale.test.ts index 02adafa..8d5271f 100644 --- a/test/client/locale.test.ts +++ b/test/client/stores/locale.test.ts @@ -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 })