svelte-i18n/test/runtime/stores/locale.test.ts

147 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-11-05 14:01:23 +01:00
import { get } from 'svelte/store';
2020-11-05 14:01:23 +01:00
import { lookup } from '../../../src/runtime/includes/lookup';
import {
getPossibleLocales,
getCurrentLocale,
$locale,
2020-11-05 14:01:23 +01:00
} from '../../../src/runtime/stores/locale';
import { getOptions, init } from '../../../src/runtime/configs';
import { register, isLoading } from '../../../src/runtime';
import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue';
beforeEach(() => {
2021-08-24 04:59:40 +02:00
init({ fallbackLocale: undefined as any });
2020-11-05 14:01:23 +01:00
$locale.set(undefined);
});
test('sets and gets the fallback locale', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'en' });
expect(getOptions().fallbackLocale).toBe('en');
});
test('gets all possible locales from a reference locale', () => {
expect(getPossibleLocales('en-US')).toEqual(['en-US', 'en']);
expect(getPossibleLocales('az-Cyrl-AZ')).toEqual([
'az-Cyrl-AZ',
'az-Cyrl',
'az',
2020-11-05 14:01:23 +01:00
]);
});
test('gets all fallback locales of a locale including the global fallback locale', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'pt' });
expect(getPossibleLocales('en-US')).toEqual(['en-US', 'en', 'pt']);
expect(getPossibleLocales('az-Cyrl-AZ')).toEqual([
'az-Cyrl-AZ',
'az-Cyrl',
'az',
'pt',
2020-11-05 14:01:23 +01:00
]);
});
test('remove duplicate fallback locales', () => {
expect(getPossibleLocales('en-AU', 'en-GB')).toEqual([
'en-AU',
'en',
'en-GB',
]);
});
test('gets all fallback locales of a locale including the global fallback locale and its fallbacks', () => {
expect(getPossibleLocales('en-US', 'pt-BR')).toEqual([
'en-US',
'en',
'pt-BR',
'pt',
]);
expect(getPossibleLocales('en-US', 'pt-BR')).toEqual([
'en-US',
'en',
'pt-BR',
'pt',
]);
expect(getPossibleLocales('az-Cyrl-AZ', 'pt-BR')).toEqual([
'az-Cyrl-AZ',
'az-Cyrl',
'az',
'pt-BR',
'pt',
2020-11-05 14:01:23 +01:00
]);
});
test("don't list fallback locale twice", () => {
expect(getPossibleLocales('pt-BR', 'pt-BR')).toEqual(['pt-BR', 'pt']);
expect(getPossibleLocales('pt', 'pt-BR')).toEqual(['pt', 'pt-BR']);
2020-11-05 14:01:23 +01:00
});
test('gets the current locale', () => {
2020-11-05 14:01:23 +01:00
expect(getCurrentLocale()).toBeUndefined();
$locale.set('es-ES');
expect(getCurrentLocale()).toBe('es-ES');
});
test('if no initial locale is set, set the locale to the fallback', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'pt' });
expect(get($locale)).toBe('pt');
expect(getOptions().fallbackLocale).toBe('pt');
});
test('if no initial locale was found, set to the fallback locale', () => {
2019-11-27 04:58:53 +01:00
init({
fallbackLocale: 'en',
2021-08-24 04:59:40 +02:00
initialLocale: null as any,
2020-11-05 14:01:23 +01:00
});
expect(get($locale)).toBe('en');
expect(getOptions().fallbackLocale).toBe('en');
});
2019-11-27 02:25:33 +01:00
test('should flush the queue of the locale when changing the store value', async () => {
register(
'en',
2020-11-05 14:01:23 +01:00
() => new Promise((res) => setTimeout(() => res({ foo: 'Foo' }), 50)),
);
2019-11-27 02:25:33 +01:00
2020-11-05 14:01:23 +01:00
expect(hasLocaleQueue('en')).toBe(true);
2019-11-27 02:25:33 +01:00
2020-11-05 14:01:23 +01:00
await $locale.set('en');
2019-11-27 02:25:33 +01:00
2020-11-05 14:01:23 +01:00
expect(hasLocaleQueue('en')).toBe(false);
expect(lookup('foo', 'en')).toBe('Foo');
});
test('if no locale is set, ignore the loading delay', async () => {
register(
'en',
2020-11-05 14:01:23 +01:00
() => new Promise((res) => setTimeout(() => res({ foo: 'Foo' }), 50)),
);
2020-11-05 14:01:23 +01:00
const promise = $locale.set('en');
2020-11-05 14:01:23 +01:00
expect(get(isLoading)).toBe(true);
2020-11-05 14:01:23 +01:00
await promise;
2020-11-05 14:01:23 +01:00
expect(get(isLoading)).toBe(false);
});
test("if a locale is set, don't ignore the loading delay", async () => {
register(
'en',
2020-11-05 14:01:23 +01:00
() => new Promise((res) => setTimeout(() => res({ foo: 'Foo' }), 50)),
);
register(
'pt',
2020-11-05 14:01:23 +01:00
() => new Promise((res) => setTimeout(() => res({ foo: 'Foo' }), 50)),
);
2020-11-05 14:01:23 +01:00
await $locale.set('en');
const promise = $locale.set('pt');
2020-11-05 14:01:23 +01:00
expect(get(isLoading)).toBe(false);
2020-11-05 14:01:23 +01:00
await promise;
2020-11-05 14:01:23 +01:00
expect(get(isLoading)).toBe(false);
});