|
| 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'; |
1 | 6 | import { storage } from '../storage'; |
2 | | -import { getLanguage } from './utils'; |
| 7 | +import type { TxKeyPath } from './utils'; |
| 8 | +import { changeLanguage, getLanguage, translate } from './utils'; |
3 | 9 |
|
4 | 10 | jest.mock('../storage', () => ({ |
5 | 11 | storage: { |
6 | | - getString: jest.fn(), |
| 12 | + getString: jest.fn().mockReturnValue('en'), |
7 | 13 | }, |
8 | 14 | })); |
9 | 15 |
|
| 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 | + |
10 | 32 | describe('getLanguage', () => { |
11 | 33 | it('should call storage.getString with LOCAL', () => { |
12 | | - getLanguage(); |
| 34 | + const lang = getLanguage(); |
13 | 35 | 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'); |
14 | 50 | }); |
15 | 51 | }); |
0 commit comments