summaryrefslogtreecommitdiff
path: root/spec/frontend/error_tracking_settings/store/actions_spec.js
blob: bcd816c2ae08b1101eed815c9bb9429085d3567b (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
import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants';
import testAction from 'helpers/vuex_action_helper';
import * as actions from '~/error_tracking_settings/store/actions';
import * as types from '~/error_tracking_settings/store/mutation_types';
import defaultState from '~/error_tracking_settings/store/state';
import axios from '~/lib/utils/axios_utils';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { refreshCurrentPage } from '~/lib/utils/url_utility';
import { projectList } from '../mock';

jest.mock('~/lib/utils/url_utility');

describe('error tracking settings actions', () => {
  let state;

  describe('project list actions', () => {
    let mock;

    beforeEach(() => {
      mock = new MockAdapter(axios);
      state = { ...defaultState(), listProjectsEndpoint: TEST_HOST };
    });

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

    it('should request and transform the project list', async () => {
      mock.onGet(TEST_HOST).reply(() => [200, { projects: projectList }]);
      await testAction(
        actions.fetchProjects,
        null,
        state,
        [],
        [
          { type: 'requestProjects' },
          {
            type: 'receiveProjectsSuccess',
            payload: projectList.map(convertObjectPropsToCamelCase),
          },
        ],
      );
      expect(mock.history.get.length).toBe(1);
    });

    it('should handle a server error', async () => {
      mock.onGet(`${TEST_HOST}.json`).reply(() => [400]);
      await testAction(
        actions.fetchProjects,
        null,
        state,
        [],
        [
          { type: 'requestProjects' },
          {
            type: 'receiveProjectsError',
          },
        ],
      );
      expect(mock.history.get.length).toBe(1);
    });

    it('should request projects correctly', () => {
      return testAction(
        actions.requestProjects,
        null,
        state,
        [{ type: types.SET_PROJECTS_LOADING, payload: true }, { type: types.RESET_CONNECT }],
        [],
      );
    });

    it('should receive projects correctly', () => {
      const testPayload = [];
      return testAction(
        actions.receiveProjectsSuccess,
        testPayload,
        state,
        [
          { type: types.UPDATE_CONNECT_SUCCESS },
          { type: types.RECEIVE_PROJECTS, payload: testPayload },
          { type: types.SET_PROJECTS_LOADING, payload: false },
        ],
        [],
      );
    });

    it('should handle errors when receiving projects', () => {
      const testPayload = [];
      return testAction(
        actions.receiveProjectsError,
        testPayload,
        state,
        [
          { type: types.UPDATE_CONNECT_ERROR },
          { type: types.CLEAR_PROJECTS },
          { type: types.SET_PROJECTS_LOADING, payload: false },
        ],
        [],
      );
    });
  });

  describe('save changes actions', () => {
    let mock;

    beforeEach(() => {
      mock = new MockAdapter(axios);
      state = {
        operationsSettingsEndpoint: TEST_HOST,
      };
    });

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

    it('should save the page', async () => {
      mock.onPatch(TEST_HOST).reply(200);
      await testAction(actions.updateSettings, null, state, [], [{ type: 'requestSettings' }]);
      expect(mock.history.patch.length).toBe(1);
      expect(refreshCurrentPage).toHaveBeenCalled();
    });

    it('should handle a server error', async () => {
      mock.onPatch(TEST_HOST).reply(400);
      await testAction(
        actions.updateSettings,
        null,
        state,
        [],
        [
          { type: 'requestSettings' },
          {
            type: 'receiveSettingsError',
            payload: new Error('Request failed with status code 400'),
          },
        ],
      );
      expect(mock.history.patch.length).toBe(1);
    });

    it('should request to save the page', () => {
      return testAction(
        actions.requestSettings,
        null,
        state,
        [{ type: types.UPDATE_SETTINGS_LOADING, payload: true }],
        [],
      );
    });

    it('should handle errors when requesting to save the page', () => {
      return testAction(
        actions.receiveSettingsError,
        {},
        state,
        [{ type: types.UPDATE_SETTINGS_LOADING, payload: false }],
        [],
      );
    });
  });

  describe('generic actions to update the store', () => {
    const testData = 'test';
    it('should reset the `connect success` flag when updating the api host', () => {
      return testAction(
        actions.updateApiHost,
        testData,
        state,
        [{ type: types.UPDATE_API_HOST, payload: testData }, { type: types.RESET_CONNECT }],
        [],
      );
    });

    it('should reset the `connect success` flag when updating the token', () => {
      return testAction(
        actions.updateToken,
        testData,
        state,
        [{ type: types.UPDATE_TOKEN, payload: testData }, { type: types.RESET_CONNECT }],
        [],
      );
    });

    it.each([true, false])('should set the `integrated` flag to `%s`', async (payload) => {
      await testAction(actions.updateIntegrated, payload, state, [
        { type: types.UPDATE_INTEGRATED, payload },
      ]);
    });
  });
});