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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
import { GlIntersectionObserver } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { useMockIntersectionObserver } from 'helpers/mock_dom_observer';
import axios from '~/lib/utils/axios_utils';
import { visitUrl } from '~/lib/utils/url_utility';
import '~/behaviors/markdown/render_gfm';
import IssuableApp from '~/issue_show/components/app.vue';
import eventHub from '~/issue_show/event_hub';
import {
appProps,
initialRequest,
publishedIncidentUrl,
secondRequest,
zoomMeetingUrl,
} from '../mock_data';
import IncidentTabs from '~/issue_show/components/incident_tabs.vue';
import DescriptionComponent from '~/issue_show/components/description.vue';
import PinnedLinks from '~/issue_show/components/pinned_links.vue';
function formatText(text) {
return text.trim().replace(/\s\s+/g, ' ');
}
jest.mock('~/lib/utils/url_utility');
jest.mock('~/issue_show/event_hub');
const REALTIME_REQUEST_STACK = [initialRequest, secondRequest];
describe('Issuable output', () => {
useMockIntersectionObserver();
let mock;
let realtimeRequestCount = 0;
let wrapper;
const findStickyHeader = () => wrapper.find('[data-testid="issue-sticky-header"]');
const mountComponent = (props = {}) => {
wrapper = mount(IssuableApp, {
propsData: { ...appProps, ...props },
});
};
beforeEach(() => {
setFixtures(`
<div>
<title>Title</title>
<div class="detail-page-description content-block">
<details open>
<summary>One</summary>
</details>
<details>
<summary>Two</summary>
</details>
</div>
<div class="flash-container"></div>
<span id="task_status"></span>
</div>
`);
mock = new MockAdapter(axios);
mock
.onGet('/gitlab-org/gitlab-shell/-/issues/9/realtime_changes/realtime_changes')
.reply(() => {
const res = Promise.resolve([200, REALTIME_REQUEST_STACK[realtimeRequestCount]]);
realtimeRequestCount += 1;
return res;
});
mountComponent();
});
afterEach(() => {
mock.restore();
realtimeRequestCount = 0;
wrapper.vm.poll.stop();
wrapper.destroy();
wrapper = null;
});
it('should render a title/description/edited and update title/description/edited on update', () => {
let editedText;
return axios
.waitForAll()
.then(() => {
editedText = wrapper.find('.edited-text');
})
.then(() => {
expect(document.querySelector('title').innerText).toContain('this is a title (#1)');
expect(wrapper.find('.title').text()).toContain('this is a title');
expect(wrapper.find('.md').text()).toContain('this is a description!');
expect(wrapper.find('.js-task-list-field').element.value).toContain(
'this is a description',
);
expect(formatText(editedText.text())).toMatch(/Edited[\s\S]+?by Some User/);
expect(editedText.find('.author-link').attributes('href')).toMatch(/\/some_user$/);
expect(editedText.find('time').text()).toBeTruthy();
expect(wrapper.vm.state.lock_version).toEqual(1);
})
.then(() => {
wrapper.vm.poll.makeRequest();
return axios.waitForAll();
})
.then(() => {
expect(document.querySelector('title').innerText).toContain('2 (#1)');
expect(wrapper.find('.title').text()).toContain('2');
expect(wrapper.find('.md').text()).toContain('42');
expect(wrapper.find('.js-task-list-field').element.value).toContain('42');
expect(wrapper.find('.edited-text').text()).toBeTruthy();
expect(formatText(wrapper.find('.edited-text').text())).toMatch(
/Edited[\s\S]+?by Other User/,
);
expect(editedText.find('.author-link').attributes('href')).toMatch(/\/other_user$/);
expect(editedText.find('time').text()).toBeTruthy();
expect(wrapper.vm.state.lock_version).toEqual(2);
});
});
it('shows actions if permissions are correct', () => {
wrapper.vm.showForm = true;
return wrapper.vm.$nextTick().then(() => {
expect(wrapper.find('.markdown-selector').exists()).toBe(true);
});
});
it('does not show actions if permissions are incorrect', () => {
wrapper.vm.showForm = true;
wrapper.setProps({ canUpdate: false });
return wrapper.vm.$nextTick().then(() => {
expect(wrapper.find('.markdown-selector').exists()).toBe(false);
});
});
it('does not update formState if form is already open', () => {
wrapper.vm.updateAndShowForm();
wrapper.vm.state.titleText = 'testing 123';
wrapper.vm.updateAndShowForm();
return wrapper.vm.$nextTick().then(() => {
expect(wrapper.vm.store.formState.title).not.toBe('testing 123');
});
});
it('opens reCAPTCHA modal if update rejected as spam', () => {
let modal;
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockResolvedValue({
data: {
recaptcha_html: '<div class="g-recaptcha">recaptcha_html</div>',
},
});
wrapper.vm.canUpdate = true;
wrapper.vm.showForm = true;
return wrapper.vm
.$nextTick()
.then(() => {
wrapper.vm.$refs.recaptchaModal.scriptSrc = '//scriptsrc';
return wrapper.vm.updateIssuable();
})
.then(() => {
modal = wrapper.find('.js-recaptcha-modal');
expect(modal.isVisible()).toBe(true);
expect(modal.find('.g-recaptcha').text()).toEqual('recaptcha_html');
expect(document.body.querySelector('.js-recaptcha-script').src).toMatch('//scriptsrc');
})
.then(() => {
modal.find('.close').trigger('click');
return wrapper.vm.$nextTick();
})
.then(() => {
expect(modal.isVisible()).toBe(false);
expect(document.body.querySelector('.js-recaptcha-script')).toBeNull();
});
});
describe('Pinned links propagated', () => {
it.each`
prop | value
${'zoomMeetingUrl'} | ${zoomMeetingUrl}
${'publishedIncidentUrl'} | ${publishedIncidentUrl}
`('sets the $prop correctly on underlying pinned links', ({ prop, value }) => {
expect(wrapper.vm[prop]).toEqual(value);
expect(wrapper.find(`[data-testid="${prop}"]`).attributes('href')).toBe(value);
});
});
describe('updateIssuable', () => {
it('fetches new data after update', () => {
const updateStoreSpy = jest.spyOn(wrapper.vm, 'updateStoreState');
const getDataSpy = jest.spyOn(wrapper.vm.service, 'getData');
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockResolvedValue({
data: { web_url: window.location.pathname },
});
return wrapper.vm.updateIssuable().then(() => {
expect(updateStoreSpy).toHaveBeenCalled();
expect(getDataSpy).toHaveBeenCalled();
});
});
it('correctly updates issuable data', () => {
const spy = jest.spyOn(wrapper.vm.service, 'updateIssuable').mockResolvedValue({
data: { web_url: window.location.pathname },
});
return wrapper.vm.updateIssuable().then(() => {
expect(spy).toHaveBeenCalledWith(wrapper.vm.formState);
expect(eventHub.$emit).toHaveBeenCalledWith('close.form');
});
});
it('does not redirect if issue has not moved', () => {
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockResolvedValue({
data: {
web_url: window.location.pathname,
confidential: wrapper.vm.isConfidential,
},
});
return wrapper.vm.updateIssuable().then(() => {
expect(visitUrl).not.toHaveBeenCalled();
});
});
it('does not redirect if issue has not moved and user has switched tabs', () => {
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockResolvedValue({
data: {
web_url: '',
confidential: wrapper.vm.isConfidential,
},
});
return wrapper.vm.updateIssuable().then(() => {
expect(visitUrl).not.toHaveBeenCalled();
});
});
it('redirects if returned web_url has changed', () => {
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockResolvedValue({
data: {
web_url: '/testing-issue-move',
confidential: wrapper.vm.isConfidential,
},
});
wrapper.vm.updateIssuable();
return wrapper.vm.updateIssuable().then(() => {
expect(visitUrl).toHaveBeenCalledWith('/testing-issue-move');
});
});
describe('shows dialog when issue has unsaved changed', () => {
it('confirms on title change', () => {
wrapper.vm.showForm = true;
wrapper.vm.state.titleText = 'title has changed';
const e = { returnValue: null };
wrapper.vm.handleBeforeUnloadEvent(e);
return wrapper.vm.$nextTick().then(() => {
expect(e.returnValue).not.toBeNull();
});
});
it('confirms on description change', () => {
wrapper.vm.showForm = true;
wrapper.vm.state.descriptionText = 'description has changed';
const e = { returnValue: null };
wrapper.vm.handleBeforeUnloadEvent(e);
return wrapper.vm.$nextTick().then(() => {
expect(e.returnValue).not.toBeNull();
});
});
it('does nothing when nothing has changed', () => {
const e = { returnValue: null };
wrapper.vm.handleBeforeUnloadEvent(e);
return wrapper.vm.$nextTick().then(() => {
expect(e.returnValue).toBeNull();
});
});
});
describe('error when updating', () => {
it('closes form on error', () => {
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockRejectedValue();
return wrapper.vm.updateIssuable().then(() => {
expect(eventHub.$emit).not.toHaveBeenCalledWith('close.form');
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
`Error updating issue`,
);
});
});
it('returns the correct error message for issuableType', () => {
jest.spyOn(wrapper.vm.service, 'updateIssuable').mockRejectedValue();
wrapper.setProps({ issuableType: 'merge request' });
return wrapper.vm
.$nextTick()
.then(wrapper.vm.updateIssuable)
.then(() => {
expect(eventHub.$emit).not.toHaveBeenCalledWith('close.form');
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
`Error updating merge request`,
);
});
});
it('shows error message from backend if exists', () => {
const msg = 'Custom error message from backend';
jest
.spyOn(wrapper.vm.service, 'updateIssuable')
.mockRejectedValue({ response: { data: { errors: [msg] } } });
return wrapper.vm.updateIssuable().then(() => {
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
`${wrapper.vm.defaultErrorMessage}. ${msg}`,
);
});
});
});
});
describe('deleteIssuable', () => {
it('changes URL when deleted', () => {
jest.spyOn(wrapper.vm.service, 'deleteIssuable').mockResolvedValue({
data: {
web_url: '/test',
},
});
return wrapper.vm.deleteIssuable().then(() => {
expect(visitUrl).toHaveBeenCalledWith('/test');
});
});
it('stops polling when deleting', () => {
const spy = jest.spyOn(wrapper.vm.poll, 'stop');
jest.spyOn(wrapper.vm.service, 'deleteIssuable').mockResolvedValue({
data: {
web_url: '/test',
},
});
return wrapper.vm.deleteIssuable().then(() => {
expect(spy).toHaveBeenCalledWith();
});
});
it('closes form on error', () => {
jest.spyOn(wrapper.vm.service, 'deleteIssuable').mockRejectedValue();
return wrapper.vm.deleteIssuable().then(() => {
expect(eventHub.$emit).not.toHaveBeenCalledWith('close.form');
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
'Error deleting issue',
);
});
});
});
describe('updateAndShowForm', () => {
it('shows locked warning if form is open & data is different', () => {
return wrapper.vm
.$nextTick()
.then(() => {
wrapper.vm.updateAndShowForm();
wrapper.vm.poll.makeRequest();
return new Promise(resolve => {
wrapper.vm.$watch('formState.lockedWarningVisible', value => {
if (value) {
resolve();
}
});
});
})
.then(() => {
expect(wrapper.vm.formState.lockedWarningVisible).toEqual(true);
expect(wrapper.vm.formState.lock_version).toEqual(1);
expect(wrapper.find('.alert').exists()).toBe(true);
});
});
});
describe('requestTemplatesAndShowForm', () => {
let formSpy;
beforeEach(() => {
formSpy = jest.spyOn(wrapper.vm, 'updateAndShowForm');
});
it('shows the form if template names request is successful', () => {
const mockData = [{ name: 'Bug' }];
mock.onGet('/issuable-templates-path').reply(() => Promise.resolve([200, mockData]));
return wrapper.vm.requestTemplatesAndShowForm().then(() => {
expect(formSpy).toHaveBeenCalledWith(mockData);
});
});
it('shows the form if template names request failed', () => {
mock
.onGet('/issuable-templates-path')
.reply(() => Promise.reject(new Error('something went wrong')));
return wrapper.vm.requestTemplatesAndShowForm().then(() => {
expect(document.querySelector('.flash-container .flash-text').textContent).toContain(
'Error updating issue',
);
expect(formSpy).toHaveBeenCalledWith();
});
});
});
describe('show inline edit button', () => {
it('should not render by default', () => {
expect(wrapper.find('.btn-edit').exists()).toBe(true);
});
it('should render if showInlineEditButton', () => {
wrapper.setProps({ showInlineEditButton: true });
return wrapper.vm.$nextTick(() => {
expect(wrapper.find('.btn-edit').exists()).toBe(true);
});
});
});
describe('updateStoreState', () => {
it('should make a request and update the state of the store', () => {
const data = { foo: 1 };
const getDataSpy = jest.spyOn(wrapper.vm.service, 'getData').mockResolvedValue({ data });
const updateStateSpy = jest
.spyOn(wrapper.vm.store, 'updateState')
.mockImplementation(jest.fn);
return wrapper.vm.updateStoreState().then(() => {
expect(getDataSpy).toHaveBeenCalled();
expect(updateStateSpy).toHaveBeenCalledWith(data);
});
});
it('should show error message if store update fails', () => {
jest.spyOn(wrapper.vm.service, 'getData').mockRejectedValue();
wrapper.setProps({ issuableType: 'merge request' });
return wrapper.vm.updateStoreState().then(() => {
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
`Error updating ${wrapper.vm.issuableType}`,
);
});
});
});
describe('issueChanged', () => {
beforeEach(() => {
wrapper.vm.store.formState.title = '';
wrapper.vm.store.formState.description = '';
wrapper.setProps({
initialDescriptionText: '',
initialTitleText: '',
});
});
it('returns true when title is changed', () => {
wrapper.vm.store.formState.title = 'RandomText';
expect(wrapper.vm.issueChanged).toBe(true);
});
it('returns false when title is empty null', () => {
wrapper.vm.store.formState.title = null;
expect(wrapper.vm.issueChanged).toBe(false);
});
it('returns false when `initialTitleText` is null and `formState.title` is empty string', () => {
wrapper.vm.store.formState.title = '';
wrapper.setProps({ initialTitleText: null });
expect(wrapper.vm.issueChanged).toBe(false);
});
it('returns true when description is changed', () => {
wrapper.vm.store.formState.description = 'RandomText';
expect(wrapper.vm.issueChanged).toBe(true);
});
it('returns false when description is empty null', () => {
wrapper.vm.store.formState.description = null;
expect(wrapper.vm.issueChanged).toBe(false);
});
it('returns false when `initialDescriptionText` is null and `formState.description` is empty string', () => {
wrapper.vm.store.formState.description = '';
wrapper.setProps({ initialDescriptionText: null });
expect(wrapper.vm.issueChanged).toBe(false);
});
});
describe('sticky header', () => {
describe('when title is in view', () => {
it('is not shown', () => {
expect(wrapper.find('.issue-sticky-header').exists()).toBe(false);
});
});
describe('when title is not in view', () => {
beforeEach(() => {
wrapper.vm.state.titleText = 'Sticky header title';
wrapper.find(GlIntersectionObserver).vm.$emit('disappear');
});
it('is shown with title', () => {
expect(findStickyHeader().text()).toContain('Sticky header title');
});
it('is shown with Open when status is opened', () => {
wrapper.setProps({ issuableStatus: 'opened' });
return wrapper.vm.$nextTick(() => {
expect(findStickyHeader().text()).toContain('Open');
});
});
it('is shown with Closed when status is closed', () => {
wrapper.setProps({ issuableStatus: 'closed' });
return wrapper.vm.$nextTick(() => {
expect(findStickyHeader().text()).toContain('Closed');
});
});
});
});
describe('Composable description component', () => {
const findIncidentTabs = () => wrapper.find(IncidentTabs);
const findDescriptionComponent = () => wrapper.find(DescriptionComponent);
const findPinnedLinks = () => wrapper.find(PinnedLinks);
const borderClass = 'gl-border-b-1 gl-border-b-gray-100 gl-border-b-solid gl-mb-6';
describe('when using description component', () => {
it('renders the description component', () => {
expect(findDescriptionComponent().exists()).toBe(true);
});
it('does not render incident tabs', () => {
expect(findIncidentTabs().exists()).toBe(false);
});
it('adds a border below the header', () => {
expect(findPinnedLinks().attributes('class')).toContain(borderClass);
});
});
describe('when using incident tabs description wrapper', () => {
beforeEach(() => {
mountComponent({
descriptionComponent: IncidentTabs,
showTitleBorder: false,
});
});
it('renders the description component', () => {
expect(findDescriptionComponent().exists()).toBe(true);
});
it('renders incident tabs', () => {
expect(findIncidentTabs().exists()).toBe(true);
});
it('does not add a border below the header', () => {
expect(findPinnedLinks().attributes('class')).not.toContain(borderClass);
});
});
});
});
|