summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/pipelines_actions_spec.js
blob: 26e61efc4f6eaf20246b76213d08c97d4feafecf (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
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { TEST_HOST } from 'spec/test_constants';
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
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 GlCountdown from '~/vue_shared/components/gl_countdown.vue';
import { TRACKING_CATEGORIES } from '~/pipelines/constants';

jest.mock('~/flash');
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal', () => {
  return {
    confirmAction: jest.fn(),
  };
});

describe('Pipelines Actions dropdown', () => {
  let wrapper;
  let mock;

  const createComponent = (props, mountFn = shallowMount) => {
    wrapper = mountFn(PipelinesManualActions, {
      propsData: {
        ...props,
      },
    });
  };

  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findAllDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
  const findAllCountdowns = () => wrapper.findAllComponents(GlCountdown);

  beforeEach(() => {
    mock = new MockAdapter(axios);
  });

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

    mock.restore();
    confirmAction.mockReset();
  });

  describe('manual actions', () => {
    const mockActions = [
      {
        name: 'stop_review',
        path: `${TEST_HOST}/root/review-app/builds/1893/play`,
      },
      {
        name: 'foo',
        path: `${TEST_HOST}/disabled/pipeline/action`,
        playable: false,
      },
    ];

    beforeEach(() => {
      createComponent({ actions: mockActions });
    });

    it('renders a dropdown with the provided actions', () => {
      expect(findAllDropdownItems()).toHaveLength(mockActions.length);
    });

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

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

        findAllDropdownItems().at(0).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(mockActions.path).reply(500);

        findAllDropdownItems().at(0).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', () => {
    const scheduledJobAction = {
      name: 'scheduled action',
      path: `${TEST_HOST}/scheduled/job/action`,
      playable: true,
      scheduled_at: '2063-04-05T00:42:00Z',
    };
    const expiredJobAction = {
      name: 'expired action',
      path: `${TEST_HOST}/expired/job/action`,
      playable: true,
      scheduled_at: '2018-10-05T08:23:00Z',
    };

    beforeEach(() => {
      jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
      createComponent({ actions: [scheduledJobAction, expiredJobAction] });
    });

    it('makes post request after confirming', async () => {
      mock.onPost(scheduledJobAction.path).reply(200);
      confirmAction.mockResolvedValueOnce(true);

      findAllDropdownItems().at(0).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(scheduledJobAction.path).reply(200);
      confirmAction.mockResolvedValueOnce(false);

      findAllDropdownItems().at(0).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(
        scheduledJobAction.scheduled_at,
      );
    });

    it('displays 00:00:00 for expired jobs in the dropdown', () => {
      expect(findAllCountdowns().at(1).props('endDateString')).toBe(expiredJobAction.scheduled_at);
    });
  });
});