1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
import { GlAlert } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import { visitUrl } from '~/lib/utils/url_utility';
import WorkItemDetail from '~/work_items/components/work_item_detail.vue';
import WorkItemsRoot from '~/work_items/pages/work_item_root.vue';
import deleteWorkItem from '~/work_items/graphql/delete_work_item.mutation.graphql';
import { deleteWorkItemResponse, deleteWorkItemFailureResponse } from '../mock_data';
jest.mock('~/lib/utils/url_utility', () => ({
...jest.requireActual('~/lib/utils/url_utility'),
visitUrl: jest.fn(),
}));
Vue.use(VueApollo);
describe('Work items root component', () => {
let wrapper;
const issuesListPath = '/-/issues';
const mockToastShow = jest.fn();
const findWorkItemDetail = () => wrapper.findComponent(WorkItemDetail);
const findAlert = () => wrapper.findComponent(GlAlert);
const createComponent = ({
deleteWorkItemHandler = jest.fn().mockResolvedValue(deleteWorkItemResponse),
} = {}) => {
wrapper = shallowMount(WorkItemsRoot, {
apolloProvider: createMockApollo([[deleteWorkItem, deleteWorkItemHandler]]),
provide: {
issuesListPath,
},
propsData: {
id: '1',
},
mocks: {
$toast: {
show: mockToastShow,
},
},
});
};
it('renders WorkItemDetail', () => {
createComponent();
expect(findWorkItemDetail().props()).toEqual({
isModal: false,
workItemId: 'gid://gitlab/WorkItem/1',
workItemParentId: null,
workItemIid: '1',
});
});
it('deletes work item when deleteWorkItem event emitted', async () => {
const deleteWorkItemHandler = jest.fn().mockResolvedValue(deleteWorkItemResponse);
createComponent({
deleteWorkItemHandler,
});
findWorkItemDetail().vm.$emit('deleteWorkItem', { workItemType: 'task', workItemId: '1' });
await waitForPromises();
expect(deleteWorkItemHandler).toHaveBeenCalledWith({
input: {
id: '1',
},
});
expect(mockToastShow).toHaveBeenCalled();
expect(visitUrl).toHaveBeenCalledWith(issuesListPath);
});
it('shows alert if delete fails', async () => {
const deleteWorkItemHandler = jest.fn().mockRejectedValue(deleteWorkItemFailureResponse);
createComponent({
deleteWorkItemHandler,
});
findWorkItemDetail().vm.$emit('deleteWorkItem', { workItemType: 'task', workItemId: '1' });
await waitForPromises();
expect(findAlert().exists()).toBe(true);
});
});
|