2020-03-13 15:44:24 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import DuplicateDashboardForm from '~/monitoring/components/duplicate_dashboard_form.vue';
|
|
|
|
|
|
|
|
import { dashboardGitResponse } from '../mock_data';
|
|
|
|
|
2020-05-30 21:06:31 +05:30
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
const createMountedWrapper = (props = {}) => {
|
|
|
|
// Use `mount` to render native input elements
|
|
|
|
wrapper = mount(DuplicateDashboardForm, {
|
|
|
|
propsData: { ...props },
|
|
|
|
sync: false,
|
2020-06-23 00:09:42 +05:30
|
|
|
// We need to attach to document, so that `document.activeElement` is properly set in jsdom
|
|
|
|
attachToDocument: true,
|
2020-05-30 21:06:31 +05:30
|
|
|
});
|
|
|
|
};
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-05-30 21:06:31 +05:30
|
|
|
describe('DuplicateDashboardForm', () => {
|
2020-03-13 15:44:24 +05:30
|
|
|
const defaultBranch = 'master';
|
|
|
|
|
|
|
|
const findByRef = ref => wrapper.find({ ref });
|
|
|
|
const setValue = (ref, val) => {
|
|
|
|
findByRef(ref).setValue(val);
|
|
|
|
};
|
|
|
|
const setChecked = value => {
|
|
|
|
const input = wrapper.find(`.form-check-input[value="${value}"]`);
|
|
|
|
input.element.checked = true;
|
|
|
|
input.trigger('click');
|
|
|
|
input.trigger('change');
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-05-30 21:06:31 +05:30
|
|
|
createMountedWrapper({ dashboard: dashboardGitResponse[0], defaultBranch });
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('renders correctly', () => {
|
|
|
|
expect(wrapper.exists()).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders form elements', () => {
|
|
|
|
expect(findByRef('fileName').exists()).toEqual(true);
|
|
|
|
expect(findByRef('branchName').exists()).toEqual(true);
|
|
|
|
expect(findByRef('branchOption').exists()).toEqual(true);
|
|
|
|
expect(findByRef('commitMessage').exists()).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('validates the file name', () => {
|
|
|
|
const findInvalidFeedback = () => findByRef('fileNameFormGroup').find('.invalid-feedback');
|
|
|
|
|
|
|
|
it('when is empty', () => {
|
|
|
|
setValue('fileName', '');
|
|
|
|
return wrapper.vm.$nextTick(() => {
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(findByRef('fileNameFormGroup').classes()).toContain('is-valid');
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(findInvalidFeedback().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when is valid', () => {
|
|
|
|
setValue('fileName', 'my_dashboard.yml');
|
|
|
|
return wrapper.vm.$nextTick(() => {
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(findByRef('fileNameFormGroup').classes()).toContain('is-valid');
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(findInvalidFeedback().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when is not valid', () => {
|
|
|
|
setValue('fileName', 'my_dashboard.exe');
|
|
|
|
return wrapper.vm.$nextTick(() => {
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(findByRef('fileNameFormGroup').classes()).toContain('is-invalid');
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(findInvalidFeedback().text()).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('emits `change` event', () => {
|
|
|
|
const lastChange = () =>
|
|
|
|
wrapper.vm.$nextTick().then(() => {
|
|
|
|
wrapper.find('form').trigger('change');
|
|
|
|
|
|
|
|
// Resolves to the last emitted change
|
|
|
|
const changes = wrapper.emitted().change;
|
|
|
|
return changes[changes.length - 1][0];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with the inital form values', () => {
|
|
|
|
expect(wrapper.emitted().change).toHaveLength(1);
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
return expect(lastChange()).resolves.toEqual({
|
2020-03-13 15:44:24 +05:30
|
|
|
branch: '',
|
|
|
|
commitMessage: expect.any(String),
|
|
|
|
dashboard: dashboardGitResponse[0].path,
|
|
|
|
fileName: 'common_metrics.yml',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('containing an inputted file name', () => {
|
|
|
|
setValue('fileName', 'my_dashboard.yml');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return expect(lastChange()).resolves.toMatchObject({
|
2020-03-13 15:44:24 +05:30
|
|
|
fileName: 'my_dashboard.yml',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('containing a default commit message when no message is set', () => {
|
|
|
|
setValue('commitMessage', '');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return expect(lastChange()).resolves.toMatchObject({
|
2020-03-13 15:44:24 +05:30
|
|
|
commitMessage: expect.stringContaining('Create custom dashboard'),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('containing an inputted commit message', () => {
|
|
|
|
setValue('commitMessage', 'My commit message');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return expect(lastChange()).resolves.toMatchObject({
|
2020-03-13 15:44:24 +05:30
|
|
|
commitMessage: expect.stringContaining('My commit message'),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('containing an inputted branch name', () => {
|
|
|
|
setValue('branchName', 'a-new-branch');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return expect(lastChange()).resolves.toMatchObject({
|
2020-03-13 15:44:24 +05:30
|
|
|
branch: 'a-new-branch',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when a `default` branch option is set, branch input is invisible and ignored', () => {
|
|
|
|
setChecked(wrapper.vm.$options.radioVals.DEFAULT);
|
|
|
|
setValue('branchName', 'a-new-branch');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
return Promise.all([
|
|
|
|
expect(lastChange()).resolves.toMatchObject({
|
|
|
|
branch: defaultBranch,
|
|
|
|
}),
|
|
|
|
wrapper.vm.$nextTick(() => {
|
|
|
|
expect(findByRef('branchName').isVisible()).toBe(false);
|
|
|
|
}),
|
|
|
|
]);
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('when `new` branch option is chosen, focuses on the branch name input', () => {
|
|
|
|
setChecked(wrapper.vm.$options.radioVals.NEW);
|
|
|
|
|
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
|
|
|
wrapper.find('form').trigger('change');
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(document.activeElement).toBe(findByRef('branchName').element);
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-30 21:06:31 +05:30
|
|
|
|
|
|
|
describe('DuplicateDashboardForm escapes elements', () => {
|
|
|
|
const branchToEscape = "<img/src='x'onerror=alert(document.domain)>";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
createMountedWrapper({ dashboard: dashboardGitResponse[0], defaultBranch: branchToEscape });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should escape branch name data', () => {
|
|
|
|
const branchOptionHtml = wrapper.vm.branchOptions[0].html;
|
|
|
|
const escapedBranch = '<img/src='x'onerror=alert(document.domain)>';
|
|
|
|
|
|
|
|
expect(branchOptionHtml).toEqual(expect.stringContaining(escapedBranch));
|
|
|
|
});
|
|
|
|
});
|