debian-mirror-gitlab/spec/frontend/sidebar/confidential_issue_sidebar_spec.js

194 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-12-21 20:55:43 +05:30
import { shallowMount } from '@vue/test-utils';
import { mockTracking, triggerEvent } from 'helpers/tracking_helper';
2020-01-01 13:55:28 +05:30
import ConfidentialIssueSidebar from '~/sidebar/components/confidential/confidential_issue_sidebar.vue';
2019-12-21 20:55:43 +05:30
import EditForm from '~/sidebar/components/confidential/edit_form.vue';
import SidebarService from '~/sidebar/services/sidebar_service';
import createFlash from '~/flash';
2020-03-13 15:44:24 +05:30
import RecaptchaModal from '~/vue_shared/components/recaptcha_modal.vue';
2020-05-24 23:13:21 +05:30
import createStore from '~/notes/stores';
2020-06-23 00:09:42 +05:30
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
2020-07-28 23:09:34 +05:30
import eventHub from '~/sidebar/event_hub';
2019-12-21 20:55:43 +05:30
jest.mock('~/flash');
jest.mock('~/sidebar/services/sidebar_service');
describe('Confidential Issue Sidebar Block', () => {
2020-06-23 00:09:42 +05:30
useMockLocationHelper();
2019-12-21 20:55:43 +05:30
let wrapper;
2020-07-28 23:09:34 +05:30
const mutate = jest
.fn()
.mockResolvedValue({ data: { issueSetConfidential: { issue: { confidential: true } } } });
2019-12-21 20:55:43 +05:30
const findRecaptchaModal = () => wrapper.find(RecaptchaModal);
const triggerUpdateConfidentialAttribute = () => {
wrapper.setData({ edit: true });
return (
// wait for edit form to become visible
wrapper.vm
.$nextTick()
.then(() => {
2020-07-28 23:09:34 +05:30
eventHub.$emit('updateConfidentialAttribute');
2019-12-21 20:55:43 +05:30
})
// wait for reCAPTCHA modal to render
.then(() => wrapper.vm.$nextTick())
);
};
2020-07-28 23:09:34 +05:30
const createComponent = ({ propsData, data = {} }) => {
2020-05-24 23:13:21 +05:30
const store = createStore();
2019-12-21 20:55:43 +05:30
const service = new SidebarService();
wrapper = shallowMount(ConfidentialIssueSidebar, {
2020-05-24 23:13:21 +05:30
store,
2020-07-28 23:09:34 +05:30
data() {
return data;
},
2019-12-21 20:55:43 +05:30
propsData: {
service,
2020-07-28 23:09:34 +05:30
iid: '',
fullPath: '',
2019-12-21 20:55:43 +05:30
...propsData,
},
2020-07-28 23:09:34 +05:30
mocks: {
$apollo: {
mutate,
},
},
2019-12-21 20:55:43 +05:30
});
};
afterEach(() => {
wrapper.destroy();
});
it.each`
2020-05-24 23:13:21 +05:30
confidential | isEditable
${false} | ${false}
${false} | ${true}
${true} | ${false}
${true} | ${true}
2019-12-21 20:55:43 +05:30
`(
2020-05-24 23:13:21 +05:30
'renders for confidential = $confidential and isEditable = $isEditable',
({ confidential, isEditable }) => {
2019-12-21 20:55:43 +05:30
createComponent({
2020-07-28 23:09:34 +05:30
propsData: {
isEditable,
},
2019-12-21 20:55:43 +05:30
});
2020-05-24 23:13:21 +05:30
wrapper.vm.$store.state.noteableData.confidential = confidential;
2019-12-21 20:55:43 +05:30
2020-05-24 23:13:21 +05:30
return wrapper.vm.$nextTick().then(() => {
expect(wrapper.element).toMatchSnapshot();
});
2019-12-21 20:55:43 +05:30
},
);
describe('if editable', () => {
beforeEach(() => {
createComponent({
2020-07-28 23:09:34 +05:30
propsData: {
isEditable: true,
},
2019-12-21 20:55:43 +05:30
});
2020-05-24 23:13:21 +05:30
wrapper.vm.$store.state.noteableData.confidential = true;
2019-12-21 20:55:43 +05:30
});
it('displays the edit form when editable', () => {
wrapper.setData({ edit: false });
2020-03-13 15:44:24 +05:30
return wrapper.vm
.$nextTick()
.then(() => {
wrapper.find({ ref: 'editLink' }).trigger('click');
return wrapper.vm.$nextTick();
})
.then(() => {
expect(wrapper.find(EditForm).exists()).toBe(true);
});
2019-12-21 20:55:43 +05:30
});
it('displays the edit form when opened from collapsed state', () => {
wrapper.setData({ edit: false });
2020-03-13 15:44:24 +05:30
return wrapper.vm
.$nextTick()
.then(() => {
wrapper.find({ ref: 'collapseIcon' }).trigger('click');
return wrapper.vm.$nextTick();
})
.then(() => {
expect(wrapper.find(EditForm).exists()).toBe(true);
});
2019-12-21 20:55:43 +05:30
});
it('tracks the event when "Edit" is clicked', () => {
const spy = mockTracking('_category_', wrapper.element, jest.spyOn);
const editLink = wrapper.find({ ref: 'editLink' });
triggerEvent(editLink.element);
expect(spy).toHaveBeenCalledWith('_category_', 'click_edit_button', {
label: 'right_sidebar',
property: 'confidentiality',
});
});
describe('for successful update', () => {
beforeEach(() => {
SidebarService.prototype.update.mockResolvedValue({ data: 'irrelevant' });
});
it('reloads the page', () =>
triggerUpdateConfidentialAttribute().then(() => {
expect(window.location.reload).toHaveBeenCalled();
}));
it('does not show an error message', () =>
triggerUpdateConfidentialAttribute().then(() => {
expect(createFlash).not.toHaveBeenCalled();
}));
});
describe('for update error', () => {
beforeEach(() => {
SidebarService.prototype.update.mockRejectedValue(new Error('updating failed!'));
});
it('does not reload the page', () =>
triggerUpdateConfidentialAttribute().then(() => {
expect(window.location.reload).not.toHaveBeenCalled();
}));
it('shows an error message', () =>
triggerUpdateConfidentialAttribute().then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('for spam error', () => {
beforeEach(() => {
SidebarService.prototype.update.mockRejectedValue({ name: 'SpamError' });
});
it('does not reload the page', () =>
triggerUpdateConfidentialAttribute().then(() => {
expect(window.location.reload).not.toHaveBeenCalled();
}));
it('does not show an error message', () =>
triggerUpdateConfidentialAttribute().then(() => {
expect(createFlash).not.toHaveBeenCalled();
}));
it('shows a reCAPTCHA modal', () => {
expect(findRecaptchaModal().exists()).toBe(false);
return triggerUpdateConfidentialAttribute().then(() => {
expect(findRecaptchaModal().exists()).toBe(true);
});
});
});
});
});