debian-mirror-gitlab/spec/frontend/profile/preferences/components/profile_preferences_spec.js

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

250 lines
7.1 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlButton } from '@gitlab/ui';
2021-01-29 00:20:46 +05:30
import { shallowMount } from '@vue/test-utils';
2021-04-17 20:07:23 +05:30
import { nextTick } from 'vue';
2021-10-27 15:23:28 +05:30
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
2021-03-11 19:13:27 +05:30
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2021-01-29 00:20:46 +05:30
import IntegrationView from '~/profile/preferences/components/integration_view.vue';
2021-03-11 19:13:27 +05:30
import ProfilePreferences from '~/profile/preferences/components/profile_preferences.vue';
import { i18n } from '~/profile/preferences/constants';
2021-04-17 20:07:23 +05:30
import {
integrationViews,
userFields,
bodyClasses,
themes,
lightModeThemeId1,
darkModeThemeId,
lightModeThemeId2,
} from '../mock_data';
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
jest.mock('~/flash');
2021-03-11 19:13:27 +05:30
const expectedUrl = '/foo';
2021-01-29 00:20:46 +05:30
2021-10-27 15:23:28 +05:30
useMockLocationHelper();
2021-01-29 00:20:46 +05:30
describe('ProfilePreferences component', () => {
let wrapper;
const defaultProvide = {
integrationViews: [],
userFields,
2021-03-11 19:13:27 +05:30
bodyClasses,
2021-04-17 20:07:23 +05:30
themes,
2021-03-11 19:13:27 +05:30
profilePreferencesPath: '/update-profile',
formEl: document.createElement('form'),
2021-01-29 00:20:46 +05:30
};
function createComponent(options = {}) {
2021-03-11 19:13:27 +05:30
const { props = {}, provide = {}, attachTo } = options;
return extendedWrapper(
shallowMount(ProfilePreferences, {
provide: {
...defaultProvide,
...provide,
},
propsData: props,
attachTo,
}),
);
}
function findIntegrationsDivider() {
return wrapper.findByTestId('profile-preferences-integrations-rule');
}
function findIntegrationsHeading() {
return wrapper.findByTestId('profile-preferences-integrations-heading');
}
function findSubmitButton() {
return wrapper.findComponent(GlButton);
2021-01-29 00:20:46 +05:30
}
2021-04-17 20:07:23 +05:30
function createThemeInput(themeId = lightModeThemeId1) {
const input = document.createElement('input');
input.setAttribute('name', 'user[theme_id]');
input.setAttribute('type', 'radio');
input.setAttribute('value', themeId.toString());
input.setAttribute('checked', 'checked');
return input;
}
function createForm(themeInput = createThemeInput()) {
const form = document.createElement('form');
form.setAttribute('url', expectedUrl);
form.setAttribute('method', 'put');
form.appendChild(themeInput);
return form;
}
function setupBody() {
const div = document.createElement('div');
div.classList.add('container-fluid');
document.body.appendChild(div);
document.body.classList.add('content-wrapper');
}
2021-01-29 00:20:46 +05:30
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
it('should not render Integrations section', () => {
wrapper = createComponent();
2022-10-11 01:57:18 +05:30
const views = wrapper.findAllComponents(IntegrationView);
2021-03-11 19:13:27 +05:30
const divider = findIntegrationsDivider();
const heading = findIntegrationsHeading();
2021-01-29 00:20:46 +05:30
expect(divider.exists()).toBe(false);
expect(heading.exists()).toBe(false);
expect(views).toHaveLength(0);
});
it('should render Integration section', () => {
wrapper = createComponent({ provide: { integrationViews } });
2021-03-11 19:13:27 +05:30
const divider = findIntegrationsDivider();
const heading = findIntegrationsHeading();
2022-10-11 01:57:18 +05:30
const views = wrapper.findAllComponents(IntegrationView);
2021-01-29 00:20:46 +05:30
expect(divider.exists()).toBe(true);
expect(heading.exists()).toBe(true);
expect(views).toHaveLength(integrationViews.length);
});
2021-03-11 19:13:27 +05:30
describe('form submit', () => {
let form;
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
beforeEach(() => {
2021-04-17 20:07:23 +05:30
setupBody();
form = createForm();
2021-03-11 19:13:27 +05:30
wrapper = createComponent({ provide: { formEl: form }, attachTo: document.body });
const beforeSendEvent = new CustomEvent('ajax:beforeSend');
form.dispatchEvent(beforeSendEvent);
});
it('disables the submit button', async () => {
2021-04-17 20:07:23 +05:30
await nextTick();
2021-03-11 19:13:27 +05:30
const button = findSubmitButton();
expect(button.props('disabled')).toBe(true);
});
it('success re-enables the submit button', async () => {
const successEvent = new CustomEvent('ajax:success');
form.dispatchEvent(successEvent);
2021-04-17 20:07:23 +05:30
await nextTick();
2021-03-11 19:13:27 +05:30
const button = findSubmitButton();
expect(button.props('disabled')).toBe(false);
});
it('error re-enables the submit button', async () => {
const errorEvent = new CustomEvent('ajax:error');
form.dispatchEvent(errorEvent);
2021-04-17 20:07:23 +05:30
await nextTick();
2021-03-11 19:13:27 +05:30
const button = findSubmitButton();
expect(button.props('disabled')).toBe(false);
});
it('displays the default success message', () => {
const successEvent = new CustomEvent('ajax:success');
form.dispatchEvent(successEvent);
2021-09-30 23:02:18 +05:30
expect(createFlash).toHaveBeenCalledWith({ message: i18n.defaultSuccess, type: 'notice' });
2021-03-11 19:13:27 +05:30
});
it('displays the custom success message', () => {
const message = 'foo';
const successEvent = new CustomEvent('ajax:success', { detail: [{ message }] });
form.dispatchEvent(successEvent);
2021-09-30 23:02:18 +05:30
expect(createFlash).toHaveBeenCalledWith({ message, type: 'notice' });
2021-03-11 19:13:27 +05:30
});
it('displays the default error message', () => {
const errorEvent = new CustomEvent('ajax:error');
form.dispatchEvent(errorEvent);
2021-09-30 23:02:18 +05:30
expect(createFlash).toHaveBeenCalledWith({ message: i18n.defaultError, type: 'alert' });
2021-03-11 19:13:27 +05:30
});
it('displays the custom error message', () => {
const message = 'bar';
const errorEvent = new CustomEvent('ajax:error', { detail: [{ message }] });
form.dispatchEvent(errorEvent);
2021-09-30 23:02:18 +05:30
expect(createFlash).toHaveBeenCalledWith({ message, type: 'alert' });
2021-03-11 19:13:27 +05:30
});
2021-01-29 00:20:46 +05:30
});
2021-04-17 20:07:23 +05:30
describe('theme changes', () => {
let themeInput;
let form;
function setupWrapper() {
wrapper = createComponent({ provide: { formEl: form }, attachTo: document.body });
}
function selectThemeId(themeId) {
themeInput.setAttribute('value', themeId.toString());
}
function dispatchBeforeSendEvent() {
const beforeSendEvent = new CustomEvent('ajax:beforeSend');
form.dispatchEvent(beforeSendEvent);
}
function dispatchSuccessEvent() {
const successEvent = new CustomEvent('ajax:success');
form.dispatchEvent(successEvent);
}
beforeEach(() => {
setupBody();
themeInput = createThemeInput();
form = createForm(themeInput);
});
it('reloads the page when switching from light to dark mode', async () => {
selectThemeId(lightModeThemeId1);
setupWrapper();
selectThemeId(darkModeThemeId);
dispatchBeforeSendEvent();
await nextTick();
dispatchSuccessEvent();
await nextTick();
expect(window.location.reload).toHaveBeenCalledTimes(1);
});
it('reloads the page when switching from dark to light mode', async () => {
selectThemeId(darkModeThemeId);
setupWrapper();
selectThemeId(lightModeThemeId1);
dispatchBeforeSendEvent();
await nextTick();
dispatchSuccessEvent();
await nextTick();
expect(window.location.reload).toHaveBeenCalledTimes(1);
});
it('does not reload the page when switching between light mode themes', async () => {
selectThemeId(lightModeThemeId1);
setupWrapper();
selectThemeId(lightModeThemeId2);
dispatchBeforeSendEvent();
await nextTick();
dispatchSuccessEvent();
await nextTick();
expect(window.location.reload).not.toHaveBeenCalled();
});
});
2021-01-29 00:20:46 +05:30
});