From 60ba73682fcb7ac711ccca18f8cc20e22c6d8833 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Thu, 28 Nov 2019 16:17:22 -0300 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20more=20tests=20for?= =?UTF-8?q?=20loaderQueue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/loaderQueue.ts | 2 +- test/client/includes/loaderQueue.test.ts | 46 +++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/client/includes/loaderQueue.ts b/src/client/includes/loaderQueue.ts index ee7d752..96d1925 100644 --- a/src/client/includes/loaderQueue.ts +++ b/src/client/includes/loaderQueue.ts @@ -46,7 +46,7 @@ export function hasLocaleQueue(locale: string) { } const activeLocaleFlushes: { [key: string]: Promise } = {} -export async function flushQueue(locale: string = getCurrentLocale()) { +export function flushQueue(locale: string = getCurrentLocale()) { if (!hasLocaleQueue(locale)) return if (locale in activeLocaleFlushes) return activeLocaleFlushes[locale] diff --git a/test/client/includes/loaderQueue.test.ts b/test/client/includes/loaderQueue.test.ts index 8c7673c..a587eb2 100644 --- a/test/client/includes/loaderQueue.test.ts +++ b/test/client/includes/loaderQueue.test.ts @@ -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') + }) + ) + }) })