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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
import { GlIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { mockTracking, triggerEvent } from 'helpers/tracking_helper';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import createStore from '~/notes/stores';
import EditForm from '~/sidebar/components/lock/edit_form.vue';
import IssuableLockForm from '~/sidebar/components/lock/issuable_lock_form.vue';
import toast from '~/vue_shared/plugins/global_toast';
import waitForPromises from 'helpers/wait_for_promises';
import { ISSUABLE_TYPE_ISSUE, ISSUABLE_TYPE_MR } from './constants';
jest.mock('~/vue_shared/plugins/global_toast');
Vue.use(Vuex);
describe('IssuableLockForm', () => {
let wrapper;
let store;
let issuableType; // Either ISSUABLE_TYPE_ISSUE or ISSUABLE_TYPE_MR
let updateLockedAttribute;
const setIssuableType = (pageType) => {
issuableType = pageType;
};
const findSidebarCollapseIcon = () => wrapper.find('[data-testid="sidebar-collapse-icon"]');
const findLockStatus = () => wrapper.find('[data-testid="lock-status"]');
const findEditLink = () => wrapper.find('[data-testid="edit-link"]');
const findEditForm = () => wrapper.findComponent(EditForm);
const findLockButton = () => wrapper.find('[data-testid="issuable-lock"]');
const findSidebarLockStatusTooltip = () =>
getBinding(findSidebarCollapseIcon().element, 'gl-tooltip');
const findIssuableLockClickable = () => wrapper.find('[data-testid="issuable-lock"]');
const initStore = (isLocked) => {
if (issuableType === ISSUABLE_TYPE_ISSUE) {
store = createStore();
store.getters.getNoteableData.targetType = 'issue';
} else {
updateLockedAttribute = jest.fn().mockResolvedValue();
store = new Vuex.Store({
getters: {
getNoteableData: () => ({ targetType: issuableType }),
},
actions: {
updateLockedAttribute,
},
});
}
store.getters.getNoteableData.discussion_locked = isLocked;
};
const createComponent = ({ props = {}, movedMrSidebar = false }) => {
wrapper = shallowMount(IssuableLockForm, {
store,
provide: {
fullPath: '',
glFeatures: {
movedMrSidebar,
},
},
propsData: {
isEditable: true,
...props,
},
directives: {
GlTooltip: createMockDirective('gl-tooltip'),
},
});
};
describe.each`
pageType
${ISSUABLE_TYPE_ISSUE} | ${ISSUABLE_TYPE_MR}
`('In $pageType page', ({ pageType }) => {
beforeEach(() => {
setIssuableType(pageType);
});
describe.each`
isLocked
${false} | ${true}
`(`renders for isLocked = $isLocked`, ({ isLocked }) => {
beforeEach(() => {
initStore(isLocked);
createComponent({});
});
it('shows the lock status', () => {
expect(findLockStatus().text()).toBe(isLocked ? 'Locked' : 'Unlocked');
});
describe('edit form', () => {
let isEditable;
beforeEach(() => {
isEditable = false;
createComponent({ props: { isEditable } });
});
describe('when not editable', () => {
it('does not display the edit form when opened if not editable', async () => {
expect(findEditForm().exists()).toBe(false);
findSidebarCollapseIcon().trigger('click');
await nextTick();
expect(findEditForm().exists()).toBe(false);
});
});
describe('when editable', () => {
beforeEach(() => {
isEditable = true;
createComponent({ props: { isEditable } });
});
it('shows the editable status', () => {
expect(findEditLink().exists()).toBe(isEditable);
expect(findEditLink().text()).toBe('Edit');
});
describe("when 'Edit' is clicked", () => {
it('displays the edit form when editable', async () => {
expect(findEditForm().exists()).toBe(false);
findEditLink().trigger('click');
await nextTick();
expect(findEditForm().exists()).toBe(true);
});
it('tracks the event', () => {
const spy = mockTracking('_category_', wrapper.element, jest.spyOn);
triggerEvent(findEditLink().element);
expect(spy).toHaveBeenCalledWith('_category_', 'click_edit_button', {
label: 'right_sidebar',
property: 'lock_issue',
});
});
});
describe('When sidebar is collapsed', () => {
it('displays the edit form when opened', async () => {
expect(findEditForm().exists()).toBe(false);
findSidebarCollapseIcon().trigger('click');
await nextTick();
expect(findEditForm().exists()).toBe(true);
});
it('renders a tooltip with the lock status text', () => {
const tooltip = findSidebarLockStatusTooltip();
expect(tooltip).toBeDefined();
expect(tooltip.value.title).toBe(isLocked ? 'Locked' : 'Unlocked');
});
it('renders lock icon', () => {
const icon = findSidebarCollapseIcon().findComponent(GlIcon).props('name');
const expected = isLocked ? 'lock' : 'lock-open';
expect(icon).toBe(expected);
});
});
});
});
});
});
describe('merge requests', () => {
beforeEach(() => {
setIssuableType('merge_request');
});
it.each`
locked | message
${true} | ${'Merge request locked.'}
${false} | ${'Merge request unlocked.'}
`('displays $message when merge request is $locked', async ({ locked, message }) => {
initStore(locked);
createComponent({ movedMrSidebar: true });
await findLockButton().trigger('click');
await waitForPromises();
expect(toast).toHaveBeenCalledWith(message);
});
});
describe('moved_mr_sidebar flag', () => {
describe('when the flag is off', () => {
it('does not show the non editable lock status', () => {
createComponent({ movedMrSidebar: false });
expect(findIssuableLockClickable().exists()).toBe(false);
});
});
describe('when the flag is on', () => {
it('shows the non editable lock status', () => {
createComponent({ movedMrSidebar: true });
expect(findIssuableLockClickable().exists()).toBe(true);
});
});
});
});
|