summaryrefslogtreecommitdiff
path: root/spec/frontend/packages_and_registries/infrastructure_registry/components/details/store/actions_spec.js
blob: b9383d6c38cc8c70e499a6330ef07bb57fd21bc8 (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
import testAction from 'helpers/vuex_action_helper';
import Api from '~/api';
import createFlash from '~/flash';
import { FETCH_PACKAGE_VERSIONS_ERROR } from '~/packages_and_registries/infrastructure_registry/details/constants';
import {
  fetchPackageVersions,
  deletePackage,
  deletePackageFile,
} from '~/packages_and_registries/infrastructure_registry/details/store/actions';
import * as types from '~/packages_and_registries/infrastructure_registry/details/store/mutation_types';
import {
  DELETE_PACKAGE_ERROR_MESSAGE,
  DELETE_PACKAGE_FILE_ERROR_MESSAGE,
  DELETE_PACKAGE_FILE_SUCCESS_MESSAGE,
} from '~/packages_and_registries/shared/constants';
import { npmPackage as packageEntity } from '../../mock_data';

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

describe('Actions Package details store', () => {
  describe('fetchPackageVersions', () => {
    it('should fetch the package versions', (done) => {
      Api.projectPackage = jest.fn().mockResolvedValue({ data: packageEntity });

      testAction(
        fetchPackageVersions,
        undefined,
        { packageEntity },
        [
          { type: types.SET_LOADING, payload: true },
          { type: types.SET_PACKAGE_VERSIONS, payload: packageEntity.versions },
          { type: types.SET_LOADING, payload: false },
        ],
        [],
        () => {
          expect(Api.projectPackage).toHaveBeenCalledWith(
            packageEntity.project_id,
            packageEntity.id,
          );
          done();
        },
      );
    });

    it("does not set the versions if they don't exist", (done) => {
      Api.projectPackage = jest.fn().mockResolvedValue({ data: { packageEntity, versions: null } });

      testAction(
        fetchPackageVersions,
        undefined,
        { packageEntity },
        [
          { type: types.SET_LOADING, payload: true },
          { type: types.SET_LOADING, payload: false },
        ],
        [],
        () => {
          expect(Api.projectPackage).toHaveBeenCalledWith(
            packageEntity.project_id,
            packageEntity.id,
          );
          done();
        },
      );
    });

    it('should create flash on API error', (done) => {
      Api.projectPackage = jest.fn().mockRejectedValue();

      testAction(
        fetchPackageVersions,
        undefined,
        { packageEntity },
        [
          { type: types.SET_LOADING, payload: true },
          { type: types.SET_LOADING, payload: false },
        ],
        [],
        () => {
          expect(Api.projectPackage).toHaveBeenCalledWith(
            packageEntity.project_id,
            packageEntity.id,
          );
          expect(createFlash).toHaveBeenCalledWith({
            message: FETCH_PACKAGE_VERSIONS_ERROR,
            type: 'warning',
          });
          done();
        },
      );
    });
  });

  describe('deletePackage', () => {
    it('should call Api.deleteProjectPackage', (done) => {
      Api.deleteProjectPackage = jest.fn().mockResolvedValue();
      testAction(deletePackage, undefined, { packageEntity }, [], [], () => {
        expect(Api.deleteProjectPackage).toHaveBeenCalledWith(
          packageEntity.project_id,
          packageEntity.id,
        );
        done();
      });
    });
    it('should create flash on API error', (done) => {
      Api.deleteProjectPackage = jest.fn().mockRejectedValue();

      testAction(deletePackage, undefined, { packageEntity }, [], [], () => {
        expect(createFlash).toHaveBeenCalledWith({
          message: DELETE_PACKAGE_ERROR_MESSAGE,
          type: 'warning',
        });
        done();
      });
    });
  });

  describe('deletePackageFile', () => {
    const fileId = 'a_file_id';

    it('should call Api.deleteProjectPackageFile and commit the right data', (done) => {
      const packageFiles = [{ id: 'foo' }, { id: fileId }];
      Api.deleteProjectPackageFile = jest.fn().mockResolvedValue();
      testAction(
        deletePackageFile,
        fileId,
        { packageEntity, packageFiles },
        [{ type: types.UPDATE_PACKAGE_FILES, payload: [{ id: 'foo' }] }],
        [],
        () => {
          expect(Api.deleteProjectPackageFile).toHaveBeenCalledWith(
            packageEntity.project_id,
            packageEntity.id,
            fileId,
          );
          expect(createFlash).toHaveBeenCalledWith({
            message: DELETE_PACKAGE_FILE_SUCCESS_MESSAGE,
            type: 'success',
          });
          done();
        },
      );
    });
    it('should create flash on API error', (done) => {
      Api.deleteProjectPackageFile = jest.fn().mockRejectedValue();
      testAction(deletePackageFile, fileId, { packageEntity }, [], [], () => {
        expect(createFlash).toHaveBeenCalledWith({
          message: DELETE_PACKAGE_FILE_ERROR_MESSAGE,
          type: 'warning',
        });
        done();
      });
    });
  });
});