svelte-i18n/test/runtime/index.test.ts

76 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-11-05 14:01:23 +01:00
import { defineMessages, waitLocale, register, init } from '../../src/runtime';
import { $locale } from '../../src/runtime/stores/locale';
import { hasLocaleQueue } from '../../src/runtime/includes/loaderQueue';
import {
getLocaleDictionary,
$dictionary,
2020-11-05 14:01:23 +01:00
} from '../../src/runtime/stores/dictionary';
import { $format } from '../../src/runtime/stores/formatters';
2019-11-28 19:00:04 +01:00
test('defineMessages returns the identity of its first argument', () => {
2020-11-05 14:01:23 +01:00
const obj = {};
expect(obj).toBe(defineMessages(obj));
});
describe('waiting for a locale to load', () => {
beforeEach(() => {
2020-11-05 14:01:23 +01:00
$dictionary.set({});
$locale.set(undefined);
});
2020-11-05 14:01:23 +01:00
it('should wait for a locale queue to be flushed', async () => {
register('en', () => Promise.resolve({ foo: 'foo' }));
$locale.set('en');
2020-11-05 14:01:23 +01:00
await waitLocale('en');
2020-11-05 14:01:23 +01:00
expect(hasLocaleQueue('en')).toBe(false);
expect(getLocaleDictionary('en')).toMatchObject({ foo: 'foo' });
});
2020-11-05 14:01:23 +01:00
it('should wait for the current locale queue to be flushed', async () => {
register('en', () => Promise.resolve({ foo: 'foo' }));
init({ fallbackLocale: 'pt', initialLocale: 'en' });
2020-11-05 14:01:23 +01:00
await waitLocale();
2020-11-05 14:01:23 +01:00
expect(hasLocaleQueue('en')).toBe(false);
expect(getLocaleDictionary('en')).toMatchObject({ foo: 'foo' });
});
2020-11-05 14:01:23 +01:00
it('should wait for the fallback locale queue to be flushed if initial not set', async () => {
register('pt', () => Promise.resolve({ foo: 'foo' }));
init({ fallbackLocale: 'pt' });
2020-11-05 14:01:23 +01:00
await waitLocale();
2020-11-05 14:01:23 +01:00
expect(hasLocaleQueue('pt')).toBe(false);
expect(getLocaleDictionary('pt')).toMatchObject({ foo: 'foo' });
});
});
2019-11-29 23:07:33 +01:00
describe('format updates', () => {
beforeEach(() => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'en' });
});
it('format store is updated when locale changes', () => {
const fn = jest.fn();
const cancel = $format.subscribe(fn);
$locale.set('pt');
expect(fn).toHaveBeenCalledTimes(2);
cancel();
});
it('format store is updated when dictionary changes', () => {
const fn = jest.fn();
const cancel = $format.subscribe(fn);
$dictionary.set({});
expect(fn).toHaveBeenCalledTimes(2);
cancel();
});
});