summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/pipelines_manual_actions_spec.js
blob: e9695d57f93baed8b4fb49033b4dc9bddff0211b (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
import { GlDropdown, GlDropdownItem, GlLoadingIcon } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import mockPipelineActionsQueryResponse from 'test_fixtures/graphql/pipelines/get_pipeline_actions.query.graphql.json';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
import PipelinesManualActions from '~/pipelines/components/pipelines_list/pipelines_manual_actions.vue';
import getPipelineActionsQuery from '~/pipelines/graphql/queries/get_pipeline_actions.query.graphql';
import { TRACKING_CATEGORIES } from '~/pipelines/constants';
import GlCountdown from '~/vue_shared/components/gl_countdown.vue';

Vue.use(VueApollo);

jest.mock('~/alert');
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal');

describe('Pipeline manual actions', () => {
  let wrapper;
  let mock;

  const queryHandler = jest.fn().mockResolvedValue(mockPipelineActionsQueryResponse);
  const {
    data: {
      project: {
        pipeline: {
          jobs: { nodes },
        },
      },
    },
  } = mockPipelineActionsQueryResponse;

  const mockPath = nodes[2].playPath;

  const createComponent = (limit = 50) => {
    wrapper = shallowMountExtended(PipelinesManualActions, {
      provide: {
        fullPath: 'root/ci-project',
        manualActionsLimit: limit,
      },
      propsData: {
        iid: 100,
      },
      stubs: {
        GlDropdown,
      },
      apolloProvider: createMockApollo([[getPipelineActionsQuery, queryHandler]]),
    });
  };

  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findAllDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
  const findAllCountdowns = () => wrapper.findAllComponents(GlCountdown);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findLimitMessage = () => wrapper.findByTestId('limit-reached-msg');

  it('skips calling query on mount', () => {
    createComponent();

    expect(queryHandler).not.toHaveBeenCalled();
  });

  describe('loading', () => {
    beforeEach(() => {
      createComponent();

      findDropdown().vm.$emit('shown');
    });

    it('display loading state while actions are being fetched', async () => {
      expect(findAllDropdownItems().at(0).text()).toBe('Loading...');
      expect(findLoadingIcon().exists()).toBe(true);
      expect(findAllDropdownItems()).toHaveLength(1);
    });
  });

  describe('loaded', () => {
    beforeEach(async () => {
      mock = new MockAdapter(axios);

      createComponent();

      findDropdown().vm.$emit('shown');

      await waitForPromises();
    });

    afterEach(() => {
      mock.restore();
      confirmAction.mockReset();
    });

    it('displays dropdown with the provided actions', () => {
      expect(findAllDropdownItems()).toHaveLength(3);
    });

    it("displays a disabled action when it's not playable", () => {
      expect(findAllDropdownItems().at(0).attributes('disabled')).toBe('true');
    });

    describe('on action click', () => {
      it('makes a request and toggles the loading state', async () => {
        mock.onPost(mockPath).reply(HTTP_STATUS_OK);

        findAllDropdownItems().at(1).vm.$emit('click');

        await nextTick();

        expect(findDropdown().props('loading')).toBe(true);

        await waitForPromises();

        expect(findDropdown().props('loading')).toBe(false);
      });

      it('makes a failed request and toggles the loading state', async () => {
        mock.onPost(mockPath).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);

        findAllDropdownItems().at(1).vm.$emit('click');

        await nextTick();

        expect(findDropdown().props('loading')).toBe(true);

        await waitForPromises();

        expect(findDropdown().props('loading')).toBe(false);
        expect(createAlert).toHaveBeenCalledTimes(1);
      });
    });

    describe('tracking', () => {
      afterEach(() => {
        unmockTracking();
      });

      it('tracks manual actions click', () => {
        const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);

        findDropdown().vm.$emit('shown');

        expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_manual_actions', {
          label: TRACKING_CATEGORIES.table,
        });
      });
    });

    describe('scheduled jobs', () => {
      beforeEach(() => {
        jest
          .spyOn(Date, 'now')
          .mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
      });

      it('makes post request after confirming', async () => {
        mock.onPost(mockPath).reply(HTTP_STATUS_OK);

        confirmAction.mockResolvedValueOnce(true);

        findAllDropdownItems().at(2).vm.$emit('click');

        expect(confirmAction).toHaveBeenCalled();

        await waitForPromises();

        expect(mock.history.post).toHaveLength(1);
      });

      it('does not make post request if confirmation is cancelled', async () => {
        mock.onPost(mockPath).reply(HTTP_STATUS_OK);

        confirmAction.mockResolvedValueOnce(false);

        findAllDropdownItems().at(2).vm.$emit('click');

        expect(confirmAction).toHaveBeenCalled();

        await waitForPromises();

        expect(mock.history.post).toHaveLength(0);
      });

      it('displays the remaining time in the dropdown', () => {
        expect(findAllCountdowns().at(0).props('endDateString')).toBe(nodes[2].scheduledAt);
      });
    });
  });

  describe('limit message', () => {
    it('limit message does not show', async () => {
      createComponent();

      findDropdown().vm.$emit('shown');

      await waitForPromises();

      expect(findLimitMessage().exists()).toBe(false);
    });

    it('limit message does show', async () => {
      createComponent(3);

      findDropdown().vm.$emit('shown');

      await waitForPromises();

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