mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-09-28 15:14:45 +02:00
test: 💍 add tests for invalid ICU syntax
This commit is contained in:
parent
0d623f9884
commit
9b5023edbf
@ -72,10 +72,7 @@ const formatMessage: MessageFormatter = (id, options = {}) => {
|
|||||||
try {
|
try {
|
||||||
result = getMessageFormatter(message, locale).format(values) as string;
|
result = getMessageFormatter(message, locale).format(values) as string;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn(
|
console.warn(`[svelte-i18n] Message "${id}" has syntax error:`, e.message);
|
||||||
`[svelte-i18n] Message with "${id}" has syntax error:`,
|
|
||||||
e.message,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -93,7 +90,10 @@ const formatNumber: NumberFormatter = (n, options) => {
|
|||||||
return getNumberFormatter(options).format(n);
|
return getNumberFormatter(options).format(n);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getJSON: JSONGetter = <T = any>(id: string, locale = getCurrentLocale()) => {
|
const getJSON: JSONGetter = <T = any>(
|
||||||
|
id: string,
|
||||||
|
locale = getCurrentLocale(),
|
||||||
|
) => {
|
||||||
return lookup(id, locale) as T;
|
return lookup(id, locale) as T;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
3
test/fixtures/en.json
vendored
3
test/fixtures/en.json
vendored
@ -5,5 +5,6 @@
|
|||||||
},
|
},
|
||||||
"photos": "You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}",
|
"photos": "You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}",
|
||||||
"title": "Page title",
|
"title": "Page title",
|
||||||
"sneakers": "sneakers"
|
"sneakers": "sneakers",
|
||||||
|
"with-syntax-error": "Hello {{name}}!"
|
||||||
}
|
}
|
||||||
|
@ -106,30 +106,43 @@ describe('format message', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('errors out when value found is not string', () => {
|
it('errors out when value found is not string', () => {
|
||||||
const { warn } = global.console;
|
const spy = jest.spyOn(global.console, 'warn').mockImplementation();
|
||||||
|
|
||||||
jest.spyOn(global.console, 'warn').mockImplementation();
|
|
||||||
|
|
||||||
expect(typeof formatMessage('form')).toBe('object');
|
expect(typeof formatMessage('form')).toBe('object');
|
||||||
expect(console.warn).toBeCalledWith(
|
expect(spy).toBeCalledWith(
|
||||||
`[svelte-i18n] Message with id "form" must be of type "string", found: "object". Gettin its value through the "$format" method is deprecated; use the "json" method instead.`,
|
`[svelte-i18n] Message with id "form" must be of type "string", found: "object". Gettin its value through the "$format" method is deprecated; use the "json" method instead.`,
|
||||||
);
|
);
|
||||||
|
|
||||||
global.console.warn = warn;
|
spy.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('warn on missing messages', () => {
|
it('warn on missing messages', () => {
|
||||||
const { warn } = global.console;
|
const spy = jest.spyOn(global.console, 'warn').mockImplementation();
|
||||||
|
|
||||||
jest.spyOn(global.console, 'warn').mockImplementation();
|
|
||||||
|
|
||||||
formatMessage('missing');
|
formatMessage('missing');
|
||||||
|
|
||||||
expect(console.warn).toBeCalledWith(
|
expect(spy).toBeCalledWith(
|
||||||
`[svelte-i18n] The message "missing" was not found in "en".`,
|
`[svelte-i18n] The message "missing" was not found in "en".`,
|
||||||
);
|
);
|
||||||
|
|
||||||
global.console.warn = warn;
|
spy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw with invalid syntax', () => {
|
||||||
|
$locale.set('en');
|
||||||
|
const spy = jest.spyOn(global.console, 'warn').mockImplementation();
|
||||||
|
|
||||||
|
// eslint-disable-next-line line-comment-position
|
||||||
|
formatMessage('with-syntax-error', { values: { name: 'John' } });
|
||||||
|
|
||||||
|
expect(spy).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining(
|
||||||
|
`[svelte-i18n] Message "with-syntax-error" has syntax error:`,
|
||||||
|
),
|
||||||
|
expect.anything(),
|
||||||
|
);
|
||||||
|
|
||||||
|
spy.mockRestore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user