summaryrefslogtreecommitdiff
path: root/spec/frontend/api/packages_api_spec.js
blob: d55d2036dcf581d508318c614a5f93cdda99db10 (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
import MockAdapter from 'axios-mock-adapter';
import { publishPackage } from '~/api/packages_api';
import axios from '~/lib/utils/axios_utils';
import httpStatus from '~/lib/utils/http_status';

describe('Api', () => {
  const dummyApiVersion = 'v3000';
  const dummyUrlRoot = '/gitlab';
  const dummyGon = {
    api_version: dummyApiVersion,
    relative_url_root: dummyUrlRoot,
  };
  let originalGon;
  let mock;

  beforeEach(() => {
    mock = new MockAdapter(axios);
    originalGon = window.gon;
    window.gon = { ...dummyGon };
  });

  afterEach(() => {
    mock.restore();
    window.gon = originalGon;
  });

  describe('packages', () => {
    const projectPath = 'project_a';
    const name = 'foo';
    const packageVersion = '0';
    const apiResponse = [{ id: 1, name: 'foo' }];

    describe('publishPackage', () => {
      it('publishes the package', () => {
        const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/packages/generic/${name}/${packageVersion}/${name}`;

        jest.spyOn(axios, 'put');
        mock.onPut(expectedUrl).replyOnce(httpStatus.OK, apiResponse);

        return publishPackage(
          {
            projectPath,
            name,
            version: 0,
            fileName: name,
            files: [new File(['zip contents'], 'bar.zip')],
          },
          { status: 'hidden', select: 'package_file' },
        ).then(({ data }) => {
          expect(data).toEqual(apiResponse);
          expect(axios.put).toHaveBeenCalledWith(expectedUrl, expect.any(File), {
            params: { select: 'package_file', status: 'hidden' },
          });
        });
      });
    });
  });
});