2020-11-05 14:01:23 +01:00
|
|
|
import { get } from 'svelte/store';
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
import {
|
|
|
|
$format,
|
|
|
|
$formatTime,
|
|
|
|
$formatDate,
|
|
|
|
$formatNumber,
|
2020-11-05 14:01:23 +01:00
|
|
|
} from '../../../src/runtime/stores/formatters';
|
|
|
|
import { init } from '../../../src/runtime/configs';
|
|
|
|
import { addMessages } from '../../../src/runtime/stores/dictionary';
|
|
|
|
import { $locale } from '../../../src/runtime/stores/locale';
|
2020-01-23 14:21:29 +01:00
|
|
|
import {
|
|
|
|
MessageFormatter,
|
2020-01-16 01:47:43 +01:00
|
|
|
TimeFormatter,
|
|
|
|
DateFormatter,
|
|
|
|
NumberFormatter,
|
2020-11-05 14:01:23 +01:00
|
|
|
} from '../../../src/runtime/types';
|
|
|
|
|
|
|
|
let formatMessage: MessageFormatter;
|
|
|
|
let formatTime: TimeFormatter;
|
|
|
|
let formatDate: DateFormatter;
|
|
|
|
let formatNumber: NumberFormatter;
|
2020-01-23 14:11:04 +01:00
|
|
|
|
2020-01-16 01:47:43 +01:00
|
|
|
$locale.subscribe(() => {
|
2020-11-05 14:01:23 +01:00
|
|
|
formatMessage = get($format);
|
|
|
|
formatTime = get($formatTime);
|
|
|
|
formatDate = get($formatDate);
|
|
|
|
formatNumber = get($formatNumber);
|
|
|
|
});
|
|
|
|
|
|
|
|
addMessages('en', require('../../fixtures/en.json'));
|
|
|
|
addMessages('en-GB', require('../../fixtures/en-GB.json'));
|
|
|
|
addMessages('pt', require('../../fixtures/pt.json'));
|
|
|
|
addMessages('pt-BR', require('../../fixtures/pt-BR.json'));
|
|
|
|
addMessages('pt-PT', require('../../fixtures/pt-PT.json'));
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-11-05 14:01:23 +01:00
|
|
|
init({ fallbackLocale: 'en' });
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('formats a message by its id and the current locale', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(formatMessage({ id: 'form.field_1_name' })).toBe('Name');
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('formats a message by its id and the a passed locale', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(formatMessage({ id: 'form.field_1_name', locale: 'pt' })).toBe('Nome');
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('formats a message with interpolated values', () => {
|
|
|
|
expect(formatMessage({ id: 'photos', values: { n: 0 } })).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'You have no photos.',
|
|
|
|
);
|
2020-01-16 01:47:43 +01:00
|
|
|
expect(formatMessage({ id: 'photos', values: { n: 1 } })).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'You have one photo.',
|
|
|
|
);
|
2020-01-16 01:47:43 +01:00
|
|
|
expect(formatMessage({ id: 'photos', values: { n: 21 } })).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'You have 21 photos.',
|
|
|
|
);
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('accepts a message id as first argument', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(formatMessage('form.field_1_name')).toBe('Name');
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('accepts a message id as first argument and formatting options as second', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
expect(formatMessage('form.field_1_name', { locale: 'pt' })).toBe('Nome');
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('throws if no locale is set', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
$locale.set(null);
|
2020-01-16 01:47:43 +01:00
|
|
|
expect(() => formatMessage('form.field_1_name')).toThrow(
|
2020-11-05 14:01:23 +01:00
|
|
|
'[svelte-i18n] Cannot format a message without first setting the initial locale.',
|
|
|
|
);
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('uses a missing message default value', () => {
|
|
|
|
expect(formatMessage('missing', { default: 'Missing Default' })).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'Missing Default',
|
|
|
|
);
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
test('warn on missing messages', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
const { warn } = global.console;
|
|
|
|
|
|
|
|
jest.spyOn(global.console, 'warn').mockImplementation();
|
2020-01-16 01:47:43 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
formatMessage('missing');
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
expect(console.warn).toBeCalledWith(
|
2020-11-05 14:01:23 +01:00
|
|
|
`[svelte-i18n] The message "missing" was not found in "en".`,
|
|
|
|
);
|
2020-01-16 01:47:43 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
global.console.warn = warn;
|
|
|
|
});
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
describe('format utilities', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
it('time', () => {
|
|
|
|
expect(formatTime(new Date(2019, 0, 1, 20, 37))).toBe('8:37 PM');
|
|
|
|
});
|
|
|
|
it('date', () => {
|
|
|
|
expect(formatDate(new Date(2019, 0, 1, 20, 37))).toBe('1/1/19');
|
|
|
|
});
|
|
|
|
it('number', () => {
|
|
|
|
expect(formatNumber(123123123)).toBe('123,123,123');
|
|
|
|
});
|
|
|
|
});
|