2018-01-29 13:00:15 +00:00
|
|
|
import MockAdaptor from 'axios-mock-adapter';
|
2017-05-30 11:30:26 +00:00
|
|
|
import IntegrationSettingsForm from '~/integrations/integration_settings_form';
|
2021-10-26 12:12:11 +00:00
|
|
|
import eventHub from '~/integrations/edit/event_hub';
|
2021-02-14 18:09:20 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-12-14 00:14:57 +00:00
|
|
|
import { SAVE_INTEGRATION_EVENT } from '~/integrations/constants';
|
2021-10-26 12:12:11 +00:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2020-09-15 18:09:43 +00:00
|
|
|
|
|
|
|
jest.mock('~/vue_shared/plugins/global_toast');
|
2021-10-26 12:12:11 +00:00
|
|
|
jest.mock('lodash/delay', () => (callback) => callback());
|
|
|
|
|
|
|
|
const FIXTURE = 'services/edit_service.html';
|
2021-12-07 12:10:33 +00:00
|
|
|
const mockFormSelector = '.js-integration-settings-form';
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
describe('IntegrationSettingsForm', () => {
|
2021-10-26 12:12:11 +00:00
|
|
|
let integrationSettingsForm;
|
|
|
|
|
|
|
|
const mockStoreDispatch = () => jest.spyOn(integrationSettingsForm.vue.$store, 'dispatch');
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(FIXTURE);
|
2021-10-26 12:12:11 +00:00
|
|
|
|
2021-12-07 12:10:33 +00:00
|
|
|
integrationSettingsForm = new IntegrationSettingsForm(mockFormSelector);
|
2021-10-26 12:12:11 +00:00
|
|
|
integrationSettingsForm.init();
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2021-12-14 00:14:57 +00:00
|
|
|
afterEach(() => {
|
|
|
|
eventHub.dispose(); // clear event hub handlers
|
|
|
|
});
|
|
|
|
|
2020-09-15 18:09:43 +00:00
|
|
|
describe('constructor', () => {
|
2017-05-30 11:30:26 +00:00
|
|
|
it('should initialize form element refs on class object', () => {
|
|
|
|
expect(integrationSettingsForm.$form).toBeDefined();
|
2021-10-04 06:09:05 +00:00
|
|
|
expect(integrationSettingsForm.$form.nodeName).toBe('FORM');
|
2021-12-07 12:10:33 +00:00
|
|
|
expect(integrationSettingsForm.formSelector).toBe(mockFormSelector);
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-26 12:12:11 +00:00
|
|
|
describe('event handling', () => {
|
|
|
|
let mockAxios;
|
2017-05-30 11:30:26 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-10-26 12:12:11 +00:00
|
|
|
mockAxios = new MockAdaptor(axios);
|
2020-04-30 18:09:38 +00:00
|
|
|
jest.spyOn(axios, 'put');
|
2021-12-07 12:10:33 +00:00
|
|
|
jest.spyOn(integrationSettingsForm.$form, 'submit');
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|
|
|
|
|
2018-01-29 13:00:15 +00:00
|
|
|
afterEach(() => {
|
2021-10-26 12:12:11 +00:00
|
|
|
mockAxios.restore();
|
2021-01-24 18:09:28 +00:00
|
|
|
});
|
|
|
|
|
2021-10-26 12:12:11 +00:00
|
|
|
describe('when event hub receives `SAVE_INTEGRATION_EVENT`', () => {
|
|
|
|
describe('when form is valid', () => {
|
|
|
|
it('should submit the form', async () => {
|
2021-12-07 12:10:33 +00:00
|
|
|
eventHub.$emit(SAVE_INTEGRATION_EVENT, true);
|
2021-10-26 12:12:11 +00:00
|
|
|
await waitForPromises();
|
2021-01-24 18:09:28 +00:00
|
|
|
|
2021-10-26 12:12:11 +00:00
|
|
|
expect(integrationSettingsForm.$form.submit).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
2021-01-24 18:09:28 +00:00
|
|
|
|
2021-10-26 12:12:11 +00:00
|
|
|
describe('when form is invalid', () => {
|
|
|
|
it('should dispatch `setIsSaving` with `false` and not submit form', async () => {
|
|
|
|
const dispatchSpy = mockStoreDispatch();
|
2021-01-24 18:09:28 +00:00
|
|
|
|
2021-12-07 12:10:33 +00:00
|
|
|
eventHub.$emit(SAVE_INTEGRATION_EVENT, false);
|
2021-01-24 18:09:28 +00:00
|
|
|
|
2021-10-26 12:12:11 +00:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(dispatchSpy).toHaveBeenCalledWith('setIsSaving', false);
|
|
|
|
expect(integrationSettingsForm.$form.submit).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-01-24 18:09:28 +00:00
|
|
|
});
|
2017-05-30 11:30:26 +00:00
|
|
|
});
|