2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2018-01-29 13:00:15 +00:00
|
|
|
import MockAdaptor from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-05-30 11:30:26 +00:00
|
|
|
import IntegrationSettingsForm from '~/integrations/integration_settings_form';
|
|
|
|
|
|
|
|
describe('IntegrationSettingsForm', () => {
|
2019-03-26 16:03:28 +00:00
|
|
|
const FIXTURE = 'services/edit_service.html';
|
2017-05-30 11:30:26 +00:00
|
|
|
preloadFixtures(FIXTURE);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(FIXTURE);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('contructor', () => {
|
|
|
|
let integrationSettingsForm;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
|
2020-04-30 18:09:38 +00:00
|
|
|
jest.spyOn(integrationSettingsForm, 'init').mockImplementation(() => {});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should initialize form element refs on class object', () => {
|
|
|
|
// Form Reference
|
|
|
|
expect(integrationSettingsForm.$form).toBeDefined();
|
|
|
|
expect(integrationSettingsForm.$form.prop('nodeName')).toEqual('FORM');
|
2020-04-15 12:09:18 +00:00
|
|
|
expect(integrationSettingsForm.formActive).toBeDefined();
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
// Form Child Elements
|
|
|
|
expect(integrationSettingsForm.$submitBtn).toBeDefined();
|
|
|
|
expect(integrationSettingsForm.$submitBtnLoader).toBeDefined();
|
|
|
|
expect(integrationSettingsForm.$submitBtnLabel).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should initialize form metadata on class object', () => {
|
2017-05-31 07:18:02 +00:00
|
|
|
expect(integrationSettingsForm.testEndPoint).toBeDefined();
|
2017-05-30 11:30:26 +00:00
|
|
|
expect(integrationSettingsForm.canTestService).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-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.formActive = true;
|
|
|
|
integrationSettingsForm.toggleServiceState();
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
expect(integrationSettingsForm.$form.attr('novalidate')).not.toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set `novalidate` attribute to form when called with `false`', () => {
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.formActive = false;
|
|
|
|
integrationSettingsForm.toggleServiceState();
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
expect(integrationSettingsForm.$form.attr('novalidate')).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('toggleSubmitBtnLabel', () => {
|
|
|
|
let integrationSettingsForm;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set Save button label to "Test settings and save changes" when serviceActive & canTestService are `true`', () => {
|
2017-05-31 07:18:02 +00:00
|
|
|
integrationSettingsForm.canTestService = true;
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.formActive = true;
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.toggleSubmitBtnLabel();
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(integrationSettingsForm.$submitBtnLabel.text()).toEqual(
|
|
|
|
'Test settings and save changes',
|
|
|
|
);
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set Save button label to "Save changes" when either serviceActive or canTestService (or both) is `false`', () => {
|
2017-05-31 07:18:02 +00:00
|
|
|
integrationSettingsForm.canTestService = false;
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.formActive = false;
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.toggleSubmitBtnLabel();
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2017-05-30 11:30:26 +00:00
|
|
|
expect(integrationSettingsForm.$submitBtnLabel.text()).toEqual('Save changes');
|
|
|
|
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.formActive = true;
|
|
|
|
|
|
|
|
integrationSettingsForm.toggleSubmitBtnLabel();
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2017-05-30 11:30:26 +00:00
|
|
|
expect(integrationSettingsForm.$submitBtnLabel.text()).toEqual('Save changes');
|
|
|
|
|
2017-05-31 07:18:02 +00:00
|
|
|
integrationSettingsForm.canTestService = true;
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.formActive = false;
|
2017-05-31 07:18:02 +00:00
|
|
|
|
2020-04-15 12:09:18 +00:00
|
|
|
integrationSettingsForm.toggleSubmitBtnLabel();
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2017-05-30 11:30:26 +00:00
|
|
|
expect(integrationSettingsForm.$submitBtnLabel.text()).toEqual('Save changes');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('toggleSubmitBtnState', () => {
|
|
|
|
let integrationSettingsForm;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should disable Save button and show loader animation when called with `true`', () => {
|
|
|
|
integrationSettingsForm.toggleSubmitBtnState(true);
|
|
|
|
|
|
|
|
expect(integrationSettingsForm.$submitBtn.is(':disabled')).toBeTruthy();
|
|
|
|
expect(integrationSettingsForm.$submitBtnLoader.hasClass('hidden')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should enable Save button and hide loader animation when called with `false`', () => {
|
|
|
|
integrationSettingsForm.toggleSubmitBtnState(false);
|
|
|
|
|
|
|
|
expect(integrationSettingsForm.$submitBtn.is(':disabled')).toBeFalsy();
|
|
|
|
expect(integrationSettingsForm.$submitBtnLoader.hasClass('hidden')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('testSettings', () => {
|
|
|
|
let integrationSettingsForm;
|
|
|
|
let formData;
|
2018-01-29 13:00:15 +00:00
|
|
|
let mock;
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-01-29 13:00:15 +00:00
|
|
|
mock = new MockAdaptor(axios);
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
jest.spyOn(axios, 'put');
|
2018-01-29 13:00:15 +00:00
|
|
|
|
2017-05-30 11:30:26 +00:00
|
|
|
integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
|
2019-09-19 21:06:29 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-serialize
|
2017-05-30 11:30:26 +00:00
|
|
|
formData = integrationSettingsForm.$form.serialize();
|
|
|
|
});
|
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should make an ajax request with provided `formData`', () => {
|
|
|
|
return integrationSettingsForm.testSettings(formData).then(() => {
|
|
|
|
expect(axios.put).toHaveBeenCalledWith(integrationSettingsForm.testEndPoint, formData);
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should show error Flash with `Save anyway` action if ajax request responds with error in test', () => {
|
2017-05-30 11:30:26 +00:00
|
|
|
const errorMessage = 'Test failed.';
|
2018-01-29 13:00:15 +00:00
|
|
|
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
|
|
|
|
error: true,
|
|
|
|
message: errorMessage,
|
|
|
|
service_response: 'some error',
|
2018-06-01 11:43:53 +00:00
|
|
|
test_failed: true,
|
2018-01-29 13:00:15 +00:00
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
return integrationSettingsForm.testSettings(formData).then(() => {
|
|
|
|
const $flashContainer = $('.flash-container');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
$flashContainer
|
|
|
|
.find('.flash-text')
|
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
).toEqual('Test failed. some error');
|
|
|
|
|
|
|
|
expect($flashContainer.find('.flash-action')).toBeDefined();
|
|
|
|
expect(
|
|
|
|
$flashContainer
|
|
|
|
.find('.flash-action')
|
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
).toEqual('Save anyway');
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should not show error Flash with `Save anyway` action if ajax request responds with error in validation', () => {
|
2018-06-01 11:43:53 +00:00
|
|
|
const errorMessage = 'Validations failed.';
|
|
|
|
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
|
|
|
|
error: true,
|
|
|
|
message: errorMessage,
|
|
|
|
service_response: 'some error',
|
|
|
|
test_failed: false,
|
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
return integrationSettingsForm.testSettings(formData).then(() => {
|
|
|
|
const $flashContainer = $('.flash-container');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
$flashContainer
|
|
|
|
.find('.flash-text')
|
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
).toEqual('Validations failed. some error');
|
|
|
|
|
|
|
|
expect($flashContainer.find('.flash-action')).toBeDefined();
|
|
|
|
expect(
|
|
|
|
$flashContainer
|
|
|
|
.find('.flash-action')
|
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
).toEqual('');
|
|
|
|
});
|
2018-06-01 11:43:53 +00:00
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should submit form if ajax request responds without any error in test', () => {
|
|
|
|
jest.spyOn(integrationSettingsForm.$form, 'submit').mockImplementation(() => {});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
|
|
|
|
error: false,
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
return integrationSettingsForm.testSettings(formData).then(() => {
|
|
|
|
expect(integrationSettingsForm.$form.submit).toHaveBeenCalled();
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should submit form when clicked on `Save anyway` action of error Flash', () => {
|
|
|
|
jest.spyOn(integrationSettingsForm.$form, 'submit').mockImplementation(() => {});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
const errorMessage = 'Test failed.';
|
|
|
|
mock.onPut(integrationSettingsForm.testEndPoint).reply(200, {
|
|
|
|
error: true,
|
|
|
|
message: errorMessage,
|
2018-06-01 11:43:53 +00:00
|
|
|
test_failed: true,
|
2018-01-29 13:00:15 +00:00
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
return integrationSettingsForm
|
2018-10-17 07:13:26 +00:00
|
|
|
.testSettings(formData)
|
2018-01-29 13:00:15 +00:00
|
|
|
.then(() => {
|
|
|
|
const $flashAction = $('.flash-container .flash-action');
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
expect($flashAction).toBeDefined();
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
$flashAction.get(0).click();
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(integrationSettingsForm.$form.submit).toHaveBeenCalled();
|
2020-04-30 18:09:38 +00:00
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should show error Flash if ajax request failed', () => {
|
2017-05-30 11:30:26 +00:00
|
|
|
const errorMessage = 'Something went wrong on our end.';
|
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
mock.onPut(integrationSettingsForm.testEndPoint).networkError();
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
return integrationSettingsForm.testSettings(formData).then(() => {
|
|
|
|
expect(
|
|
|
|
$('.flash-container .flash-text')
|
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
).toEqual(errorMessage);
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
it('should always call `toggleSubmitBtnState` with `false` once request is completed', () => {
|
2018-01-29 13:00:15 +00:00
|
|
|
mock.onPut(integrationSettingsForm.testEndPoint).networkError();
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
jest.spyOn(integrationSettingsForm, 'toggleSubmitBtnState').mockImplementation(() => {});
|
2017-05-30 11:30:26 +00:00
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
return integrationSettingsForm.testSettings(formData).then(() => {
|
|
|
|
expect(integrationSettingsForm.toggleSubmitBtnState).toHaveBeenCalledWith(false);
|
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|