-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate the test for lang seector
- Loading branch information
Showing
3 changed files
with
116 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* eslint-disable react/prop-types */ | ||
import React, { useMemo } from 'react'; | ||
import { fireEvent, render, waitFor } from '@testing-library/react'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
import { getCookies } from '@edx/frontend-platform/i18n/lib'; | ||
import { AppContext } from '@edx/frontend-platform/react'; | ||
import { initializeMockApp } from '@edx/frontend-platform/testing'; | ||
|
||
import '@testing-library/jest-dom'; | ||
|
||
import { patchPreferences, postSetLang } from './data'; | ||
import LanguageSelector from '.'; | ||
|
||
jest.mock('@openedx/paragon/icons', () => ({ | ||
Language: () => <div>LanguageIcon</div>, | ||
})); | ||
jest.mock('./data', () => ({ | ||
patchPreferences: jest.fn(), | ||
postSetLang: jest.fn(), | ||
})); | ||
|
||
const { LANGUAGE_PREFERENCE_COOKIE_NAME } = process.env; | ||
|
||
const LanguageSelectorContext = ({ authenticatedUser = null }) => { | ||
const contextValue = useMemo(() => ({ | ||
authenticatedUser, | ||
config: { | ||
LANGUAGE_PREFERENCE_COOKIE_NAME, | ||
LOGO_TRADEMARK_URL: process.env.LOGO_TRADEMARK_URL, | ||
LMS_BASE_URL: process.env.LMS_BASE_URL, | ||
SITE_SUPPORTED_LANGUAGES: ['es', 'en'], | ||
}, | ||
}), [authenticatedUser]); | ||
|
||
return ( | ||
<IntlProvider locale="en"> | ||
<AppContext.Provider | ||
value={contextValue} | ||
> | ||
<LanguageSelector | ||
options={['es', 'en']} | ||
username={authenticatedUser?.username} | ||
langCookieName={LANGUAGE_PREFERENCE_COOKIE_NAME} | ||
/> | ||
</AppContext.Provider> | ||
</IntlProvider> | ||
); | ||
}; | ||
|
||
describe('LanguageSelector', () => { | ||
let initService; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
initService = initializeMockApp(); | ||
}); | ||
|
||
it('change the language for a non authenticated user', () => { | ||
const setSpy = jest.spyOn(getCookies(), 'set'); | ||
const component = render(<LanguageSelectorContext />); | ||
|
||
expect(component.queryByRole('button')).toBeInTheDocument(); | ||
|
||
const langDropdown = component.queryByRole('button'); | ||
fireEvent.click(langDropdown); | ||
fireEvent.click(component.queryByText('Español')); | ||
|
||
expect(setSpy).toHaveBeenCalledWith(LANGUAGE_PREFERENCE_COOKIE_NAME, 'es'); | ||
}); | ||
it('update the lang preference for an autheticathed user', async () => { | ||
const userData = { username: 'test-user' }; | ||
const component = render(<LanguageSelectorContext authenticatedUser={userData} />); | ||
|
||
expect(component.queryByRole('button')).toBeInTheDocument(); | ||
|
||
const langDropdown = component.queryByRole('button'); | ||
fireEvent.click(langDropdown); | ||
fireEvent.click(component.queryByText('Español')); | ||
|
||
await waitFor(() => { | ||
expect(patchPreferences).toHaveBeenCalledWith(userData.username, { prefLang: 'es' }); | ||
expect(postSetLang).toHaveBeenCalledWith('es'); | ||
}); | ||
}); | ||
|
||
it('call logError if it can not update the user preference', async () => { | ||
const userData = { username: 'test-user' }; | ||
const component = render(<LanguageSelectorContext authenticatedUser={userData} />); | ||
patchPreferences.mockRejectedValueOnce(new Error('error')); | ||
|
||
expect(component.queryByRole('button')).toBeInTheDocument(); | ||
|
||
const langDropdown = component.queryByRole('button'); | ||
const { loggingService } = initService; | ||
fireEvent.click(langDropdown); | ||
fireEvent.click(component.queryByText('Español')); | ||
|
||
await waitFor(() => { | ||
expect(loggingService.logError).toHaveBeenCalled(); | ||
}); | ||
}); | ||
it('should disp,ay the language icon and modify the label according to the screen size', () => { | ||
const component = render(<LanguageSelectorContext />); | ||
expect(component.queryByRole('button').textContent).toBe('LanguageIconEnglish'); | ||
|
||
global.innerWidth = 700; | ||
global.dispatchEvent(new Event('resize')); | ||
expect(component.queryByRole('button').textContent).toBe('LanguageIconEN'); | ||
|
||
global.innerWidth = 500; | ||
global.dispatchEvent(new Event('resize')); | ||
expect(component.queryByRole('button').textContent).toBe('LanguageIcon'); | ||
}); | ||
}); |