|
| 1 | +/* eslint-disable react/prop-types */ |
| 2 | +import React, { useMemo } from 'react'; |
| 3 | +import { |
| 4 | + act, fireEvent, render, waitFor, |
| 5 | +} from '@testing-library/react'; |
| 6 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 7 | +import { getCookies } from '@edx/frontend-platform/i18n/lib'; |
| 8 | +import { AppContext } from '@edx/frontend-platform/react'; |
| 9 | +import { initializeMockApp } from '@edx/frontend-platform/testing'; |
| 10 | +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; |
| 11 | + |
| 12 | +import '@testing-library/jest-dom'; |
| 13 | + |
| 14 | +import LanguageSelector from '.'; |
| 15 | + |
| 16 | +jest.mock('@edx/frontend-platform/auth', () => ({ |
| 17 | + ...jest.requireActual('@edx/frontend-platform/auth'), |
| 18 | + getAuthenticatedHttpClient: jest.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('@openedx/paragon/icons', () => ({ |
| 22 | + Language: () => <div>LanguageIcon</div>, |
| 23 | +})); |
| 24 | + |
| 25 | +const { LANGUAGE_PREFERENCE_COOKIE_NAME } = process.env; |
| 26 | + |
| 27 | +const LanguageSelectorContext = ({ authenticatedUser = null }) => { |
| 28 | + const contextValue = useMemo(() => ({ |
| 29 | + authenticatedUser, |
| 30 | + config: { |
| 31 | + LANGUAGE_PREFERENCE_COOKIE_NAME, |
| 32 | + LOGO_TRADEMARK_URL: process.env.LOGO_TRADEMARK_URL, |
| 33 | + LMS_BASE_URL: process.env.LMS_BASE_URL, |
| 34 | + SITE_SUPPORTED_LANGUAGES: ['es', 'en'], |
| 35 | + }, |
| 36 | + }), [authenticatedUser]); |
| 37 | + |
| 38 | + return ( |
| 39 | + <IntlProvider locale="en"> |
| 40 | + <AppContext.Provider |
| 41 | + value={contextValue} |
| 42 | + > |
| 43 | + <LanguageSelector |
| 44 | + options={['es', 'en']} |
| 45 | + username={authenticatedUser?.username} |
| 46 | + langCookieName={LANGUAGE_PREFERENCE_COOKIE_NAME} |
| 47 | + /> |
| 48 | + </AppContext.Provider> |
| 49 | + </IntlProvider> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +describe('LanguageSelector', () => { |
| 54 | + let initService; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + jest.clearAllMocks(); |
| 58 | + initService = initializeMockApp(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('change the language for a non authenticated user', () => { |
| 62 | + const setSpy = jest.spyOn(getCookies(), 'set'); |
| 63 | + const component = render(<LanguageSelectorContext />); |
| 64 | + |
| 65 | + expect(component.queryByRole('button')).toBeInTheDocument(); |
| 66 | + |
| 67 | + const langDropdown = component.queryByRole('button'); |
| 68 | + fireEvent.click(langDropdown); |
| 69 | + fireEvent.click(component.queryByText('Español')); |
| 70 | + |
| 71 | + expect(setSpy).toHaveBeenCalledWith(LANGUAGE_PREFERENCE_COOKIE_NAME, 'es'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('update the lang preference for an autheticathed user', async () => { |
| 75 | + const userData = { username: 'test-user' }; |
| 76 | + const processedParams = { |
| 77 | + 'pref-lang': 'es', |
| 78 | + }; |
| 79 | + getAuthenticatedHttpClient.mockReturnValue({ |
| 80 | + patch: jest.fn(), |
| 81 | + post: jest.fn(), |
| 82 | + }); |
| 83 | + const formData = new FormData(); |
| 84 | + formData.append('language', 'es'); |
| 85 | + |
| 86 | + const component = render(<LanguageSelectorContext authenticatedUser={userData} />); |
| 87 | + |
| 88 | + expect(component.queryByRole('button')).toBeInTheDocument(); |
| 89 | + |
| 90 | + const langDropdown = component.queryByRole('button'); |
| 91 | + fireEvent.click(langDropdown); |
| 92 | + fireEvent.click(component.queryByText('Español')); |
| 93 | + |
| 94 | + await waitFor(() => { |
| 95 | + expect(getAuthenticatedHttpClient().patch) |
| 96 | + .toHaveBeenCalledWith(`${process.env.LMS_BASE_URL}/api/user/v1/preferences/${userData.username}`, processedParams, { |
| 97 | + headers: { 'Content-Type': 'application/merge-patch+json' }, |
| 98 | + }); |
| 99 | + expect(getAuthenticatedHttpClient().post) |
| 100 | + .toHaveBeenCalledWith( |
| 101 | + `${process.env.LMS_BASE_URL}/i18n/setlang/`, |
| 102 | + formData, |
| 103 | + { headers: { Accept: 'application/json', 'X-Requested-With': 'XMLHttpRequest' } }, |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('call logError if it can not update the user preference', async () => { |
| 109 | + const { loggingService } = initService; |
| 110 | + const userData = { username: 'test-user' }; |
| 111 | + const component = render(<LanguageSelectorContext authenticatedUser={userData} />); |
| 112 | + getAuthenticatedHttpClient.mockReturnValue({ |
| 113 | + patch: jest.fn().mockRejectedValue(new Error('Error')), |
| 114 | + }); |
| 115 | + expect(component.queryByRole('button')).toBeInTheDocument(); |
| 116 | + |
| 117 | + const langDropdown = component.queryByRole('button'); |
| 118 | + fireEvent.click(langDropdown); |
| 119 | + fireEvent.click(component.queryByText('Español')); |
| 120 | + |
| 121 | + await waitFor(() => { |
| 122 | + expect(loggingService.logError).toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should disp,ay the language icon and modify the label according to the screen size', () => { |
| 127 | + const component = render(<LanguageSelectorContext />); |
| 128 | + expect(component.queryByRole('button').textContent).toBe('LanguageIconEnglish'); |
| 129 | + |
| 130 | + act(() => { |
| 131 | + global.innerWidth = 700; |
| 132 | + global.dispatchEvent(new Event('resize')); |
| 133 | + }); |
| 134 | + expect(component.queryByRole('button').textContent).toBe('LanguageIconEN'); |
| 135 | + act(() => { |
| 136 | + global.innerWidth = 500; |
| 137 | + global.dispatchEvent(new Event('resize')); |
| 138 | + }); |
| 139 | + expect(component.queryByRole('button').textContent).toBe('LanguageIcon'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments