summaryrefslogtreecommitdiff
path: root/spec/frontend/deploy_freeze/store/actions_spec.js
blob: cd823e1fc28542056b2fdeb6778935036b892c41 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import Api from '~/api';
import * as actions from '~/deploy_freeze/store/actions';
import * as types from '~/deploy_freeze/store/mutation_types';
import getInitialState from '~/deploy_freeze/store/state';
import { createAlert } from '~/flash';
import * as logger from '~/lib/logger';
import axios from '~/lib/utils/axios_utils';
import { freezePeriodsFixture } from '../helpers';
import { timezoneDataFixture } from '../../vue_shared/components/timezone_dropdown/helpers';

jest.mock('~/api.js');
jest.mock('~/flash');

describe('deploy freeze store actions', () => {
  const freezePeriodFixture = freezePeriodsFixture[0];
  let mock;
  let state;

  beforeEach(() => {
    mock = new MockAdapter(axios);
    state = getInitialState({
      projectId: '8',
      timezoneData: timezoneDataFixture,
    });
    Api.freezePeriods.mockResolvedValue({ data: freezePeriodsFixture });
    Api.createFreezePeriod.mockResolvedValue();
    Api.updateFreezePeriod.mockResolvedValue();
    Api.deleteFreezePeriod.mockResolvedValue();
  });

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

  describe('setSelectedFreezePeriod', () => {
    it('commits SET_SELECTED_TIMEZONE mutation', () => {
      testAction(
        actions.setFreezePeriod,
        {
          id: 3,
          cronTimezone: 'UTC',
          freezeStart: 'start',
          freezeEnd: 'end',
        },
        {},
        [
          {
            payload: 3,
            type: types.SET_SELECTED_ID,
          },
          {
            payload: 'UTC',
            type: types.SET_SELECTED_TIMEZONE,
          },
          {
            payload: 'start',
            type: types.SET_FREEZE_START_CRON,
          },
          {
            payload: 'end',
            type: types.SET_FREEZE_END_CRON,
          },
        ],
      );
    });
  });

  describe('setSelectedTimezone', () => {
    it('commits SET_SELECTED_TIMEZONE mutation', () => {
      testAction(actions.setSelectedTimezone, {}, {}, [
        {
          payload: {},
          type: types.SET_SELECTED_TIMEZONE,
        },
      ]);
    });
  });

  describe('setFreezeStartCron', () => {
    it('commits SET_FREEZE_START_CRON mutation', () => {
      testAction(actions.setFreezeStartCron, {}, {}, [
        {
          type: types.SET_FREEZE_START_CRON,
        },
      ]);
    });
  });

  describe('setFreezeEndCron', () => {
    it('commits SET_FREEZE_END_CRON mutation', () => {
      testAction(actions.setFreezeEndCron, {}, {}, [
        {
          type: types.SET_FREEZE_END_CRON,
        },
      ]);
    });
  });

  describe('addFreezePeriod', () => {
    it('dispatch correct actions on adding a freeze period', async () => {
      await testAction(
        actions.addFreezePeriod,
        {},
        state,
        [{ type: 'RESET_MODAL' }],
        [
          { type: 'requestFreezePeriod' },
          { type: 'receiveFreezePeriodSuccess' },
          { type: 'fetchFreezePeriods' },
        ],
      );

      expect(Api.createFreezePeriod).toHaveBeenCalledWith(state.projectId, {
        freeze_start: state.freezeStartCron,
        freeze_end: state.freezeEndCron,
        cron_timezone: state.selectedTimezoneIdentifier,
      });
    });

    it('should show alert and set error in state on add failure', async () => {
      Api.createFreezePeriod.mockRejectedValue();

      await testAction(
        actions.addFreezePeriod,
        {},
        state,
        [],
        [{ type: 'requestFreezePeriod' }, { type: 'receiveFreezePeriodError' }],
      );

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

  describe('updateFreezePeriod', () => {
    it('dispatch correct actions on updating a freeze period', async () => {
      await testAction(
        actions.updateFreezePeriod,
        {},
        state,
        [{ type: 'RESET_MODAL' }],
        [
          { type: 'requestFreezePeriod' },
          { type: 'receiveFreezePeriodSuccess' },
          { type: 'fetchFreezePeriods' },
        ],
      );

      expect(Api.updateFreezePeriod).toHaveBeenCalledWith(state.projectId, {
        id: state.selectedId,
        freeze_start: state.freezeStartCron,
        freeze_end: state.freezeEndCron,
        cron_timezone: state.selectedTimezoneIdentifier,
      });
    });

    it('should show alert and set error in state on add failure', async () => {
      Api.updateFreezePeriod.mockRejectedValue();

      await testAction(
        actions.updateFreezePeriod,
        {},
        state,
        [],
        [{ type: 'requestFreezePeriod' }, { type: 'receiveFreezePeriodError' }],
      );

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

  describe('fetchFreezePeriods', () => {
    it('dispatch correct actions on fetchFreezePeriods', () => {
      return testAction(
        actions.fetchFreezePeriods,
        {},
        state,
        [
          { type: types.REQUEST_FREEZE_PERIODS },
          { type: types.RECEIVE_FREEZE_PERIODS_SUCCESS, payload: freezePeriodsFixture },
        ],
        [],
      );
    });

    it('should show alert and set error in state on fetch variables failure', async () => {
      Api.freezePeriods.mockRejectedValue();

      await testAction(
        actions.fetchFreezePeriods,
        {},
        state,
        [{ type: types.REQUEST_FREEZE_PERIODS }],
        [],
      );

      expect(createAlert).toHaveBeenCalledWith({
        message: 'There was an error fetching the deploy freezes.',
      });
    });
  });

  describe('deleteFreezePeriod', () => {
    it('dispatch correct actions on deleting a freeze period', async () => {
      await testAction(
        actions.deleteFreezePeriod,
        freezePeriodFixture,
        state,
        [
          { type: 'REQUEST_DELETE_FREEZE_PERIOD', payload: freezePeriodFixture.id },
          { type: 'RECEIVE_DELETE_FREEZE_PERIOD_SUCCESS', payload: freezePeriodFixture.id },
        ],
        [],
      );

      expect(Api.deleteFreezePeriod).toHaveBeenCalledWith(state.projectId, freezePeriodFixture.id);
    });

    it('should show alert and set error in state on delete failure', async () => {
      jest.spyOn(logger, 'logError').mockImplementation();
      const error = new Error();
      Api.deleteFreezePeriod.mockRejectedValue(error);

      await testAction(
        actions.deleteFreezePeriod,
        freezePeriodFixture,
        state,
        [
          { type: 'REQUEST_DELETE_FREEZE_PERIOD', payload: freezePeriodFixture.id },
          { type: 'RECEIVE_DELETE_FREEZE_PERIOD_ERROR', payload: freezePeriodFixture.id },
        ],
        [],
      );

      expect(createAlert).toHaveBeenCalled();

      expect(logger.logError).toHaveBeenCalledWith('Unable to delete deploy freeze', error);
    });
  });
});