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

170 lines
4.8 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 {
isFallbackLocaleOf,
getFallbackOf,
getRelatedLocalesOf,
getCurrentLocale,
$locale,
isRelatedLocale,
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(() => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: undefined });
$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('checks if a locale is a fallback locale of another locale', () => {
2020-11-05 14:01:23 +01:00
expect(isFallbackLocaleOf('en', 'en-US')).toBe(true);
expect(isFallbackLocaleOf('en', 'en')).toBe(false);
expect(isFallbackLocaleOf('it', 'en-US')).toBe(false);
});
test('checks if a locale is a related locale of another locale', () => {
expect(isRelatedLocale('en', 'en-US')).toBe(true);
expect(isRelatedLocale('pt-BR', 'pt')).toBe(true);
expect(isRelatedLocale('en', 'en')).toBe(true);
expect(isRelatedLocale('en', 'it-IT')).toBe(false);
expect(isRelatedLocale('en-US', 'it')).toBe(false);
});
test('gets the next fallback locale of a locale', () => {
2020-11-05 14:01:23 +01:00
expect(getFallbackOf('az-Cyrl-AZ')).toBe('az-Cyrl');
expect(getFallbackOf('en-US')).toBe('en');
expect(getFallbackOf('en')).toBeNull();
});
test('gets the global fallback locale if set', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'en' });
expect(getFallbackOf('it')).toBe('en');
});
test('should not get the global fallback as the fallback of itself', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'en' });
expect(getFallbackOf('en')).toBeNull();
});
test('if global fallback locale has a fallback, it should return it', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'en-US' });
expect(getFallbackOf('en-US')).toBe('en');
});
test('gets all fallback locales of a locale', () => {
2020-11-05 14:01:23 +01:00
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US']);
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US']);
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
'az',
'az-Cyrl',
'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(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt']);
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt']);
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
'az',
'az-Cyrl',
'az-Cyrl-AZ',
'pt',
2020-11-05 14:01:23 +01:00
]);
});
test('gets all fallback locales of a locale including the global fallback locale and its fallbacks', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'pt-BR' });
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt', 'pt-BR']);
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt', 'pt-BR']);
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
'az',
'az-Cyrl',
'az-Cyrl-AZ',
'pt',
'pt-BR',
2020-11-05 14:01:23 +01:00
]);
});
test("don't list fallback locale twice", () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'pt-BR' });
expect(getRelatedLocalesOf('pt-BR')).toEqual(['pt', 'pt-BR']);
expect(getRelatedLocalesOf('pt')).toEqual(['pt']);
});
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',
initialLocale: null,
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);
});