debian-mirror-gitlab/spec/frontend/integrations/integration_settings_form_spec.js

249 lines
8.7 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import MockAdaptor from 'axios-mock-adapter';
2017-09-10 17:25:29 +05:30
import IntegrationSettingsForm from '~/integrations/integration_settings_form';
2021-12-11 22:18:48 +05:30
import eventHub from '~/integrations/edit/event_hub';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2020-11-24 15:15:51 +05:30
import toast from '~/vue_shared/plugins/global_toast';
2021-12-11 22:18:48 +05:30
import {
I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE,
I18N_SUCCESSFUL_CONNECTION_MESSAGE,
I18N_DEFAULT_ERROR_MESSAGE,
GET_JIRA_ISSUE_TYPES_EVENT,
TOGGLE_INTEGRATION_EVENT,
TEST_INTEGRATION_EVENT,
SAVE_INTEGRATION_EVENT,
} from '~/integrations/constants';
import waitForPromises from 'helpers/wait_for_promises';
2020-11-24 15:15:51 +05:30
jest.mock('~/vue_shared/plugins/global_toast');
2021-12-11 22:18:48 +05:30
jest.mock('lodash/delay', () => (callback) => callback());
const FIXTURE = 'services/edit_service.html';
2017-09-10 17:25:29 +05:30
describe('IntegrationSettingsForm', () => {
2021-12-11 22:18:48 +05:30
let integrationSettingsForm;
const mockStoreDispatch = () => jest.spyOn(integrationSettingsForm.vue.$store, 'dispatch');
2017-09-10 17:25:29 +05:30
beforeEach(() => {
loadFixtures(FIXTURE);
2021-12-11 22:18:48 +05:30
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
integrationSettingsForm.init();
2017-09-10 17:25:29 +05:30
});
2020-11-24 15:15:51 +05:30
describe('constructor', () => {
2017-09-10 17:25:29 +05:30
it('should initialize form element refs on class object', () => {
expect(integrationSettingsForm.$form).toBeDefined();
2021-11-18 22:05:49 +05:30
expect(integrationSettingsForm.$form.nodeName).toBe('FORM');
2020-04-22 19:07:51 +05:30
expect(integrationSettingsForm.formActive).toBeDefined();
2017-09-10 17:25:29 +05:30
});
it('should initialize form metadata on class object', () => {
expect(integrationSettingsForm.testEndPoint).toBeDefined();
});
});
2021-12-11 22:18:48 +05:30
describe('event handling', () => {
let mockAxios;
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2021-12-11 22:18:48 +05:30
mockAxios = new MockAdaptor(axios);
2020-05-24 23:13:21 +05:30
jest.spyOn(axios, 'put');
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
afterEach(() => {
2021-12-11 22:18:48 +05:30
mockAxios.restore();
eventHub.dispose(); // clear event hub handlers
2018-03-17 18:26:18 +05:30
});
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
describe('when event hub receives `TOGGLE_INTEGRATION_EVENT`', () => {
it('should remove `novalidate` attribute to form when called with `true`', () => {
eventHub.$emit(TOGGLE_INTEGRATION_EVENT, true);
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(integrationSettingsForm.$form.getAttribute('novalidate')).toBe(null);
});
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
it('should set `novalidate` attribute to form when called with `false`', () => {
eventHub.$emit(TOGGLE_INTEGRATION_EVENT, false);
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(integrationSettingsForm.$form.getAttribute('novalidate')).toBe('novalidate');
2018-03-17 18:26:18 +05:30
});
2021-12-11 22:18:48 +05:30
});
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
describe('when event hub receives `TEST_INTEGRATION_EVENT`', () => {
describe('when form is valid', () => {
beforeEach(() => {
jest.spyOn(integrationSettingsForm.$form, 'checkValidity').mockReturnValue(true);
});
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
it('should make an ajax request with provided `formData`', async () => {
eventHub.$emit(TEST_INTEGRATION_EVENT);
await waitForPromises();
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(axios.put).toHaveBeenCalledWith(
integrationSettingsForm.testEndPoint,
new FormData(integrationSettingsForm.$form),
);
});
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
it('should show success message if test is successful', async () => {
jest.spyOn(integrationSettingsForm.$form, 'submit').mockImplementation(() => {});
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
mockAxios.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: false,
});
2018-12-13 13:39:08 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(TEST_INTEGRATION_EVENT);
await waitForPromises();
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(toast).toHaveBeenCalledWith(I18N_SUCCESSFUL_CONNECTION_MESSAGE);
});
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
it('should show error message if ajax request responds with test error', async () => {
const errorMessage = 'Test failed.';
const serviceResponse = 'some error';
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
mockAxios.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: true,
message: errorMessage,
service_response: serviceResponse,
test_failed: false,
});
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(TEST_INTEGRATION_EVENT);
await waitForPromises();
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(toast).toHaveBeenCalledWith(`${errorMessage} ${serviceResponse}`);
});
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
it('should show error message if ajax request failed', async () => {
mockAxios.onPut(integrationSettingsForm.testEndPoint).networkError();
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(TEST_INTEGRATION_EVENT);
await waitForPromises();
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(toast).toHaveBeenCalledWith(I18N_DEFAULT_ERROR_MESSAGE);
});
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
it('should always dispatch `setIsTesting` with `false` once request is completed', async () => {
const dispatchSpy = mockStoreDispatch();
mockAxios.onPut(integrationSettingsForm.testEndPoint).networkError();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(TEST_INTEGRATION_EVENT);
await waitForPromises();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
expect(dispatchSpy).toHaveBeenCalledWith('setIsTesting', false);
});
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
describe('when form is invalid', () => {
beforeEach(() => {
jest.spyOn(integrationSettingsForm.$form, 'checkValidity').mockReturnValue(false);
jest.spyOn(integrationSettingsForm, 'testSettings');
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
it('should dispatch `setIsTesting` with `false` and not call `testSettings`', async () => {
const dispatchSpy = mockStoreDispatch();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(TEST_INTEGRATION_EVENT);
await waitForPromises();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
expect(dispatchSpy).toHaveBeenCalledWith('setIsTesting', false);
expect(integrationSettingsForm.testSettings).not.toHaveBeenCalled();
});
});
2021-03-11 19:13:27 +05:30
});
2021-12-11 22:18:48 +05:30
describe('when event hub receives `GET_JIRA_ISSUE_TYPES_EVENT`', () => {
it('should always dispatch `requestJiraIssueTypes`', () => {
const dispatchSpy = mockStoreDispatch();
mockAxios.onPut(integrationSettingsForm.testEndPoint).networkError();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(GET_JIRA_ISSUE_TYPES_EVENT);
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
expect(dispatchSpy).toHaveBeenCalledWith('requestJiraIssueTypes');
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
it('should make an ajax request with provided `formData`', () => {
eventHub.$emit(GET_JIRA_ISSUE_TYPES_EVENT);
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
expect(axios.put).toHaveBeenCalledWith(
integrationSettingsForm.testEndPoint,
new FormData(integrationSettingsForm.$form),
);
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
it('should dispatch `receiveJiraIssueTypesSuccess` with the correct payload if ajax request is successful', async () => {
const dispatchSpy = mockStoreDispatch();
const mockData = ['ISSUE', 'EPIC'];
mockAxios.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: false,
issuetypes: mockData,
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(GET_JIRA_ISSUE_TYPES_EVENT);
await waitForPromises();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
expect(dispatchSpy).toHaveBeenCalledWith('receiveJiraIssueTypesSuccess', mockData);
2021-03-11 19:13:27 +05:30
});
2021-12-11 22:18:48 +05:30
it.each(['Custom error message here', undefined])(
'should dispatch "receiveJiraIssueTypesError" with a message if the backend responds with error',
async (responseErrorMessage) => {
const dispatchSpy = mockStoreDispatch();
const expectedErrorMessage =
responseErrorMessage || I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE;
mockAxios.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: true,
message: responseErrorMessage,
});
eventHub.$emit(GET_JIRA_ISSUE_TYPES_EVENT);
await waitForPromises();
expect(dispatchSpy).toHaveBeenCalledWith(
'receiveJiraIssueTypesError',
expectedErrorMessage,
);
},
);
});
describe('when event hub receives `SAVE_INTEGRATION_EVENT`', () => {
describe('when form is valid', () => {
beforeEach(() => {
jest.spyOn(integrationSettingsForm.$form, 'checkValidity').mockReturnValue(true);
jest.spyOn(integrationSettingsForm.$form, 'submit');
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
it('should submit the form', async () => {
eventHub.$emit(SAVE_INTEGRATION_EVENT);
await waitForPromises();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
expect(integrationSettingsForm.$form.submit).toHaveBeenCalled();
expect(integrationSettingsForm.$form.submit).toHaveBeenCalledTimes(1);
});
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
describe('when form is invalid', () => {
beforeEach(() => {
jest.spyOn(integrationSettingsForm.$form, 'checkValidity').mockReturnValue(false);
jest.spyOn(integrationSettingsForm.$form, 'submit');
2021-03-11 19:13:27 +05:30
});
2021-12-11 22:18:48 +05:30
it('should dispatch `setIsSaving` with `false` and not submit form', async () => {
const dispatchSpy = mockStoreDispatch();
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
eventHub.$emit(SAVE_INTEGRATION_EVENT);
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
await waitForPromises();
expect(dispatchSpy).toHaveBeenCalledWith('setIsSaving', false);
expect(integrationSettingsForm.$form.submit).not.toHaveBeenCalled();
});
});
});
2021-03-11 19:13:27 +05:30
});
2017-09-10 17:25:29 +05:30
});