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

sidebar_confidentiality_form_spec.js « confidential « components « sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ea035c718463ade751cc2ab4849b6ff423ce189 (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
import { GlSprintf } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';
import createFlash from '~/flash';
import SidebarConfidentialityForm from '~/sidebar/components/confidential/sidebar_confidentiality_form.vue';
import { confidentialityQueries } from '~/sidebar/constants';

jest.mock('~/flash');

describe('Sidebar Confidentiality Form', () => {
  let wrapper;

  const findWarningMessage = () => wrapper.find(`[data-testid="warning-message"]`);
  const findConfidentialToggle = () => wrapper.find(`[data-testid="confidential-toggle"]`);
  const findCancelButton = () => wrapper.find(`[data-testid="confidential-cancel"]`);

  const createComponent = ({
    props = {},
    mutate = jest.fn().mockResolvedValue('Success'),
  } = {}) => {
    wrapper = shallowMount(SidebarConfidentialityForm, {
      propsData: {
        fullPath: 'group/project',
        iid: '1',
        confidential: false,
        issuableType: 'issue',
        ...props,
      },
      mocks: {
        $apollo: {
          mutate,
        },
      },
      stubs: {
        GlSprintf,
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  it('emits a `closeForm` event when Cancel button is clicked', () => {
    createComponent();
    findCancelButton().vm.$emit('click');

    expect(wrapper.emitted().closeForm).toHaveLength(1);
  });

  it('renders a loading state after clicking on turn on/off button', async () => {
    createComponent();
    findConfidentialToggle().vm.$emit('click', new MouseEvent('click'));

    expect(wrapper.vm.$apollo.mutate).toHaveBeenCalled();
    await nextTick();
    expect(findConfidentialToggle().props('loading')).toBe(true);
  });

  it('creates a flash if mutation is rejected', async () => {
    createComponent({ mutate: jest.fn().mockRejectedValue('Error!') });
    findConfidentialToggle().vm.$emit('click', new MouseEvent('click'));
    await waitForPromises();

    expect(createFlash).toHaveBeenCalledWith({
      message: 'Something went wrong while setting issue confidentiality.',
    });
  });

  it('creates a flash if mutation contains errors', async () => {
    createComponent({
      mutate: jest.fn().mockResolvedValue({
        data: { issuableSetConfidential: { errors: ['Houston, we have a problem!'] } },
      }),
    });
    findConfidentialToggle().vm.$emit('click', new MouseEvent('click'));
    await waitForPromises();

    expect(createFlash).toHaveBeenCalledWith({
      message: 'Houston, we have a problem!',
    });
  });

  describe('when issue is not confidential', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders a message about making an issue confidential', () => {
      expect(findWarningMessage().text()).toBe(
        'You are going to turn on confidentiality. Only project members with at least the Reporter role, the author, and assignees can view or be notified about this issue.',
      );
    });

    it('has a `Turn on` button text', () => {
      expect(findConfidentialToggle().text()).toBe('Turn on');
    });

    it('calls a mutation to set confidential to true on button click', () => {
      findConfidentialToggle().vm.$emit('click', new MouseEvent('click'));
      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: confidentialityQueries[wrapper.vm.issuableType].mutation,
        variables: {
          input: {
            confidential: true,
            iid: '1',
            projectPath: 'group/project',
          },
        },
      });
    });
  });

  describe('when issue is confidential', () => {
    beforeEach(() => {
      createComponent({ props: { confidential: true } });
    });

    it('renders a message about making an issue non-confidential', () => {
      expect(findWarningMessage().text()).toBe(
        'You are going to turn off the confidentiality. This means everyone will be able to see and leave a comment on this issue.',
      );
    });

    it('has a `Turn off` button text', () => {
      expect(findConfidentialToggle().text()).toBe('Turn off');
    });

    it('calls a mutation to set confidential to false on button click', () => {
      findConfidentialToggle().vm.$emit('click', new MouseEvent('click'));
      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: confidentialityQueries[wrapper.vm.issuableType].mutation,
        variables: {
          input: {
            confidential: false,
            iid: '1',
            projectPath: 'group/project',
          },
        },
      });
    });
  });

  describe('when issuable type is `epic`', () => {
    beforeEach(() => {
      createComponent({ props: { confidential: true, issuableType: 'epic' } });
    });

    it('renders a message about making an epic non-confidential', () => {
      expect(findWarningMessage().text()).toBe(
        'You are going to turn off the confidentiality. This means everyone will be able to see and leave a comment on this epic.',
      );
    });

    it('calls a mutation to set epic confidentiality with correct parameters', () => {
      findConfidentialToggle().vm.$emit('click', new MouseEvent('click'));

      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: confidentialityQueries[wrapper.vm.issuableType].mutation,
        variables: {
          input: {
            confidential: false,
            iid: '1',
            groupPath: 'group/project',
          },
        },
      });
    });
  });
});