summaryrefslogtreecommitdiff
path: root/spec/javascripts/registry/stores/actions_spec.js
blob: 0613ec8e0f1e6e56f5f557b4e83dce345ae05793 (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
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import * as actions from '~/registry/stores/actions';
import * as types from '~/registry/stores/mutation_types';
import state from '~/registry/stores/state';
import { TEST_HOST } from 'spec/test_constants';
import testAction from '../../helpers/vuex_action_helper';
import {
  reposServerResponse,
  registryServerResponse,
  parsedReposServerResponse,
} from '../mock_data';

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

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

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

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

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

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

        mock.onGet(repo.tagsPath).replyOnce(200, registryServerResponse, {});
      });

      it('should set received list', done => {
        testAction(
          actions.fetchList,
          { repo },
          mockedState,
          [
            { 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: jasmine.anything(),
              },
            },
          ],
          [],
          done,
        );
      });
    });
  });

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

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

  describe('deleteItem', () => {
    it('should perform DELETE request on destroyPath', done => {
      const destroyPath = `${TEST_HOST}/mygroup/myproject/container_registry/1.json`;
      let deleted = false;
      mock.onDelete(destroyPath).replyOnce(() => {
        deleted = true;
        return [200];
      });
      testAction(
        actions.deleteItem,
        {
          destroyPath,
        },
        mockedState,
      )
        .then(() => {
          expect(mock.history.delete.length).toBe(1);
          expect(deleted).toBe(true);
          done();
        })
        .catch(done.fail);
    });
  });
});