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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import WorkItemTree from '~/work_items/components/work_item_links/work_item_tree.vue';
import WorkItemLinksForm from '~/work_items/components/work_item_links/work_item_links_form.vue';
import WorkItemLinkChild from '~/work_items/components/work_item_links/work_item_link_child.vue';
import OkrActionsSplitButton from '~/work_items/components/work_item_links/okr_actions_split_button.vue';
import workItemQuery from '~/work_items/graphql/work_item.query.graphql';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import {
FORM_TYPES,
WORK_ITEM_TYPE_ENUM_OBJECTIVE,
WORK_ITEM_TYPE_ENUM_KEY_RESULT,
} from '~/work_items/constants';
import { childrenWorkItems, workItemObjectiveWithChild } from '../../mock_data';
describe('WorkItemTree', () => {
let getWorkItemQueryHandler;
let wrapper;
const findEmptyState = () => wrapper.findByTestId('tree-empty');
const findToggleFormSplitButton = () => wrapper.findComponent(OkrActionsSplitButton);
const findForm = () => wrapper.findComponent(WorkItemLinksForm);
const findWorkItemLinkChildItems = () => wrapper.findAllComponents(WorkItemLinkChild);
Vue.use(VueApollo);
const createComponent = ({
workItemType = 'Objective',
parentWorkItemType = 'Objective',
confidential = false,
children = childrenWorkItems,
apolloProvider = null,
} = {}) => {
const mockWorkItemResponse = {
data: {
workItem: {
...workItemObjectiveWithChild,
workItemType: {
...workItemObjectiveWithChild.workItemType,
name: workItemType,
},
},
},
};
getWorkItemQueryHandler = jest.fn().mockResolvedValue(mockWorkItemResponse);
wrapper = shallowMountExtended(WorkItemTree, {
apolloProvider:
apolloProvider || createMockApollo([[workItemQuery, getWorkItemQueryHandler]]),
propsData: {
workItemType,
parentWorkItemType,
workItemId: 'gid://gitlab/WorkItem/515',
confidential,
children,
projectPath: 'test/project',
},
});
wrapper.vm.$refs.wrapper.show = jest.fn();
};
it('displays Add button', () => {
createComponent();
expect(findToggleFormSplitButton().exists()).toBe(true);
});
it('displays empty state if there are no children', () => {
createComponent({ children: [] });
expect(findEmptyState().exists()).toBe(true);
});
it('renders all hierarchy widget children', () => {
createComponent();
const workItemLinkChildren = findWorkItemLinkChildItems();
expect(workItemLinkChildren).toHaveLength(4);
expect(workItemLinkChildren.at(0).props().childItem.confidential).toBe(
childrenWorkItems[0].confidential,
);
});
it('does not display form by default', () => {
createComponent();
expect(findForm().exists()).toBe(false);
});
it.each`
option | event | formType | childType
${'New objective'} | ${'showCreateObjectiveForm'} | ${FORM_TYPES.create} | ${WORK_ITEM_TYPE_ENUM_OBJECTIVE}
${'Existing objective'} | ${'showAddObjectiveForm'} | ${FORM_TYPES.add} | ${WORK_ITEM_TYPE_ENUM_OBJECTIVE}
${'New key result'} | ${'showCreateKeyResultForm'} | ${FORM_TYPES.create} | ${WORK_ITEM_TYPE_ENUM_KEY_RESULT}
${'Existing key result'} | ${'showAddKeyResultForm'} | ${FORM_TYPES.add} | ${WORK_ITEM_TYPE_ENUM_KEY_RESULT}
`(
'when selecting $option from split button, renders the form passing $formType and $childType',
async ({ event, formType, childType }) => {
createComponent();
findToggleFormSplitButton().vm.$emit(event);
await nextTick();
expect(findForm().exists()).toBe(true);
expect(findForm().props()).toMatchObject({
formType,
childrenType: childType,
parentWorkItemType: 'Objective',
parentConfidential: false,
});
},
);
it('remove event on child triggers `removeChild` event', () => {
createComponent();
const firstChild = findWorkItemLinkChildItems().at(0);
firstChild.vm.$emit('removeChild', 'gid://gitlab/WorkItem/2');
expect(wrapper.emitted('removeChild')).toEqual([['gid://gitlab/WorkItem/2']]);
});
it('emits `show-modal` on `click` event', () => {
createComponent();
const firstChild = findWorkItemLinkChildItems().at(0);
const event = {
childItem: 'gid://gitlab/WorkItem/2',
};
firstChild.vm.$emit('click', event);
expect(wrapper.emitted('show-modal')).toEqual([[event, event.childItem]]);
});
it.each`
description | workItemType | prefetch
${'prefetches'} | ${'Issue'} | ${true}
${'does not prefetch'} | ${'Objective'} | ${false}
`(
'$description work-item-link-child on mouseover when workItemType is "$workItemType"',
async ({ workItemType, prefetch }) => {
createComponent({ workItemType });
const firstChild = findWorkItemLinkChildItems().at(0);
firstChild.vm.$emit('mouseover', childrenWorkItems[0]);
await nextTick();
await waitForPromises();
jest.advanceTimersByTime(DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
if (prefetch) {
expect(getWorkItemQueryHandler).toHaveBeenCalled();
} else {
expect(getWorkItemQueryHandler).not.toHaveBeenCalled();
}
},
);
});
|