summaryrefslogtreecommitdiff
path: root/spec/frontend/registry/list/stores/actions_spec.js
blob: 1a9c23ed188dc68ff1b2fb00579435ffcbf7320a (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 MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import * as actions from '~/registry/list/stores/actions';
import * as types from '~/registry/list/stores/mutation_types';
import { TEST_HOST } from 'helpers/test_constants';
import testAction from 'helpers/vuex_action_helper';
import createFlash from '~/flash';

import {
  reposServerResponse,
  registryServerResponse,
  parsedReposServerResponse,
} from '../mock_data';

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

describe('Actions Registry Store', () => {
  let mock;
  let state;

  beforeEach(() => {
    mock = new MockAdapter(axios);
    state = {
      endpoint: `${TEST_HOST}/endpoint.json`,
    };
  });

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

  describe('fetchRepos', () => {
    beforeEach(() => {
      mock.onGet(`${TEST_HOST}/endpoint.json`).replyOnce(200, reposServerResponse, {});
    });

    it('should set received repos', done => {
      testAction(
        actions.fetchRepos,
        null,
        state,
        [
          { type: types.TOGGLE_MAIN_LOADING },
          { type: types.TOGGLE_MAIN_LOADING },
          { type: types.SET_REPOS_LIST, payload: reposServerResponse },
        ],
        [],
        done,
      );
    });

    it('should create flash on API error', done => {
      testAction(
        actions.fetchRepos,
        null,
        {
          endpoint: null,
        },
        [{ type: types.TOGGLE_MAIN_LOADING }, { type: types.TOGGLE_MAIN_LOADING }],
        [],
        () => {
          expect(createFlash).toHaveBeenCalled();
          done();
        },
      );
    });
  });

  describe('fetchList', () => {
    let repo;
    beforeEach(() => {
      state.repos = parsedReposServerResponse;
      [, repo] = state.repos;
    });

    it('should set received list', done => {
      mock.onGet(repo.tagsPath).replyOnce(200, registryServerResponse, {});
      testAction(
        actions.fetchList,
        { repo },
        state,
        [
          { type: types.TOGGLE_REGISTRY_LIST_LOADING, payload: repo },
          { type: types.TOGGLE_REGISTRY_LIST_LOADING, payload: repo },
          {
            type: types.SET_REGISTRY_LIST,
            payload: {
              repo,
              resp: registryServerResponse,
              headers: expect.anything(),
            },
          },
        ],
        [],
        done,
      );
    });

    it('should create flash on API error', done => {
      mock.onGet(repo.tagsPath).replyOnce(400);
      const updatedRepo = {
        ...repo,
        tagsPath: null,
      };
      testAction(
        actions.fetchList,
        {
          repo: updatedRepo,
        },
        state,
        [
          { type: types.TOGGLE_REGISTRY_LIST_LOADING, payload: updatedRepo },
          { type: types.TOGGLE_REGISTRY_LIST_LOADING, payload: updatedRepo },
        ],
        [],
        () => {
          expect(createFlash).toHaveBeenCalled();
          done();
        },
      );
    });
  });

  describe('setMainEndpoint', () => {
    it('should commit set main endpoint', done => {
      testAction(
        actions.setMainEndpoint,
        'endpoint',
        state,
        [{ type: types.SET_MAIN_ENDPOINT, payload: 'endpoint' }],
        [],
        done,
      );
    });
  });

  describe('setIsDeleteDisabled', () => {
    it('should commit set is delete disabled', done => {
      testAction(
        actions.setIsDeleteDisabled,
        true,
        state,
        [{ type: types.SET_IS_DELETE_DISABLED, payload: true }],
        [],
        done,
      );
    });
  });

  describe('toggleLoading', () => {
    it('should commit toggle main loading', done => {
      testAction(
        actions.toggleLoading,
        null,
        state,
        [{ type: types.TOGGLE_MAIN_LOADING }],
        [],
        done,
      );
    });
  });

  describe('deleteItem and multiDeleteItems', () => {
    let deleted;
    const destroyPath = `${TEST_HOST}/mygroup/myproject/container_registry/1.json`;

    const expectDelete = done => {
      expect(mock.history.delete.length).toBe(1);
      expect(deleted).toBe(true);
      done();
    };

    beforeEach(() => {
      deleted = false;
      mock.onDelete(destroyPath).replyOnce(() => {
        deleted = true;
        return [200];
      });
    });

    it('deleteItem should perform DELETE request on destroyPath', done => {
      testAction(
        actions.deleteItem,
        {
          destroyPath,
        },
        state,
      )
        .then(() => {
          expectDelete(done);
        })
        .catch(done.fail);
    });

    it('multiDeleteItems should perform DELETE request on path', done => {
      testAction(actions.multiDeleteItems, { path: destroyPath, items: [1] }, state)
        .then(() => {
          expectDelete(done);
        })
        .catch(done.fail);
    });
  });
});