|
| 1 | +import React from 'react'; |
| 2 | +import { mergeConfig } from '@edx/frontend-platform'; |
| 3 | +import { getCookies } from '@edx/frontend-platform/i18n/lib'; |
| 4 | +import { changeUserSessionLanguage } from '@edx/frontend-platform/i18n'; |
| 5 | +import { |
| 6 | + act, fireEvent, initializeMockApp, render, screen, |
| 7 | +} from '../setupTest'; |
| 8 | +import LanguageSelector from './LanguageSelector'; |
| 9 | + |
| 10 | +jest.mock('@edx/frontend-platform/i18n', () => ({ |
| 11 | + ...jest.requireActual('@edx/frontend-platform/i18n'), |
| 12 | + changeUserSessionLanguage: jest.fn().mockResolvedValue({}), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('@openedx/paragon/icons', () => ({ |
| 16 | + Language: () => <div>LanguageIcon</div>, |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('@openedx/paragon', () => ({ |
| 20 | + ...jest.requireActual('@openedx/paragon'), |
| 21 | + useWindowSize: () => ({ width: global.innerWidth }), |
| 22 | +})); |
| 23 | + |
| 24 | +const LANGUAGE_PREFERENCE_COOKIE_NAME = 'language-preference'; |
| 25 | + |
| 26 | +describe('LanguageSelector', () => { |
| 27 | + let mockReload; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + |
| 32 | + mergeConfig({ |
| 33 | + ENABLE_HEADER_LANG_SELECTOR: true, |
| 34 | + LANGUAGE_PREFERENCE_COOKIE_NAME, |
| 35 | + SITE_SUPPORTED_LANGUAGES: ['es', 'en'], |
| 36 | + }); |
| 37 | + |
| 38 | + initializeMockApp(); |
| 39 | + |
| 40 | + mockReload = jest.fn(); |
| 41 | + Object.defineProperty(window, 'location', { |
| 42 | + configurable: true, |
| 43 | + writable: true, |
| 44 | + value: { reload: mockReload }, |
| 45 | + }); |
| 46 | + |
| 47 | + global.innerWidth = 1200; |
| 48 | + }); |
| 49 | + |
| 50 | + it('should not render when no supported languages are available', () => { |
| 51 | + mergeConfig({ |
| 52 | + SITE_SUPPORTED_LANGUAGES: [], |
| 53 | + }); |
| 54 | + |
| 55 | + const { container } = render(<LanguageSelector />); |
| 56 | + expect(container).toMatchSnapshot('no-supported-languages'); |
| 57 | + expect(container.querySelector('#language-selector')).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should change the language when different language is selected', async () => { |
| 61 | + jest.spyOn(getCookies(), 'get').mockImplementation(() => 'en'); |
| 62 | + |
| 63 | + const { container } = render(<LanguageSelector />); |
| 64 | + expect(container).toMatchSnapshot('before-language-change'); |
| 65 | + |
| 66 | + const langDropdown = screen.getByRole('button', { id: 'lang-selector-dropdown' }); |
| 67 | + fireEvent.click(langDropdown); |
| 68 | + |
| 69 | + const spanishOption = screen.getByRole('button', { name: 'Español' }); |
| 70 | + |
| 71 | + await act(async () => { |
| 72 | + fireEvent.click(spanishOption); |
| 73 | + }); |
| 74 | + |
| 75 | + expect(container).toMatchSnapshot('after-language-change'); |
| 76 | + expect(changeUserSessionLanguage).toHaveBeenCalledWith('es'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not change language if the same language is selected', async () => { |
| 80 | + jest.spyOn(getCookies(), 'get').mockImplementation(() => 'en'); |
| 81 | + |
| 82 | + const { container } = render(<LanguageSelector />); |
| 83 | + expect(container).toMatchSnapshot('before-same-language-selection'); |
| 84 | + |
| 85 | + const langDropdown = screen.getByRole('button', { id: 'lang-selector-dropdown' }); |
| 86 | + fireEvent.click(langDropdown); |
| 87 | + |
| 88 | + const englishOption = screen.getByRole('button', { name: 'English' }); |
| 89 | + await act(async () => { |
| 90 | + fireEvent.click(englishOption); |
| 91 | + }); |
| 92 | + |
| 93 | + expect(container).toMatchSnapshot('after-same-language-selection'); |
| 94 | + expect(changeUserSessionLanguage).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should display full language name on large screens', () => { |
| 98 | + jest.spyOn(getCookies(), 'get').mockImplementation(() => 'en'); |
| 99 | + |
| 100 | + global.innerWidth = 1200; |
| 101 | + render(<LanguageSelector />); |
| 102 | + |
| 103 | + const button = screen.getByRole('button', { id: 'lang-selector-dropdown' }); |
| 104 | + expect(button).toMatchSnapshot('large-screen-button'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should display language code on medium screens', () => { |
| 108 | + jest.spyOn(getCookies(), 'get').mockImplementation(() => 'en'); |
| 109 | + |
| 110 | + global.innerWidth = 700; |
| 111 | + render(<LanguageSelector />); |
| 112 | + |
| 113 | + const button = screen.getByRole('button', { id: 'lang-selector-dropdown' }); |
| 114 | + expect(button).toMatchSnapshot('medium-screen-button'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should display only icon on small screens', () => { |
| 118 | + jest.spyOn(getCookies(), 'get').mockImplementation(() => 'en'); |
| 119 | + |
| 120 | + global.innerWidth = 500; |
| 121 | + render(<LanguageSelector />); |
| 122 | + |
| 123 | + const button = screen.getByRole('button', { id: 'lang-selector-dropdown' }); |
| 124 | + expect(button).toMatchSnapshot('small-screen-button'); |
| 125 | + }); |
| 126 | +}); |
0 commit comments