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

214 lines
6.8 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-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';
jest.mock('~/vue_shared/plugins/global_toast');
2017-09-10 17:25:29 +05:30
describe('IntegrationSettingsForm', () => {
2019-07-07 11:18:12 +05:30
const FIXTURE = 'services/edit_service.html';
2017-09-10 17:25:29 +05:30
beforeEach(() => {
loadFixtures(FIXTURE);
});
2020-11-24 15:15:51 +05:30
describe('constructor', () => {
2017-09-10 17:25:29 +05:30
let integrationSettingsForm;
beforeEach(() => {
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
2020-05-24 23:13:21 +05:30
jest.spyOn(integrationSettingsForm, 'init').mockImplementation(() => {});
2017-09-10 17:25:29 +05:30
});
it('should initialize form element refs on class object', () => {
// Form Reference
expect(integrationSettingsForm.$form).toBeDefined();
expect(integrationSettingsForm.$form.prop('nodeName')).toEqual('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();
});
});
describe('toggleServiceState', () => {
let integrationSettingsForm;
beforeEach(() => {
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
});
it('should remove `novalidate` attribute to form when called with `true`', () => {
2020-04-22 19:07:51 +05:30
integrationSettingsForm.formActive = true;
integrationSettingsForm.toggleServiceState();
2017-09-10 17:25:29 +05:30
expect(integrationSettingsForm.$form.attr('novalidate')).not.toBeDefined();
});
it('should set `novalidate` attribute to form when called with `false`', () => {
2020-04-22 19:07:51 +05:30
integrationSettingsForm.formActive = false;
integrationSettingsForm.toggleServiceState();
2017-09-10 17:25:29 +05:30
expect(integrationSettingsForm.$form.attr('novalidate')).toBeDefined();
});
});
describe('testSettings', () => {
let integrationSettingsForm;
let formData;
2018-03-17 18:26:18 +05:30
let mock;
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2018-03-17 18:26:18 +05:30
mock = new MockAdaptor(axios);
2020-05-24 23:13:21 +05:30
jest.spyOn(axios, 'put');
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
2020-11-24 15:15:51 +05:30
integrationSettingsForm.init();
2019-12-21 20:55:43 +05:30
// eslint-disable-next-line no-jquery/no-serialize
2017-09-10 17:25:29 +05:30
formData = integrationSettingsForm.$form.serialize();
});
2018-03-17 18:26:18 +05:30
afterEach(() => {
mock.restore();
});
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
it('should make an ajax request with provided `formData`', async () => {
await integrationSettingsForm.testSettings(formData);
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
expect(axios.put).toHaveBeenCalledWith(integrationSettingsForm.testEndPoint, formData);
2017-09-10 17:25:29 +05:30
});
2020-11-24 15:15:51 +05:30
it('should show success message if test is successful', async () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(integrationSettingsForm.$form, 'submit').mockImplementation(() => {});
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: false,
});
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
await integrationSettingsForm.testSettings(formData);
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
expect(toast).toHaveBeenCalledWith('Connection successful.');
});
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
it('should show error message if ajax request responds with test error', async () => {
2018-03-17 18:26:18 +05:30
const errorMessage = 'Test failed.';
2020-11-24 15:15:51 +05:30
const serviceResponse = 'some error';
2018-03-17 18:26:18 +05:30
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: true,
message: errorMessage,
2020-11-24 15:15:51 +05:30
service_response: serviceResponse,
test_failed: false,
2018-03-17 18:26:18 +05:30
});
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
await integrationSettingsForm.testSettings(formData);
2018-12-13 13:39:08 +05:30
2020-11-24 15:15:51 +05:30
expect(toast).toHaveBeenCalledWith(`${errorMessage} ${serviceResponse}`);
2017-09-10 17:25:29 +05:30
});
2020-11-24 15:15:51 +05:30
it('should show error message if ajax request failed', async () => {
2017-09-10 17:25:29 +05:30
const errorMessage = 'Something went wrong on our end.';
2018-03-17 18:26:18 +05:30
mock.onPut(integrationSettingsForm.testEndPoint).networkError();
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
await integrationSettingsForm.testSettings(formData);
expect(toast).toHaveBeenCalledWith(errorMessage);
2017-09-10 17:25:29 +05:30
});
2020-11-24 15:15:51 +05:30
it('should always dispatch `setIsTesting` with `false` once request is completed', async () => {
const dispatchSpy = jest.fn();
2018-03-17 18:26:18 +05:30
mock.onPut(integrationSettingsForm.testEndPoint).networkError();
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
integrationSettingsForm.vue.$store = { dispatch: dispatchSpy };
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
await integrationSettingsForm.testSettings(formData);
expect(dispatchSpy).toHaveBeenCalledWith('setIsTesting', false);
2017-09-10 17:25:29 +05:30
});
});
2021-03-11 19:13:27 +05:30
describe('getJiraIssueTypes', () => {
let integrationSettingsForm;
let formData;
let mock;
beforeEach(() => {
mock = new MockAdaptor(axios);
jest.spyOn(axios, 'put');
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
integrationSettingsForm.init();
// eslint-disable-next-line no-jquery/no-serialize
formData = integrationSettingsForm.$form.serialize();
});
afterEach(() => {
mock.restore();
});
it('should always dispatch `requestJiraIssueTypes`', async () => {
const dispatchSpy = jest.fn();
mock.onPut(integrationSettingsForm.testEndPoint).networkError();
integrationSettingsForm.vue.$store = { dispatch: dispatchSpy };
await integrationSettingsForm.getJiraIssueTypes();
expect(dispatchSpy).toHaveBeenCalledWith('requestJiraIssueTypes');
});
it('should make an ajax request with provided `formData`', async () => {
await integrationSettingsForm.getJiraIssueTypes(formData);
expect(axios.put).toHaveBeenCalledWith(integrationSettingsForm.testEndPoint, formData);
});
it('should dispatch `receiveJiraIssueTypesSuccess` with the correct payload if ajax request is successful', async () => {
const mockData = ['ISSUE', 'EPIC'];
const dispatchSpy = jest.fn();
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: false,
issuetypes: mockData,
});
integrationSettingsForm.vue.$store = { dispatch: dispatchSpy };
await integrationSettingsForm.getJiraIssueTypes(formData);
expect(dispatchSpy).toHaveBeenCalledWith('receiveJiraIssueTypesSuccess', mockData);
});
it.each(['something went wrong', undefined])(
'should dispatch "receiveJiraIssueTypesError" with a message if the backend responds with error',
async (responseErrorMessage) => {
const defaultErrorMessage = 'Connection failed. Please check your settings.';
const expectedErrorMessage = responseErrorMessage || defaultErrorMessage;
const dispatchSpy = jest.fn();
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
error: true,
message: responseErrorMessage,
});
integrationSettingsForm.vue.$store = { dispatch: dispatchSpy };
await integrationSettingsForm.getJiraIssueTypes(formData);
expect(dispatchSpy).toHaveBeenCalledWith(
'receiveJiraIssueTypesError',
expectedErrorMessage,
);
},
);
});
2017-09-10 17:25:29 +05:30
});