Files
test-assignment/apps/web/src/locales.test.ts
T

25 lines
1.0 KiB
TypeScript

import { expect, it } from 'vitest';
import { en, ru } from './locales';
import { safeReturnTo } from './lib/api';
function keys(value: object, prefix = ''): string[] {
return Object.entries(value)
.flatMap(([key, child]) =>
typeof child === 'object' ? keys(child, `${prefix}${key}.`) : [`${prefix}${key}`],
)
.filter((key) => !/_(few|many)$/.test(key))
.sort();
}
it('has matching EN/RU keys and interpolation variables', () => {
expect(keys(en)).toEqual(keys(ru));
for (const key of keys(en)) {
const get = (obj: object) =>
key.split('.').reduce<unknown>((v, k) => (v as Record<string, unknown>)[k], obj) as string;
expect(get(en).match(/{{\w+}}/g)).toEqual(get(ru).match(/{{\w+}}/g));
}
});
it('only accepts internal CRM return paths', () => {
expect(safeReturnTo('/app/merchants/merchant-01?x=1')).toBe('/app/merchants/merchant-01?x=1');
for (const unsafe of ['https://example.com', '//example.com', '/application', '/app\\evil', null])
expect(safeReturnTo(unsafe)).toBe('/app');
});