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

63 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-02-15 17:44:58 +01:00
/* eslint-disable node/global-require */
2020-11-05 14:01:23 +01:00
import { get } from 'svelte/store';
import { init, getOptions, defaultFormats } from '../../src/runtime/configs';
2020-11-05 14:01:23 +01:00
import { $locale } from '../../src/runtime/stores/locale';
const warnSpy = jest.spyOn(global.console, 'warn').mockImplementation();
beforeEach(() => {
warnSpy.mockReset();
2020-11-05 14:01:23 +01:00
});
2019-11-26 03:09:05 +01:00
2019-11-27 04:58:53 +01:00
test('inits the fallback locale', () => {
2020-11-05 14:01:23 +01:00
expect(getOptions().fallbackLocale).toBeNull();
2019-11-27 04:58:53 +01:00
init({
fallbackLocale: 'en',
2020-11-05 14:01:23 +01:00
});
expect(getOptions().fallbackLocale).toBe('en');
});
2019-11-27 04:58:53 +01:00
test('inits the initial locale by string', () => {
init({
fallbackLocale: 'pt',
initialLocale: 'en',
2020-11-05 14:01:23 +01:00
});
expect(getOptions().initialLocale).toBe('en');
expect(get($locale)).toBe('en');
});
test('adds custom formats for time, date and number values', () => {
2020-11-05 14:01:23 +01:00
const customFormats = require('../fixtures/formats.json');
2019-11-27 04:58:53 +01:00
init({
2019-11-26 03:09:05 +01:00
fallbackLocale: 'en',
formats: customFormats,
2020-11-05 14:01:23 +01:00
});
expect(getOptions().formats).toMatchObject(defaultFormats);
expect(getOptions().formats).toMatchObject(customFormats);
});
test('sets the minimum delay to set the loading store value', () => {
2020-11-05 14:01:23 +01:00
init({ fallbackLocale: 'en', loadingDelay: 300 });
expect(getOptions().loadingDelay).toBe(300);
});
test('defines default missing key handler if "warnOnMissingMessages" is "true"', () => {
init({ fallbackLocale: 'en', warnOnMissingMessages: true });
expect(typeof getOptions().handleMissingMessage).toBe('function');
});
test('warns about using deprecated "warnOnMissingMessages" alongside "handleMissingMessage"', () => {
init({
fallbackLocale: 'en',
warnOnMissingMessages: true,
handleMissingMessage() {},
});
expect(warnSpy).toHaveBeenCalledWith(
'[svelte-i18n] The "warnOnMissingMessages" option is deprecated. Please use the "handleMissingMessage" option instead.',
);
});