Welcome to mirror list, hosted at ThFree Co, Russian Federation.

note_actions_spec.js « components « notes « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc50afcb01d00192f1ce16e795b9ea81e4f6f23c (plain)
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
import { GlDisclosureDropdown, GlDisclosureDropdownItem } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import { stubComponent } from 'helpers/stub_component';
import { TEST_HOST } from 'spec/test_constants';
import axios from '~/lib/utils/axios_utils';
import noteActions from '~/notes/components/note_actions.vue';
import { NOTEABLE_TYPE_MAPPING } from '~/notes/constants';
import TimelineEventButton from '~/notes/components/note_actions/timeline_event_button.vue';
import AbuseCategorySelector from '~/abuse_reports/components/abuse_category_selector.vue';
import createStore from '~/notes/stores';
import UserAccessRoleBadge from '~/vue_shared/components/user_access_role_badge.vue';
import { userDataMock } from '../mock_data';

describe('noteActions', () => {
  let wrapper;
  let store;
  let props;
  let actions;
  let axiosMock;

  const mockCloseDropdown = jest.fn();

  const findUserAccessRoleBadge = (idx) => wrapper.findAllComponents(UserAccessRoleBadge).at(idx);
  const findUserAccessRoleBadgeText = (idx) => findUserAccessRoleBadge(idx).text().trim();
  const findTimelineButton = () => wrapper.findComponent(TimelineEventButton);
  const findReportAbuseButton = () => wrapper.find(`[data-testid="report-abuse-button"]`);

  const setupStoreForIncidentTimelineEvents = ({
    userCanAdd,
    noteableType,
    isPromotionInProgress = true,
  }) => {
    store.dispatch('setUserData', {
      ...userDataMock,
      can_add_timeline_events: userCanAdd,
    });
    store.state.noteableData = {
      ...store.state.noteableData,
      type: noteableType,
    };
    store.state.isPromoteCommentToTimelineEventInProgress = isPromotionInProgress;
  };

  const mountNoteActions = (propsData, computed) => {
    return mount(noteActions, {
      store,
      propsData,
      computed,
      stubs: {
        GlDisclosureDropdown: stubComponent(GlDisclosureDropdown, {
          methods: {
            close: mockCloseDropdown,
          },
        }),
        GlDisclosureDropdownItem,
      },
    });
  };

  beforeEach(() => {
    store = createStore();

    props = {
      accessLevel: 'Maintainer',
      authorId: 1,
      author: userDataMock,
      canDelete: true,
      canEdit: true,
      canAwardEmoji: true,
      canReportAsAbuse: true,
      isAuthor: true,
      isContributor: false,
      noteableType: 'MergeRequest',
      noteId: '539',
      noteUrl: `${TEST_HOST}/group/project/-/merge_requests/1#note_1`,
      projectName: 'project',
      showReply: false,
      awardPath: `${TEST_HOST}/award_emoji`,
    };

    actions = {
      updateAssignees: jest.fn(),
    };

    axiosMock = new AxiosMockAdapter(axios);
  });

  afterEach(() => {
    axiosMock.restore();
  });

  describe('user is logged in', () => {
    beforeEach(() => {
      store.dispatch('setUserData', userDataMock);

      wrapper = mountNoteActions(props);
    });

    it('should render noteable author badge', () => {
      expect(findUserAccessRoleBadgeText(0)).toBe('Author');
    });

    it('should render access level badge', () => {
      expect(findUserAccessRoleBadgeText(1)).toBe(props.accessLevel);
    });

    it('should render contributor badge', async () => {
      wrapper.setProps({
        accessLevel: null,
        isContributor: true,
      });

      await nextTick();
      expect(findUserAccessRoleBadgeText(1)).toBe('Contributor');
    });

    it('should render emoji link', () => {
      expect(wrapper.find('[data-testid="note-emoji-button"]').exists()).toBe(true);
    });

    describe('actions dropdown', () => {
      it('should be possible to edit the comment', () => {
        expect(wrapper.find('.js-note-edit').exists()).toBe(true);
      });

      it('should be possible to report abuse to admin', () => {
        expect(findReportAbuseButton().exists()).toBe(true);
      });

      it('should be possible to copy link to a note', () => {
        expect(wrapper.find('.js-btn-copy-note-link').exists()).toBe(true);
      });

      it('should not show copy link action when `noteUrl` prop is empty', async () => {
        wrapper.setProps({
          ...props,
          author: {
            avatar_url: 'mock_path',
            id: 26,
            name: 'Example Maintainer',
            path: '/ExampleMaintainer',
            state: 'active',
            username: 'ExampleMaintainer',
          },
          noteUrl: '',
        });

        await nextTick();
        expect(wrapper.find('.js-btn-copy-note-link').exists()).toBe(false);
      });

      it('should be possible to delete comment', () => {
        expect(wrapper.find('.js-note-delete').exists()).toBe(true);
      });

      it('should not be possible to assign or unassign the comment author in a merge request', () => {
        const assignUserButton = wrapper.find('[data-testid="assign-user"]');
        expect(assignUserButton.exists()).toBe(false);
      });

      it('should render the correct (unescaped) name in the Resolved By tooltip', () => {
        const complexUnescapedName = 'This is a Ǝ\'𝞓\'E "cat"?';
        wrapper = mountNoteActions({
          ...props,
          canResolve: true,
          isResolving: false,
          isResolved: true,
          resolvedBy: {
            name: complexUnescapedName,
          },
        });

        const { resolveButton } = wrapper.vm.$refs;
        expect(resolveButton.$el.getAttribute('title')).toBe(`Resolved by ${complexUnescapedName}`);
      });
    });
  });

  describe('when a user can set metadata of an issue', () => {
    const testButtonClickTriggersAction = () => {
      axiosMock.onPut(`${TEST_HOST}/api/v4/projects/group/project/issues/1`).reply(() => {
        expect(actions.updateAssignees).toHaveBeenCalled();
      });

      const assignUserButton = wrapper.find('[data-testid="assign-user"]');
      expect(assignUserButton.exists()).toBe(true);
      assignUserButton.trigger('click');
    };

    beforeEach(() => {
      wrapper = mountNoteActions(props, {
        targetType: () => 'issue',
      });
      store.state.noteableData = {
        current_user: {
          can_set_issue_metadata: true,
        },
      };
      store.state.userData = userDataMock;
    });

    afterEach(() => {
      axiosMock.restore();
    });

    it('should be possible to assign the comment author', testButtonClickTriggersAction);
    it('should be possible to unassign the comment author', testButtonClickTriggersAction);
  });

  describe('when a user can update but not set metadata of an issue', () => {
    beforeEach(() => {
      wrapper = mountNoteActions(props, {
        targetType: () => 'issue',
      });
      store.state.noteableData = {
        current_user: {
          can_update: true,
          can_set_issue_metadata: false,
        },
      };
      store.state.userData = userDataMock;
    });

    afterEach(() => {
      axiosMock.restore();
    });

    it('should not be possible to assign or unassign the comment author', () => {
      const assignUserButton = wrapper.find('[data-testid="assign-user"]');
      expect(assignUserButton.exists()).toBe(false);
    });
  });

  describe('when a user does not have access to edit an issue', () => {
    const testButtonDoesNotRender = () => {
      const assignUserButton = wrapper.find('[data-testid="assign-user"]');
      expect(assignUserButton.exists()).toBe(false);
    };

    beforeEach(() => {
      wrapper = mountNoteActions(props, {
        targetType: () => 'issue',
      });
    });

    it('should not be possible to assign the comment author', testButtonDoesNotRender);
    it('should not be possible to unassign the comment author', testButtonDoesNotRender);
  });

  describe('user is not logged in', () => {
    beforeEach(() => {
      // userData can be null https://gitlab.com/gitlab-org/gitlab/-/issues/379375
      store.dispatch('setUserData', null);
      wrapper = mountNoteActions({
        ...props,
        canDelete: false,
        canEdit: false,
        canAwardEmoji: false,
        canReportAsAbuse: false,
      });
    });

    it('should not render emoji link', () => {
      expect(wrapper.find('.js-add-award').exists()).toBe(false);
    });

    it('should not render actions dropdown', () => {
      expect(wrapper.find('.more-actions').exists()).toBe(false);
    });
  });

  describe('for showReply = true', () => {
    beforeEach(() => {
      wrapper = mountNoteActions({
        ...props,
        showReply: true,
      });
    });

    it('shows a reply button', () => {
      const replyButton = wrapper.findComponent({ ref: 'replyButton' });

      expect(replyButton.exists()).toBe(true);
    });
  });

  describe('for showReply = false', () => {
    beforeEach(() => {
      wrapper = mountNoteActions({
        ...props,
        showReply: false,
      });
    });

    it('does not show a reply button', () => {
      const replyButton = wrapper.findComponent({ ref: 'replyButton' });

      expect(replyButton.exists()).toBe(false);
    });
  });

  describe('Draft notes', () => {
    beforeEach(() => {
      store.dispatch('setUserData', userDataMock);

      wrapper = mountNoteActions({ ...props, canResolve: true, isDraft: true });
    });

    it('should render the right resolve button title', () => {
      const resolveButton = wrapper.findComponent({ ref: 'resolveButton' });

      expect(resolveButton.exists()).toBe(true);
      expect(resolveButton.attributes('title')).toBe('Thread stays unresolved');
    });
  });

  describe('timeline event button', () => {
    // why: We are working with an integrated store, so let's imply the getter is used
    describe.each`
      desc                                         | userCanAdd | noteableType                          | exists
      ${'default'}                                 | ${true}    | ${NOTEABLE_TYPE_MAPPING.Incident}     | ${true}
      ${'when cannot add incident timeline event'} | ${false}   | ${NOTEABLE_TYPE_MAPPING.Incident}     | ${false}
      ${'when is not incident'}                    | ${true}    | ${NOTEABLE_TYPE_MAPPING.MergeRequest} | ${false}
    `('$desc', ({ userCanAdd, noteableType, exists }) => {
      beforeEach(() => {
        setupStoreForIncidentTimelineEvents({
          userCanAdd,
          noteableType,
        });

        wrapper = mountNoteActions({ ...props });
      });

      it(`handles rendering of timeline button (exists=${exists})`, () => {
        expect(findTimelineButton().exists()).toBe(exists);
      });
    });

    describe('default', () => {
      beforeEach(() => {
        setupStoreForIncidentTimelineEvents({
          userCanAdd: true,
          noteableType: NOTEABLE_TYPE_MAPPING.Incident,
        });

        wrapper = mountNoteActions({ ...props });
      });

      it('should render timeline-event-button', () => {
        expect(findTimelineButton().props()).toEqual({
          noteId: props.noteId,
          isPromotionInProgress: true,
        });
      });

      it('when timeline-event-button emits click-promote-comment-to-event, dispatches action', () => {
        jest.spyOn(store, 'dispatch').mockImplementation();

        expect(store.dispatch).not.toHaveBeenCalled();

        findTimelineButton().vm.$emit('click-promote-comment-to-event');

        expect(store.dispatch).toHaveBeenCalledTimes(1);
        expect(store.dispatch).toHaveBeenCalledWith('promoteCommentToTimelineEvent');
      });
    });
  });

  describe('report abuse button', () => {
    const findAbuseCategorySelector = () => wrapper.findComponent(AbuseCategorySelector);

    describe('when user is not allowed to report abuse', () => {
      beforeEach(() => {
        store.dispatch('setUserData', userDataMock);
        wrapper = mountNoteActions({ ...props, canReportAsAbuse: false });
      });

      it('does not render the report abuse', () => {
        expect(findReportAbuseButton().exists()).toBe(false);
      });

      it('does not render the abuse category drawer', () => {
        expect(findAbuseCategorySelector().exists()).toBe(false);
      });
    });

    describe('when user is allowed to report abuse', () => {
      beforeEach(() => {
        store.dispatch('setUserData', userDataMock);
        wrapper = mountNoteActions({ ...props, canReportAsAbuse: true });
      });

      it('renders report abuse button', () => {
        expect(findReportAbuseButton().exists()).toBe(true);
      });

      it('does not render the abuse category drawer immediately', () => {
        expect(findAbuseCategorySelector().exists()).toBe(false);
      });

      it('opens the drawer when report abuse button is clicked', async () => {
        await findReportAbuseButton().vm.$emit('action');

        expect(findAbuseCategorySelector().props('showDrawer')).toEqual(true);
      });

      it('closes the drawer', async () => {
        await findReportAbuseButton().vm.$emit('action');
        findAbuseCategorySelector().vm.$emit('close-drawer');

        await nextTick();

        expect(findAbuseCategorySelector().exists()).toEqual(false);
      });
    });
  });
});