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

abuse_report_add_note_spec.js « notes « components « abuse_report « admin « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 959b52beaef5f3ca6a0a85dd638b1c574260d6fe (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
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import { createAlert } from '~/alert';
import { clearDraft } from '~/lib/utils/autosave';
import waitForPromises from 'helpers/wait_for_promises';
import createNoteMutation from '~/admin/abuse_report/graphql/notes/create_abuse_report_note.mutation.graphql';
import AbuseReportAddNote from '~/admin/abuse_report/components/notes/abuse_report_add_note.vue';
import AbuseReportCommentForm from '~/admin/abuse_report/components/notes/abuse_report_comment_form.vue';

import { mockAbuseReport, createAbuseReportNoteResponse } from '../../mock_data';

jest.mock('~/alert');
jest.mock('~/lib/utils/autosave');
Vue.use(VueApollo);

describe('Abuse Report Add Note', () => {
  let wrapper;

  const mockAbuseReportId = mockAbuseReport.report.globalId;
  const mockDiscussionId = 'gid://gitlab/Discussion/9c7228e06fb0339a3d1440fcda960acfd8baa43a';

  const mutationSuccessHandler = jest.fn().mockResolvedValue(createAbuseReportNoteResponse);

  const findTimelineEntry = () => wrapper.findByTestId('abuse-report-note-timeline-entry');
  const findTimelineEntryInner = () =>
    wrapper.findByTestId('abuse-report-note-timeline-entry-inner');
  const findCommentFormWrapper = () => wrapper.findByTestId('abuse-report-comment-form-wrapper');

  const findAbuseReportCommentForm = () => wrapper.findComponent(AbuseReportCommentForm);
  const findReplyTextarea = () => wrapper.findByTestId('abuse-report-note-reply-textarea');

  const createComponent = ({
    mutationHandler = mutationSuccessHandler,
    abuseReportId = mockAbuseReportId,
    discussionId = '',
    isNewDiscussion = true,
    showCommentForm = false,
  } = {}) => {
    wrapper = shallowMountExtended(AbuseReportAddNote, {
      apolloProvider: createMockApollo([[createNoteMutation, mutationHandler]]),
      propsData: {
        abuseReportId,
        discussionId,
        isNewDiscussion,
        showCommentForm,
      },
    });
  };

  describe('Default', () => {
    beforeEach(() => {
      createComponent();
    });

    it('should show the comment form', () => {
      expect(findAbuseReportCommentForm().exists()).toBe(true);
      expect(findAbuseReportCommentForm().props()).toMatchObject({
        abuseReportId: mockAbuseReportId,
        isSubmitting: false,
        autosaveKey: `${mockAbuseReportId}-comment`,
        commentButtonText: 'Comment',
        initialValue: '',
      });
    });

    it('should not show the reply textarea', () => {
      expect(findReplyTextarea().exists()).toBe(false);
    });

    it('should add the correct classList to timeline-entry', () => {
      expect(findTimelineEntry().classes()).toEqual(
        expect.arrayContaining(['timeline-entry', 'note-form']),
      );

      expect(findTimelineEntryInner().classes()).toEqual(['timeline-entry-inner']);
    });
  });

  describe('When the main comments has replies', () => {
    beforeEach(() => {
      createComponent({
        discussionId: 'gid://gitlab/Discussion/9c7228e06fb0339a3d1440fcda960acfd8baa43a',
        isNewDiscussion: false,
      });
    });

    it('should add the correct classLists', () => {
      expect(findTimelineEntry().classes()).toEqual(
        expect.arrayContaining([
          'note',
          'note-wrapper',
          'note-comment',
          'discussion-reply-holder',
          'gl-border-t-0!',
          'clearfix',
        ]),
      );

      expect(findTimelineEntryInner().classes()).toEqual([]);

      expect(findCommentFormWrapper().classes()).toEqual(
        expect.arrayContaining([
          'gl-relative',
          'gl-display-flex',
          'gl-align-items-flex-start',
          'gl-flex-nowrap',
        ]),
      );
    });

    it('should show not the comment form', () => {
      expect(findAbuseReportCommentForm().exists()).toBe(false);
    });

    it('should show the reply textarea', () => {
      expect(findReplyTextarea().exists()).toBe(true);
      expect(findReplyTextarea().attributes()).toMatchObject({
        rows: '1',
        placeholder: 'Reply…',
        'aria-label': 'Reply to comment',
      });
    });
  });

  describe('Adding a comment', () => {
    const noteText = 'mock note';

    beforeEach(() => {
      createComponent();

      findAbuseReportCommentForm().vm.$emit('submitForm', {
        commentText: noteText,
      });
    });

    it('should call the mutation with provided noteText', async () => {
      expect(findAbuseReportCommentForm().props('isSubmitting')).toBe(true);

      expect(mutationSuccessHandler).toHaveBeenCalledWith({
        input: {
          noteableId: mockAbuseReportId,
          body: noteText,
          discussionId: null,
        },
      });

      await waitForPromises();

      expect(findAbuseReportCommentForm().props('isSubmitting')).toBe(false);
    });

    it('should add the correct classList to comment-form wrapper', () => {
      expect(findCommentFormWrapper().classes()).toEqual([]);
    });

    it('should clear draft from local storage', async () => {
      await waitForPromises();

      expect(clearDraft).toHaveBeenCalledWith(`${mockAbuseReportId}-comment`);
    });

    it('should emit `cancelEditing` event', async () => {
      await waitForPromises();

      expect(wrapper.emitted('cancelEditing')).toHaveLength(1);
    });

    it.each`
      description                     | errorResponse
      ${'with an error response'}     | ${new Error('The discussion could not be found')}
      ${'without an error ressponse'} | ${null}
    `('should show an error when mutation fails $description', async ({ errorResponse }) => {
      createComponent({
        mutationHandler: jest.fn().mockRejectedValue(errorResponse),
      });

      findAbuseReportCommentForm().vm.$emit('submitForm', {
        commentText: noteText,
      });

      await waitForPromises();

      const errorMessage = errorResponse
        ? 'Your comment could not be submitted because the discussion could not be found.'
        : 'Your comment could not be submitted! Please check your network connection and try again.';

      expect(createAlert).toHaveBeenCalledWith({
        message: errorMessage,
        captureError: true,
        parent: expect.anything(),
      });
    });
  });

  describe('Replying to a comment', () => {
    beforeEach(() => {
      createComponent({
        discussionId: mockDiscussionId,
        isNewDiscussion: false,
        showCommentForm: false,
      });
    });

    it('should not show the comment form', () => {
      expect(findAbuseReportCommentForm().exists()).toBe(false);
    });

    it('should show comment form when reply textarea is clicked on', async () => {
      await findReplyTextarea().trigger('click');

      expect(findAbuseReportCommentForm().exists()).toBe(true);
      expect(findAbuseReportCommentForm().props('commentButtonText')).toBe('Reply');
    });

    it('should show comment form if `showCommentForm` is true', () => {
      createComponent({
        discussionId: mockDiscussionId,
        isNewDiscussion: false,
        showCommentForm: true,
      });

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