Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jarda-svoboda committed Jul 12, 2023
1 parent 3bf0a0f commit c0f166f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions tests/data/translations/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"modifier_number": "{{value:number;}}",
"modifier_date": "{{value:date;}}",
"modifier_ago": "{{value:ago;}}",
"modifier_currency": "{{value:currency;}}",
"modifier_custom": "{{data:test;}}",
"modifier_escaped": "{{va\\:lue; option\\:1:VA\\;\\{\\{LUE\\}\\}\\:1; option\\:2:VA\\;\\{\\{LUE\\}\\}\\:2; default:DEFAULT \\{\\{VALUE\\}\\}\\;}}",
"preprocess": [
Expand Down
3 changes: 2 additions & 1 deletion tests/data/translations/ku/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"custom_modifier": "{{date:date;}}",
"modifier_number": "{{value:number;}}",
"modifier_date": "{{value:date;}}",
"modifier_ago": "{{value:ago;}}"
"modifier_ago": "{{value:ago;}}",
"modifier_currency": "{{value:currency;}}"
}
3 changes: 2 additions & 1 deletion tests/data/translations/zh-Hans/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"custom_modifier": "{{date:date;}}",
"modifier_number": "{{value:number;}}",
"modifier_date": "{{value:date;}}",
"modifier_ago": "{{value:ago;}}"
"modifier_ago": "{{value:ago;}}",
"modifier_currency": "{{value:currency;}}"
}
25 changes: 24 additions & 1 deletion tests/specs/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CONFIG } from '../data';

const { initLocale = '' } = CONFIG;

describe('Default parser', () => {
describe('parser', () => {
it('returns a key string if not defined', async () => {
const { t, loadConfig } = new i18n<Parser.Params<{ value?: any }>>();

Expand Down Expand Up @@ -216,6 +216,29 @@ describe('Default parser', () => {
await loadConfig({ ...CONFIG, parserOptions: { modifierDefaults: { ago: { format: 'week' } } } });
expect(t.get('common.modifier_ago', { value })).not.toBe(new Intl.RelativeTimeFormat(initLocale).format(-7, 'day'));
});
it('`currency` modifier works', async () => {
const { t, loadConfig } = new i18n<Parser.Params<{ value?: number }>>();

await loadConfig(CONFIG);
const value = 10;
const ratio = 21.4;

expect(t.get('common.modifier_currency', { value }, { currency: { currency: 'USD', ratio: 1 } })).toBe(new Intl.NumberFormat(initLocale, { style: 'currency', currency: 'USD' }).format(value));

expect(t.get('common.modifier_currency', { value }, { currency: { currency: 'CZK', ratio } })).toBe(new Intl.NumberFormat(initLocale, { style: 'currency', currency: 'CZK' }).format(value * ratio));
});
it('`currency` defaults work', async () => {
const { t, loadConfig } = new i18n<Parser.Params<{ value?: number }>>();

const value = 10;
const ratio = 21.4;

await loadConfig({ ...CONFIG, parserOptions: { modifierDefaults: { currency: { currency: 'USD', ratio: 1 } } } });
expect(t.get('common.modifier_currency', { value })).toBe(new Intl.NumberFormat(initLocale, { style: 'currency', currency: 'USD' }).format(value));

await loadConfig({ ...CONFIG, parserOptions: { modifierDefaults: { currency: { currency: 'CZK', ratio } } } });
expect(t.get('common.modifier_currency', { value })).toBe(new Intl.NumberFormat(initLocale, { style: 'currency', currency: 'CZK' }).format(value * ratio));
});
it('custom modifier works', async () => {
const { t, loadConfig } = new i18n<Parser.Params<{ data?: any }>>();

Expand Down

0 comments on commit c0f166f

Please sign in to comment.