summaryrefslogtreecommitdiff
path: root/spec/frontend/user_lists/store/index/actions_spec.js
blob: c5d7d557de9dd16d40a53023432473756949138a (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
import testAction from 'helpers/vuex_action_helper';
import Api from '~/api';
import {
  setUserListsOptions,
  requestUserLists,
  receiveUserListsSuccess,
  receiveUserListsError,
  fetchUserLists,
  deleteUserList,
  receiveDeleteUserListError,
  clearAlert,
} from '~/user_lists/store/index/actions';
import * as types from '~/user_lists/store/index/mutation_types';
import createState from '~/user_lists/store/index/state';
import { userList } from '../../../feature_flags/mock_data';

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

describe('~/user_lists/store/index/actions', () => {
  let state;

  beforeEach(() => {
    state = createState({ projectId: '1' });
  });

  describe('setUserListsOptions', () => {
    it('should commit SET_USER_LISTS_OPTIONS mutation', (done) => {
      testAction(
        setUserListsOptions,
        { page: '1', scope: 'all' },
        state,
        [{ type: types.SET_USER_LISTS_OPTIONS, payload: { page: '1', scope: 'all' } }],
        [],
        done,
      );
    });
  });

  describe('fetchUserLists', () => {
    beforeEach(() => {
      Api.fetchFeatureFlagUserLists.mockResolvedValue({ data: [userList], headers: {} });
    });

    describe('success', () => {
      it('dispatches requestUserLists and receiveUserListsSuccess ', (done) => {
        testAction(
          fetchUserLists,
          null,
          state,
          [],
          [
            {
              type: 'requestUserLists',
            },
            {
              payload: { data: [userList], headers: {} },
              type: 'receiveUserListsSuccess',
            },
          ],
          done,
        );
      });
    });

    describe('error', () => {
      it('dispatches requestUserLists and receiveUserListsError ', (done) => {
        Api.fetchFeatureFlagUserLists.mockRejectedValue();

        testAction(
          fetchUserLists,
          null,
          state,
          [],
          [
            {
              type: 'requestUserLists',
            },
            {
              type: 'receiveUserListsError',
            },
          ],
          done,
        );
      });
    });
  });

  describe('requestUserLists', () => {
    it('should commit RECEIVE_USER_LISTS_SUCCESS mutation', (done) => {
      testAction(requestUserLists, null, state, [{ type: types.REQUEST_USER_LISTS }], [], done);
    });
  });

  describe('receiveUserListsSuccess', () => {
    it('should commit RECEIVE_USER_LISTS_SUCCESS mutation', (done) => {
      testAction(
        receiveUserListsSuccess,
        { data: [userList], headers: {} },
        state,
        [
          {
            type: types.RECEIVE_USER_LISTS_SUCCESS,
            payload: { data: [userList], headers: {} },
          },
        ],
        [],
        done,
      );
    });
  });

  describe('receiveUserListsError', () => {
    it('should commit RECEIVE_USER_LISTS_ERROR mutation', (done) => {
      testAction(
        receiveUserListsError,
        null,
        state,
        [{ type: types.RECEIVE_USER_LISTS_ERROR }],
        [],
        done,
      );
    });
  });

  describe('deleteUserList', () => {
    beforeEach(() => {
      state.userLists = [userList];
    });

    describe('success', () => {
      beforeEach(() => {
        Api.deleteFeatureFlagUserList.mockResolvedValue();
      });

      it('should refresh the user lists', (done) => {
        testAction(
          deleteUserList,
          userList,
          state,
          [],
          [{ type: 'requestDeleteUserList', payload: userList }, { type: 'fetchUserLists' }],
          done,
        );
      });
    });

    describe('error', () => {
      beforeEach(() => {
        Api.deleteFeatureFlagUserList.mockRejectedValue({ response: { data: 'some error' } });
      });

      it('should dispatch receiveDeleteUserListError', (done) => {
        testAction(
          deleteUserList,
          userList,
          state,
          [],
          [
            { type: 'requestDeleteUserList', payload: userList },
            {
              type: 'receiveDeleteUserListError',
              payload: { list: userList, error: 'some error' },
            },
          ],
          done,
        );
      });
    });
  });

  describe('receiveDeleteUserListError', () => {
    it('should commit RECEIVE_DELETE_USER_LIST_ERROR with the given list', (done) => {
      testAction(
        receiveDeleteUserListError,
        { list: userList, error: 'mock error' },
        state,
        [
          {
            type: 'RECEIVE_DELETE_USER_LIST_ERROR',
            payload: { list: userList, error: 'mock error' },
          },
        ],
        [],
        done,
      );
    });
  });

  describe('clearAlert', () => {
    it('should commit RECEIVE_CLEAR_ALERT', (done) => {
      const alertIndex = 3;

      testAction(
        clearAlert,
        alertIndex,
        state,
        [{ type: 'RECEIVE_CLEAR_ALERT', payload: alertIndex }],
        [],
        done,
      );
    });
  });
});