Skip to content

Commit 3310c5e

Browse files
test(sonar): test i18n
1 parent d371da7 commit 3310c5e

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/core/i18n/utils.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1+
import i18n from 'i18next';
2+
import { initReactI18next } from 'react-i18next';
3+
4+
import ar from '../../translations/ar.json';
5+
import en from '../../translations/en.json';
16
import { storage } from '../storage';
2-
import { getLanguage } from './utils';
7+
import type { TxKeyPath } from './utils';
8+
import { changeLanguage, getLanguage, translate } from './utils';
39

410
jest.mock('../storage', () => ({
511
storage: {
6-
getString: jest.fn(),
12+
getString: jest.fn().mockReturnValue('en'),
713
},
814
}));
915

16+
i18n.use(initReactI18next).init({
17+
lng: 'en',
18+
fallbackLng: 'en',
19+
20+
ns: ['translationsNS'],
21+
defaultNS: 'translationsNS',
22+
23+
debug: true,
24+
25+
interpolation: {
26+
escapeValue: false,
27+
},
28+
29+
resources: { en: { translationsNS: en }, ar: { translationsNS: ar } },
30+
});
31+
1032
describe('getLanguage', () => {
1133
it('should call storage.getString with LOCAL', () => {
12-
getLanguage();
34+
const lang = getLanguage();
1335
expect(storage.getString).toHaveBeenCalledWith('local');
36+
expect(lang).toBe('en');
37+
});
38+
});
39+
40+
describe('translate', () => {
41+
it('should return translated string', () => {
42+
const key: TxKeyPath = 'onboarding.message';
43+
let options = { lang: 'en' };
44+
const translatedString = translate(key, options);
45+
expect(translatedString).toBe('Welcome to rootstrap app site');
46+
changeLanguage('ar');
47+
options = { lang: 'ar' };
48+
const translatedStringArab = translate(key, options);
49+
expect(translatedStringArab).toBe('مرحبا بكم في موقع تطبيق rootstrap');
1450
});
1551
});

0 commit comments

Comments
 (0)