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: 00b57b4916e98f0d27cffa4997cb35addbf1c99e (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
import Vue, { 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,
  STATUS_ACKNOWLEDGED,
} from '~/sidebar/constants';
import waitForPromises from 'helpers/wait_for_promises';
import EscalationStatus from 'ee_else_ce/sidebar/components/incidents/escalation_status.vue';
import { createAlert } from '~/alert';
import { logError } from '~/lib/logger';

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

Vue.use(VueApollo);

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

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

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

    return createMockApollo(requestHandlers);
  }

  function createComponent(apolloProvider) {
    wrapper = mountExtended(SidebarEscalationStatus, {
      propsData: {
        iid: '1',
        projectPath: 'gitlab-org/gitlab',
        issuableType: 'issue',
      },
      provide: {
        canUpdate: true,
      },
      directives: {
        GlTooltip: createMockDirective('gl-tooltip'),
      },
      apolloProvider,
    });

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

  beforeEach(() => {
    mockApollo = createMockApolloProvider();
  });

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

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

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

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

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

      it('has correct tooltip', async () => {
        await createComponent(mockApollo);

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

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

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

      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 () => {
        await createComponent(mockApollo);

        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 () => {
        mockApollo = createMockApolloProvider({ hasFetchError: true });
        await createComponent(mockApollo);

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

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

        await clickEditButton();
        await selectAcknowledgedStatus();

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