|
| 1 | +import { |
| 2 | + getMessage, |
| 3 | + fetchLocale, |
| 4 | + FALLBACK_LOCALE, |
| 5 | +} from '../../shared/modules/i18n'; |
| 6 | +import { t, updateCurrentLocale } from './translate'; |
| 7 | + |
| 8 | +const localeCodeMock = 'te'; |
| 9 | +const keyMock = 'testKey'; |
| 10 | +const substitutionsMock = ['a1', 'b2']; |
| 11 | +const messageMock = 'testMessage'; |
| 12 | +const messageMock2 = 'testMessage2'; |
| 13 | +const alternateLocaleDataMock = { [keyMock]: { message: messageMock2 } }; |
| 14 | + |
| 15 | +jest.mock('../../shared/modules/i18n'); |
| 16 | +jest.mock('../_locales/en/messages.json', () => ({ |
| 17 | + [keyMock]: { message: messageMock }, |
| 18 | +})); |
| 19 | + |
| 20 | +describe('Translate', () => { |
| 21 | + const getMessageMock = getMessage as jest.MockedFunction<typeof getMessage>; |
| 22 | + const fetchLocaleMock = fetchLocale as jest.MockedFunction< |
| 23 | + typeof fetchLocale |
| 24 | + >; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + jest.resetAllMocks(); |
| 28 | + await updateCurrentLocale(FALLBACK_LOCALE); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('updateCurrentLocale', () => { |
| 32 | + it('retrieves locale data from shared module', async () => { |
| 33 | + await updateCurrentLocale(localeCodeMock); |
| 34 | + |
| 35 | + expect(fetchLocale).toHaveBeenCalledTimes(1); |
| 36 | + expect(fetchLocale).toHaveBeenCalledWith(localeCodeMock); |
| 37 | + }); |
| 38 | + |
| 39 | + it('does not retrieve locale data if same locale already set', async () => { |
| 40 | + await updateCurrentLocale(localeCodeMock); |
| 41 | + await updateCurrentLocale(localeCodeMock); |
| 42 | + |
| 43 | + expect(fetchLocale).toHaveBeenCalledTimes(1); |
| 44 | + expect(fetchLocale).toHaveBeenCalledWith(localeCodeMock); |
| 45 | + }); |
| 46 | + |
| 47 | + it('does not retrieve locale data if fallback locale set', async () => { |
| 48 | + await updateCurrentLocale(localeCodeMock); |
| 49 | + await updateCurrentLocale(FALLBACK_LOCALE); |
| 50 | + |
| 51 | + expect(fetchLocale).toHaveBeenCalledTimes(1); |
| 52 | + expect(fetchLocale).toHaveBeenCalledWith(localeCodeMock); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('t', () => { |
| 57 | + it('returns value from shared module', () => { |
| 58 | + getMessageMock.mockReturnValue(messageMock); |
| 59 | + |
| 60 | + expect(t(keyMock, ...substitutionsMock)).toStrictEqual(messageMock); |
| 61 | + }); |
| 62 | + |
| 63 | + it('uses en locale by default', () => { |
| 64 | + getMessageMock.mockReturnValue(messageMock); |
| 65 | + |
| 66 | + t(keyMock, ...substitutionsMock); |
| 67 | + |
| 68 | + expect(getMessage).toHaveBeenCalledTimes(1); |
| 69 | + expect(getMessage).toHaveBeenCalledWith( |
| 70 | + FALLBACK_LOCALE, |
| 71 | + { [keyMock]: { message: messageMock } }, |
| 72 | + keyMock, |
| 73 | + substitutionsMock, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('uses locale passed to updateCurrentLocale if called', async () => { |
| 78 | + (getMessage as jest.MockedFunction<typeof getMessage>).mockReturnValue( |
| 79 | + messageMock, |
| 80 | + ); |
| 81 | + |
| 82 | + fetchLocaleMock.mockResolvedValueOnce(alternateLocaleDataMock); |
| 83 | + await updateCurrentLocale(localeCodeMock); |
| 84 | + |
| 85 | + t(keyMock, ...substitutionsMock); |
| 86 | + |
| 87 | + expect(getMessage).toHaveBeenCalledTimes(1); |
| 88 | + expect(getMessage).toHaveBeenCalledWith( |
| 89 | + localeCodeMock, |
| 90 | + alternateLocaleDataMock, |
| 91 | + keyMock, |
| 92 | + substitutionsMock, |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns value from en locale as fallback if current locale returns null', async () => { |
| 97 | + ( |
| 98 | + getMessage as jest.MockedFunction<typeof getMessage> |
| 99 | + ).mockReturnValueOnce(null); |
| 100 | + |
| 101 | + ( |
| 102 | + getMessage as jest.MockedFunction<typeof getMessage> |
| 103 | + ).mockReturnValueOnce(messageMock2); |
| 104 | + |
| 105 | + fetchLocaleMock.mockResolvedValueOnce(alternateLocaleDataMock); |
| 106 | + await updateCurrentLocale(localeCodeMock); |
| 107 | + |
| 108 | + expect(t(keyMock, ...substitutionsMock)).toStrictEqual(messageMock2); |
| 109 | + |
| 110 | + expect(getMessage).toHaveBeenCalledTimes(2); |
| 111 | + expect(getMessage).toHaveBeenCalledWith( |
| 112 | + FALLBACK_LOCALE, |
| 113 | + { [keyMock]: { message: messageMock } }, |
| 114 | + keyMock, |
| 115 | + substitutionsMock, |
| 116 | + ); |
| 117 | + expect(getMessage).toHaveBeenCalledWith( |
| 118 | + localeCodeMock, |
| 119 | + alternateLocaleDataMock, |
| 120 | + keyMock, |
| 121 | + substitutionsMock, |
| 122 | + ); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments