debian-mirror-gitlab/spec/frontend/badges/components/badge_form_spec.js

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

184 lines
5.2 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-11-25 23:54:43 +05:30
import Vue from 'vue';
import Vuex from 'vuex';
import { mount } from '@vue/test-utils';
2020-10-24 23:57:45 +05:30
import { DUMMY_IMAGE_URL, TEST_HOST } from 'helpers/test_constants';
2018-05-09 12:01:36 +05:30
import BadgeForm from '~/badges/components/badge_form.vue';
2021-03-11 19:13:27 +05:30
import createEmptyBadge from '~/badges/empty_badge';
2022-11-25 23:54:43 +05:30
import createState from '~/badges/store/state';
import mutations from '~/badges/store/mutations';
import actions from '~/badges/store/actions';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2018-11-20 20:47:30 +05:30
2022-11-25 23:54:43 +05:30
Vue.use(Vuex);
2018-05-09 12:01:36 +05:30
describe('BadgeForm component', () => {
2018-11-20 20:47:30 +05:30
let axiosMock;
2022-11-25 23:54:43 +05:30
let mockedActions;
let wrapper;
const createComponent = (propsData, customState = {}) => {
mockedActions = Object.fromEntries(Object.keys(actions).map((name) => [name, jest.fn()]));
const store = new Vuex.Store({
state: {
...createState(),
...customState,
},
mutations,
actions: mockedActions,
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
wrapper = mount(BadgeForm, {
store,
propsData,
attachTo: document.body,
});
};
2018-11-20 20:47:30 +05:30
2022-11-25 23:54:43 +05:30
beforeEach(() => {
2018-11-20 20:47:30 +05:30
axiosMock = new MockAdapter(axios);
2018-05-09 12:01:36 +05:30
});
afterEach(() => {
2022-11-25 23:54:43 +05:30
wrapper.destroy();
2018-11-20 20:47:30 +05:30
axiosMock.restore();
2018-05-09 12:01:36 +05:30
});
2022-11-25 23:54:43 +05:30
it('stops editing when cancel button is clicked', async () => {
createComponent({ isEditing: true });
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
const cancelButton = wrapper.find('.row-content-block button');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await cancelButton.trigger('click');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
expect(mockedActions.stopEditing).toHaveBeenCalled();
2018-11-20 20:47:30 +05:30
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
const sharedSubmitTests = (submitAction, props) => {
2020-01-01 13:55:28 +05:30
const nameSelector = '#badge-name';
2018-11-20 20:47:30 +05:30
const imageUrlSelector = '#badge-image-url';
2022-11-25 23:54:43 +05:30
const findImageUrl = () => wrapper.find(imageUrlSelector);
2018-11-20 20:47:30 +05:30
const linkUrlSelector = '#badge-link-url';
2022-11-25 23:54:43 +05:30
const findLinkUrl = () => wrapper.find(linkUrlSelector);
2020-01-01 13:55:28 +05:30
const setValue = (inputElementSelector, value) => {
2022-11-25 23:54:43 +05:30
const input = wrapper.find(inputElementSelector);
return input.setValue(value);
2018-11-20 20:47:30 +05:30
};
const submitForm = () => {
2022-11-25 23:54:43 +05:30
const submitButton = wrapper.find('button[type="submit"]');
return submitButton.trigger('click');
2018-11-20 20:47:30 +05:30
};
2021-03-08 18:12:59 +05:30
const expectInvalidInput = (inputElementSelector) => {
2022-11-25 23:54:43 +05:30
const input = wrapper.find(inputElementSelector);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(input.element.checkValidity()).toBe(false);
const feedbackElement = wrapper.find(`${inputElementSelector} + .invalid-feedback`);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(feedbackElement.isVisible()).toBe(true);
2018-11-20 20:47:30 +05:30
};
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
beforeEach(() => {
createComponent(props, {
2018-11-20 20:47:30 +05:30
badgeInAddForm: createEmptyBadge(),
badgeInEditForm: createEmptyBadge(),
isSaving: false,
});
2018-05-09 12:01:36 +05:30
2022-04-04 11:22:00 +05:30
setValue(nameSelector, 'TestBadge');
setValue(linkUrlSelector, `${TEST_HOST}/link/url`);
setValue(imageUrlSelector, `${window.location.origin}${DUMMY_IMAGE_URL}`);
2018-11-20 20:47:30 +05:30
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it('returns immediately if imageUrl is empty', async () => {
await setValue(imageUrlSelector, '');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await submitForm();
2018-05-09 12:01:36 +05:30
2018-11-20 20:47:30 +05:30
expectInvalidInput(imageUrlSelector);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(mockedActions[submitAction]).not.toHaveBeenCalled();
2018-11-20 20:47:30 +05:30
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it('returns immediately if imageUrl is malformed', async () => {
await setValue(imageUrlSelector, 'not-a-url');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await submitForm();
2018-05-09 12:01:36 +05:30
2018-11-20 20:47:30 +05:30
expectInvalidInput(imageUrlSelector);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(mockedActions[submitAction]).not.toHaveBeenCalled();
2018-11-20 20:47:30 +05:30
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it('returns immediately if linkUrl is empty', async () => {
await setValue(linkUrlSelector, '');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await submitForm();
2018-05-09 12:01:36 +05:30
2018-11-20 20:47:30 +05:30
expectInvalidInput(linkUrlSelector);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(mockedActions[submitAction]).not.toHaveBeenCalled();
2018-11-20 20:47:30 +05:30
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it('returns immediately if linkUrl is malformed', async () => {
await setValue(linkUrlSelector, 'not-a-url');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await submitForm();
2018-05-09 12:01:36 +05:30
2018-11-20 20:47:30 +05:30
expectInvalidInput(linkUrlSelector);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(mockedActions[submitAction]).not.toHaveBeenCalled();
2018-11-20 20:47:30 +05:30
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it(`calls ${submitAction}`, async () => {
await submitForm();
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
expect(findImageUrl().element.checkValidity()).toBe(true);
expect(findLinkUrl().element.checkValidity()).toBe(true);
expect(mockedActions[submitAction]).toHaveBeenCalled();
2018-05-09 12:01:36 +05:30
});
2018-11-20 20:47:30 +05:30
};
2018-05-09 12:01:36 +05:30
describe('if isEditing is false', () => {
2022-11-25 23:54:43 +05:30
const props = { isEditing: false };
2018-05-09 12:01:36 +05:30
it('renders one button', () => {
2022-11-25 23:54:43 +05:30
createComponent(props);
expect(wrapper.find('.row-content-block').exists()).toBe(false);
const buttons = wrapper.findAll('.form-group:last-of-type button');
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(buttons).toHaveLength(1);
const buttonAddWrapper = buttons.at(0);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(buttonAddWrapper.isVisible()).toBe(true);
expect(buttonAddWrapper.text()).toBe('Add badge');
2018-05-09 12:01:36 +05:30
});
2018-11-20 20:47:30 +05:30
2022-11-25 23:54:43 +05:30
sharedSubmitTests('addBadge', props);
2018-05-09 12:01:36 +05:30
});
describe('if isEditing is true', () => {
2022-11-25 23:54:43 +05:30
const props = { isEditing: true };
2018-05-09 12:01:36 +05:30
it('renders two buttons', () => {
2022-11-25 23:54:43 +05:30
createComponent(props);
const buttons = wrapper.findAll('.row-content-block button');
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
expect(buttons).toHaveLength(2);
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
const saveButton = buttons.at(1);
expect(saveButton.isVisible()).toBe(true);
expect(saveButton.text()).toBe('Save changes');
2018-12-13 13:39:08 +05:30
2022-11-25 23:54:43 +05:30
const cancelButton = buttons.at(0);
expect(cancelButton.isVisible()).toBe(true);
expect(cancelButton.text()).toBe('Cancel');
2018-05-09 12:01:36 +05:30
});
2018-11-20 20:47:30 +05:30
2022-11-25 23:54:43 +05:30
sharedSubmitTests('saveBadge', props);
2018-05-09 12:01:36 +05:30
});
});