debian-mirror-gitlab/spec/frontend/language_switcher/components/app_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-03-04 22:38:38 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
import LanguageSwitcherApp from '~/language_switcher/components/app.vue';
import { PREFERRED_LANGUAGE_COOKIE_KEY } from '~/language_switcher/constants';
import * as utils from '~/lib/utils/common_utils';
import { locales, ES, EN } from '../mock_data';
jest.mock('~/lib/utils/common_utils');
describe('<LanguageSwitcher />', () => {
let wrapper;
const createComponent = (props = {}) => {
wrapper = mountExtended(LanguageSwitcherApp, {
provide: {
locales,
preferredLocale: EN,
...props,
},
});
};
beforeEach(() => {
createComponent();
});
afterEach(() => {
wrapper.destroy();
});
const getPreferredLanguage = () => wrapper.find('.gl-dropdown-button-text').text();
const findLanguageDropdownItem = (code) => wrapper.findByTestId(`language_switcher_lang_${code}`);
it('preferred language', () => {
expect(getPreferredLanguage()).toBe(EN.text);
createComponent({
preferredLocale: ES,
});
expect(getPreferredLanguage()).toBe(ES.text);
});
it('switches language', async () => {
// because window.location is **READ ONLY** we cannot simply use
// jest.spyOn to mock it.
const originalLocation = window.location;
delete window.location;
window.location = {};
window.location.reload = jest.fn();
const reloadSpy = window.location.reload;
expect(reloadSpy).not.toHaveBeenCalled();
expect(utils.setCookie).not.toHaveBeenCalled();
const es = findLanguageDropdownItem(ES.value);
await es.trigger('click');
expect(reloadSpy).toHaveBeenCalled();
expect(utils.setCookie).toHaveBeenCalledWith(PREFERRED_LANGUAGE_COOKIE_KEY, ES.value);
window.location = originalLocation;
});
});