gitlab-org--gitlab-foss/spec/frontend/badges/components/badge_form_spec.js

198 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-04-08 06:20:05 -04:00
import Vue from 'vue';
import MockAdapter from 'axios-mock-adapter';
import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
import axios from '~/lib/utils/axios_utils';
2018-04-08 06:20:05 -04:00
import store from '~/badges/store';
import createEmptyBadge from '~/badges/empty_badge';
2018-04-08 06:20:05 -04:00
import BadgeForm from '~/badges/components/badge_form.vue';
import { DUMMY_IMAGE_URL, TEST_HOST } from 'helpers/test_constants';
// avoid preview background process
BadgeForm.methods.debouncedPreview = () => {};
2018-04-08 06:20:05 -04:00
describe('BadgeForm component', () => {
const Component = Vue.extend(BadgeForm);
let axiosMock;
2018-04-08 06:20:05 -04:00
let vm;
beforeEach(() => {
setFixtures(`
<div id="dummy-element"></div>
`);
axiosMock = new MockAdapter(axios);
2018-04-08 06:20:05 -04:00
});
afterEach(() => {
vm.$destroy();
axiosMock.restore();
2018-04-08 06:20:05 -04:00
});
describe('methods', () => {
beforeEach(() => {
vm = mountComponentWithStore(Component, {
el: '#dummy-element',
store,
props: {
isEditing: false,
},
});
});
describe('onCancel', () => {
it('calls stopEditing', () => {
jest.spyOn(vm, 'stopEditing').mockImplementation(() => {});
2018-04-08 06:20:05 -04:00
vm.onCancel();
expect(vm.stopEditing).toHaveBeenCalled();
});
});
});
2018-04-08 06:20:05 -04:00
const sharedSubmitTests = submitAction => {
const nameSelector = '#badge-name';
const imageUrlSelector = '#badge-image-url';
const findImageUrlElement = () => vm.$el.querySelector(imageUrlSelector);
const linkUrlSelector = '#badge-link-url';
const findLinkUrlElement = () => vm.$el.querySelector(linkUrlSelector);
const setValue = (inputElementSelector, value) => {
const inputElement = vm.$el.querySelector(inputElementSelector);
inputElement.value = value;
inputElement.dispatchEvent(new Event('input'));
};
const submitForm = () => {
const submitButton = vm.$el.querySelector('button[type="submit"]');
submitButton.click();
};
const expectInvalidInput = inputElementSelector => {
const inputElement = vm.$el.querySelector(inputElementSelector);
2018-10-09 14:03:09 -04:00
expect(inputElement.checkValidity()).toBe(false);
const feedbackElement = vm.$el.querySelector(`${inputElementSelector} + .invalid-feedback`);
2018-10-09 14:03:09 -04:00
expect(feedbackElement).toBeVisible();
};
2018-04-08 06:20:05 -04:00
beforeEach(done => {
jest.spyOn(vm, submitAction).mockReturnValue(Promise.resolve());
store.replaceState({
...store.state,
badgeInAddForm: createEmptyBadge(),
badgeInEditForm: createEmptyBadge(),
isSaving: false,
});
2018-04-08 06:20:05 -04:00
Vue.nextTick()
.then(() => {
setValue(nameSelector, 'TestBadge');
setValue(linkUrlSelector, `${TEST_HOST}/link/url`);
setValue(imageUrlSelector, `${window.location.origin}${DUMMY_IMAGE_URL}`);
})
.then(done)
.catch(done.fail);
});
2018-04-08 06:20:05 -04:00
it('returns immediately if imageUrl is empty', () => {
setValue(imageUrlSelector, '');
2018-04-08 06:20:05 -04:00
submitForm();
2018-04-08 06:20:05 -04:00
expectInvalidInput(imageUrlSelector);
2018-10-09 14:03:09 -04:00
expect(vm[submitAction]).not.toHaveBeenCalled();
});
2018-04-08 06:20:05 -04:00
it('returns immediately if imageUrl is malformed', () => {
setValue(imageUrlSelector, 'not-a-url');
2018-04-08 06:20:05 -04:00
submitForm();
2018-04-08 06:20:05 -04:00
expectInvalidInput(imageUrlSelector);
2018-10-09 14:03:09 -04:00
expect(vm[submitAction]).not.toHaveBeenCalled();
});
2018-04-08 06:20:05 -04:00
it('returns immediately if linkUrl is empty', () => {
setValue(linkUrlSelector, '');
2018-04-08 06:20:05 -04:00
submitForm();
2018-04-08 06:20:05 -04:00
expectInvalidInput(linkUrlSelector);
2018-10-09 14:03:09 -04:00
expect(vm[submitAction]).not.toHaveBeenCalled();
});
2018-04-08 06:20:05 -04:00
it('returns immediately if linkUrl is malformed', () => {
setValue(linkUrlSelector, 'not-a-url');
2018-04-08 06:20:05 -04:00
submitForm();
2018-04-08 06:20:05 -04:00
expectInvalidInput(linkUrlSelector);
2018-10-09 14:03:09 -04:00
expect(vm[submitAction]).not.toHaveBeenCalled();
});
2018-04-08 06:20:05 -04:00
it(`calls ${submitAction}`, () => {
submitForm();
2018-04-08 06:20:05 -04:00
expect(findImageUrlElement().checkValidity()).toBe(true);
expect(findLinkUrlElement().checkValidity()).toBe(true);
expect(vm[submitAction]).toHaveBeenCalled();
2018-04-08 06:20:05 -04:00
});
};
2018-04-08 06:20:05 -04:00
describe('if isEditing is false', () => {
beforeEach(() => {
vm = mountComponentWithStore(Component, {
el: '#dummy-element',
store,
props: {
isEditing: false,
},
});
});
it('renders one button', () => {
expect(vm.$el.querySelector('.row-content-block')).toBeNull();
const buttons = vm.$el.querySelectorAll('.form-group:last-of-type button');
2018-10-09 14:03:09 -04:00
2018-04-08 06:20:05 -04:00
expect(buttons.length).toBe(1);
const buttonAddElement = buttons[0];
2018-10-09 14:03:09 -04:00
2018-04-08 06:20:05 -04:00
expect(buttonAddElement).toBeVisible();
expect(buttonAddElement).toHaveText('Add badge');
});
sharedSubmitTests('addBadge');
2018-04-08 06:20:05 -04:00
});
describe('if isEditing is true', () => {
beforeEach(() => {
vm = mountComponentWithStore(Component, {
el: '#dummy-element',
store,
props: {
isEditing: true,
},
});
});
it('renders two buttons', () => {
const buttons = vm.$el.querySelectorAll('.row-content-block button');
2018-10-09 14:03:09 -04:00
2018-04-08 06:20:05 -04:00
expect(buttons.length).toBe(2);
const buttonSaveElement = buttons[0];
2018-10-09 14:03:09 -04:00
2018-04-08 06:20:05 -04:00
expect(buttonSaveElement).toBeVisible();
expect(buttonSaveElement).toHaveText('Save changes');
const buttonCancelElement = buttons[1];
2018-10-09 14:03:09 -04:00
2018-04-08 06:20:05 -04:00
expect(buttonCancelElement).toBeVisible();
expect(buttonCancelElement).toHaveText('Cancel');
});
sharedSubmitTests('saveBadge');
2018-04-08 06:20:05 -04:00
});
});