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

abuse_report_notes_spec.js « 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: a1993e2bde38e367eb60272c1d5fe73827099c11 (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
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import SkeletonLoadingContainer from '~/vue_shared/components/notes/skeleton_note.vue';
import abuseReportNotesQuery from '~/admin/abuse_report/graphql/notes/abuse_report_notes.query.graphql';
import AbuseReportNotes from '~/admin/abuse_report/components/abuse_report_notes.vue';
import AbuseReportDiscussion from '~/admin/abuse_report/components/notes/abuse_report_discussion.vue';
import AbuseReportAddNote from '~/admin/abuse_report/components/notes/abuse_report_add_note.vue';

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

jest.mock('~/alert');

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

  Vue.use(VueApollo);

  const mockAbuseReportId = mockAbuseReport.report.globalId;

  const notesQueryHandler = jest.fn().mockResolvedValue(mockNotesByIdResponse);

  const findSkeletonLoaders = () => wrapper.findAllComponents(SkeletonLoadingContainer);
  const findAbuseReportDiscussions = () => wrapper.findAllComponents(AbuseReportDiscussion);
  const findAbuseReportAddNote = () => wrapper.findComponent(AbuseReportAddNote);

  const createComponent = ({
    queryHandler = notesQueryHandler,
    abuseReportId = mockAbuseReportId,
  } = {}) => {
    wrapper = shallowMount(AbuseReportNotes, {
      apolloProvider: createMockApollo([[abuseReportNotesQuery, queryHandler]]),
      propsData: {
        abuseReportId,
      },
    });
  };

  describe('when notes are loading', () => {
    beforeEach(() => {
      createComponent();
    });

    it('should show the skeleton loaders', () => {
      expect(findSkeletonLoaders()).toHaveLength(5);
    });
  });

  describe('when notes have been loaded', () => {
    beforeEach(() => {
      createComponent();
      return waitForPromises();
    });

    it('should not render skeleton loader', () => {
      expect(findSkeletonLoaders()).toHaveLength(0);
    });

    it('should call the abuse report notes query', () => {
      expect(notesQueryHandler).toHaveBeenCalledWith({
        id: mockAbuseReportId,
      });
    });

    it('should show notes to the length of the response', () => {
      expect(findAbuseReportDiscussions()).toHaveLength(2);

      const discussions = mockNotesByIdResponse.data.abuseReport.discussions.nodes;

      expect(findAbuseReportDiscussions().at(0).props()).toMatchObject({
        abuseReportId: mockAbuseReportId,
        discussion: discussions[0].notes.nodes,
      });

      expect(findAbuseReportDiscussions().at(1).props()).toMatchObject({
        abuseReportId: mockAbuseReportId,
        discussion: discussions[1].notes.nodes,
      });
    });

    it('should show the comment form', () => {
      expect(findAbuseReportAddNote().exists()).toBe(true);

      expect(findAbuseReportAddNote().props()).toMatchObject({
        abuseReportId: mockAbuseReportId,
        discussionId: '',
        isNewDiscussion: true,
      });
    });
  });

  describe('When there is an error fetching the notes', () => {
    beforeEach(() => {
      createComponent({
        queryHandler: jest.fn().mockRejectedValue(new Error()),
      });

      return waitForPromises();
    });

    it('should show an error when query fails', () => {
      expect(createAlert).toHaveBeenCalledWith({
        message: 'An error occurred while fetching comments, please try again.',
      });
    });
  });
});