2019-11-27 02:00:23 +01:00
|
|
|
import {
|
|
|
|
hasLocaleQueue,
|
2019-11-29 03:35:48 +01:00
|
|
|
flush,
|
2019-11-27 02:00:23 +01:00
|
|
|
registerLocaleLoader,
|
|
|
|
resetQueues,
|
2020-11-05 14:01:23 +01:00
|
|
|
} from '../../../src/runtime/includes/loaderQueue';
|
|
|
|
import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary';
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-11-05 14:01:23 +01:00
|
|
|
resetQueues();
|
|
|
|
});
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
const loader = (content: any) => () => new Promise((res) => res(content));
|
2019-11-27 02:00:23 +01:00
|
|
|
|
|
|
|
test('registers a locale loader', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(hasLocaleQueue('pt-BR')).toBe(false);
|
|
|
|
registerLocaleLoader('pt-BR', loader({ message: 'Mensagem' }));
|
|
|
|
expect(hasLocaleQueue('pt-BR')).toBe(true);
|
|
|
|
});
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
test('checks if exist queues of locale and its fallbacks', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
registerLocaleLoader('en', loader({ field: 'Name' }));
|
|
|
|
expect(hasLocaleQueue('en-US')).toBe(true);
|
|
|
|
});
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
test("does nothing if there's no queue for a locale", () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(flush('foo')).toBeUndefined();
|
|
|
|
});
|
2019-11-28 20:17:22 +01:00
|
|
|
|
2019-11-27 02:00:23 +01:00
|
|
|
test('flushes the queue of a locale and its fallbacks and merge the result with the dictionary', async () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
registerLocaleLoader('en', loader({ field: 'Name' }));
|
|
|
|
registerLocaleLoader('en-US', loader({ field_2: 'Lastname' }));
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
await flush('en-US');
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(getMessageFromDictionary('en', 'field')).toBe('Name');
|
|
|
|
expect(getMessageFromDictionary('en-US', 'field_2')).toBe('Lastname');
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(hasLocaleQueue('en')).toBe(false);
|
|
|
|
expect(hasLocaleQueue('en-US')).toBe(false);
|
|
|
|
});
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2019-11-28 20:17:22 +01:00
|
|
|
test('consecutive flushes return the same promise', async () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
registerLocaleLoader('en', async () => ({}));
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
const flushA = flush('en');
|
|
|
|
const flushB = flush('en');
|
|
|
|
const flushC = flush('en');
|
2019-11-27 02:00:23 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(flushB).toStrictEqual(flushA);
|
|
|
|
expect(flushC).toStrictEqual(flushA);
|
|
|
|
});
|
2020-05-31 17:50:17 +02:00
|
|
|
|
|
|
|
test('waits for loaders added while already flushing', async () => {
|
|
|
|
registerLocaleLoader(
|
|
|
|
'en',
|
2020-11-05 14:01:23 +01:00
|
|
|
() => new Promise((res) => setTimeout(() => res({ foo: 'foo' }), 300)),
|
|
|
|
);
|
2020-05-31 17:50:17 +02:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
const flushPromise = flush('en');
|
2020-05-31 17:50:17 +02:00
|
|
|
|
|
|
|
registerLocaleLoader(
|
|
|
|
'en',
|
2020-11-05 14:01:23 +01:00
|
|
|
() => new Promise((res) => setTimeout(() => res({ bar: 'bar' }))),
|
|
|
|
);
|
2020-05-31 17:50:17 +02:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
await flushPromise;
|
2020-05-31 17:50:17 +02:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(getMessageFromDictionary('en', 'foo')).toBe('foo');
|
|
|
|
expect(getMessageFromDictionary('en', 'bar')).toBe('bar');
|
|
|
|
});
|