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

sidebar_escalation_status_spec.js « incidents « components « sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88a4913a27f8a10276e1e79cc0455ddda363b9c6 (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
import { createLocalVue } from '@vue/test-utils';
import { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import {
  fetchData,
  fetchError,
  mutationData,
  mutationError,
} from 'ee_else_ce_jest/sidebar/components/incidents/mock_data';
import createMockApollo from 'helpers/mock_apollo_helper';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import SidebarEscalationStatus from '~/sidebar/components/incidents/sidebar_escalation_status.vue';
import SidebarEditableItem from '~/sidebar/components/sidebar_editable_item.vue';
import { escalationStatusQuery, escalationStatusMutation } from '~/sidebar/constants';
import waitForPromises from 'helpers/wait_for_promises';
import EscalationStatus from 'ee_else_ce/sidebar/components/incidents/escalation_status.vue';
import { STATUS_ACKNOWLEDGED } from '~/sidebar/components/incidents/constants';
import { createAlert } from '~/flash';
import { logError } from '~/lib/logger';

jest.mock('~/lib/logger');
jest.mock('~/flash');

const localVue = createLocalVue();

describe('SidebarEscalationStatus', () => {
  let wrapper;
  const queryResolverMock = jest.fn();
  const mutationResolverMock = jest.fn();

  function createMockApolloProvider({ hasFetchError = false, hasMutationError = false } = {}) {
    localVue.use(VueApollo);

    queryResolverMock.mockResolvedValue({ data: hasFetchError ? fetchError : fetchData });
    mutationResolverMock.mockResolvedValue({
      data: hasMutationError ? mutationError : mutationData,
    });

    const requestHandlers = [
      [escalationStatusQuery, queryResolverMock],
      [escalationStatusMutation, mutationResolverMock],
    ];

    return createMockApollo(requestHandlers);
  }

  function createComponent({ mockApollo } = {}) {
    let config;

    if (mockApollo) {
      config = { apolloProvider: mockApollo };
    } else {
      config = { mocks: { $apollo: { queries: { status: { loading: false } } } } };
    }

    wrapper = mountExtended(SidebarEscalationStatus, {
      propsData: {
        iid: '1',
        projectPath: 'gitlab-org/gitlab',
        issuableType: 'issue',
      },
      provide: {
        canUpdate: true,
      },
      directives: {
        GlTooltip: createMockDirective(),
      },
      localVue,
      ...config,
    });
  }

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

  const findSidebarComponent = () => wrapper.findComponent(SidebarEditableItem);
  const findStatusComponent = () => wrapper.findComponent(EscalationStatus);
  const findEditButton = () => wrapper.findByTestId('edit-button');
  const findIcon = () => wrapper.findByTestId('status-icon');

  const clickEditButton = async () => {
    findEditButton().vm.$emit('click');
    await nextTick();
  };
  const selectAcknowledgedStatus = async () => {
    findStatusComponent().vm.$emit('input', STATUS_ACKNOWLEDGED);
    // wait for apollo requests
    await waitForPromises();
  };

  describe('sidebar', () => {
    it('renders the sidebar component', () => {
      createComponent();
      expect(findSidebarComponent().exists()).toBe(true);
    });

    describe('status icon', () => {
      it('is visible', () => {
        createComponent();

        expect(findIcon().exists()).toBe(true);
        expect(findIcon().isVisible()).toBe(true);
      });

      it('has correct tooltip', async () => {
        const mockApollo = createMockApolloProvider();
        createComponent({ mockApollo });

        // wait for apollo requests
        await waitForPromises();

        const tooltip = getBinding(findIcon().element, 'gl-tooltip');

        expect(tooltip).toBeDefined();
        expect(tooltip.value).toBe('Status: Triggered');
      });
    });

    describe('status dropdown', () => {
      beforeEach(async () => {
        const mockApollo = createMockApolloProvider();
        createComponent({ mockApollo });

        // wait for apollo requests
        await waitForPromises();
      });

      it('is closed by default', () => {
        expect(findStatusComponent().exists()).toBe(true);
        expect(findStatusComponent().isVisible()).toBe(false);
      });

      it('is shown after clicking the edit button', async () => {
        await clickEditButton();

        expect(findStatusComponent().isVisible()).toBe(true);
      });

      it('is hidden after clicking the edit button, when open already', async () => {
        await clickEditButton();
        await clickEditButton();

        expect(findStatusComponent().isVisible()).toBe(false);
      });
    });

    describe('update Status event', () => {
      beforeEach(async () => {
        const mockApollo = createMockApolloProvider();
        createComponent({ mockApollo });

        // wait for apollo requests
        await waitForPromises();

        await clickEditButton();
        await selectAcknowledgedStatus();
      });

      it('calls the mutation', () => {
        const mutationVariables = {
          iid: '1',
          projectPath: 'gitlab-org/gitlab',
          status: STATUS_ACKNOWLEDGED,
        };

        expect(mutationResolverMock).toHaveBeenCalledWith(mutationVariables);
      });

      it('closes the dropdown', () => {
        expect(findStatusComponent().isVisible()).toBe(false);
      });

      it('updates the status', () => {
        // Sometimes status has a intermediate wrapping component. A quirk of vue-test-utils
        // means that in that case 'value' is exposed as a prop. If no wrapping component
        // exists it is exposed as an attribute.
        const statusValue =
          findStatusComponent().props('value') || findStatusComponent().attributes('value');
        expect(statusValue).toBe(STATUS_ACKNOWLEDGED);
      });
    });

    describe('mutation errors', () => {
      it('should error upon fetch', async () => {
        const mockApollo = createMockApolloProvider({ hasFetchError: true });
        createComponent({ mockApollo });

        // wait for apollo requests
        await waitForPromises();

        expect(createAlert).toHaveBeenCalled();
        expect(logError).toHaveBeenCalled();
      });

      it('should error upon mutation', async () => {
        const mockApollo = createMockApolloProvider({ hasMutationError: true });
        createComponent({ mockApollo });

        // wait for apollo requests
        await waitForPromises();

        await clickEditButton();
        await selectAcknowledgedStatus();

        expect(createAlert).toHaveBeenCalled();
        expect(logError).toHaveBeenCalled();
      });
    });
  });
});