2020-07-21 12:09:30 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2020-08-17 21:09:56 +00:00
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
2020-07-21 12:09:30 +00:00
|
|
|
import Api from '~/api';
|
2020-08-20 09:09:55 +00:00
|
|
|
import { deprecatedCreateFlash as createFlash } from '~/flash';
|
2020-07-21 12:09:30 +00:00
|
|
|
import * as actions from '~/packages/list/stores/actions';
|
|
|
|
import * as types from '~/packages/list/stores/mutation_types';
|
2020-09-09 15:08:47 +00:00
|
|
|
import { MISSING_DELETE_PATH_ERROR } from '~/packages/list/constants';
|
|
|
|
import { DELETE_PACKAGE_ERROR_MESSAGE } from '~/packages/shared/constants';
|
2020-07-21 12:09:30 +00:00
|
|
|
|
|
|
|
jest.mock('~/flash.js');
|
|
|
|
jest.mock('~/api.js');
|
|
|
|
|
|
|
|
describe('Actions Package list store', () => {
|
|
|
|
const headers = 'bar';
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
Api.projectPackages = jest.fn().mockResolvedValue({ data: 'foo', headers });
|
|
|
|
Api.groupPackages = jest.fn().mockResolvedValue({ data: 'baz', headers });
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('requestPackagesList', () => {
|
|
|
|
const sorting = {
|
|
|
|
sort: 'asc',
|
|
|
|
orderBy: 'version',
|
|
|
|
};
|
2021-01-29 18:09:17 +00:00
|
|
|
|
|
|
|
const filter = [];
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should fetch the project packages list when isGroupPage is false', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
testAction(
|
|
|
|
actions.requestPackagesList,
|
|
|
|
undefined,
|
2021-01-29 18:09:17 +00:00
|
|
|
{ config: { isGroupPage: false, resourceId: 1 }, sorting, filter },
|
2020-07-21 12:09:30 +00:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
{ type: 'setLoading', payload: true },
|
|
|
|
{ type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
|
|
|
|
{ type: 'setLoading', payload: false },
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
|
|
|
|
params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should fetch the group packages list when isGroupPage is true', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
testAction(
|
|
|
|
actions.requestPackagesList,
|
|
|
|
undefined,
|
2021-01-29 18:09:17 +00:00
|
|
|
{ config: { isGroupPage: true, resourceId: 2 }, sorting, filter },
|
2020-07-21 12:09:30 +00:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
{ type: 'setLoading', payload: true },
|
|
|
|
{ type: 'receivePackagesListSuccess', payload: { data: 'baz', headers } },
|
|
|
|
{ type: 'setLoading', payload: false },
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
expect(Api.groupPackages).toHaveBeenCalledWith(2, {
|
|
|
|
params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-01-29 18:09:17 +00:00
|
|
|
it('should fetch packages of a certain type when a filter with a type is present', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
const packageType = 'maven';
|
|
|
|
|
|
|
|
testAction(
|
|
|
|
actions.requestPackagesList,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
config: { isGroupPage: false, resourceId: 1 },
|
|
|
|
sorting,
|
2021-01-29 18:09:17 +00:00
|
|
|
filter: [{ type: 'type', value: { data: 'maven' } }],
|
2020-07-21 12:09:30 +00:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
{ type: 'setLoading', payload: true },
|
|
|
|
{ type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
|
|
|
|
{ type: 'setLoading', payload: false },
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
|
|
|
|
params: {
|
|
|
|
page: 1,
|
|
|
|
per_page: 20,
|
|
|
|
sort: sorting.sort,
|
|
|
|
order_by: sorting.orderBy,
|
|
|
|
package_type: packageType,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should create flash on API error', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
Api.projectPackages = jest.fn().mockRejectedValue();
|
|
|
|
testAction(
|
|
|
|
actions.requestPackagesList,
|
|
|
|
undefined,
|
2021-01-29 18:09:17 +00:00
|
|
|
{ config: { isGroupPage: false, resourceId: 2 }, sorting, filter },
|
2020-07-21 12:09:30 +00:00
|
|
|
[],
|
2020-12-23 12:10:26 +00:00
|
|
|
[
|
|
|
|
{ type: 'setLoading', payload: true },
|
|
|
|
{ type: 'setLoading', payload: false },
|
|
|
|
],
|
2020-07-21 12:09:30 +00:00
|
|
|
() => {
|
|
|
|
expect(createFlash).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('receivePackagesListSuccess', () => {
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should set received packages', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
const data = 'foo';
|
|
|
|
|
|
|
|
testAction(
|
|
|
|
actions.receivePackagesListSuccess,
|
|
|
|
{ data, headers },
|
|
|
|
null,
|
|
|
|
[
|
|
|
|
{ type: types.SET_PACKAGE_LIST_SUCCESS, payload: data },
|
|
|
|
{ type: types.SET_PAGINATION, payload: headers },
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setInitialState', () => {
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should commit setInitialState', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
testAction(
|
|
|
|
actions.setInitialState,
|
|
|
|
'1',
|
|
|
|
null,
|
|
|
|
[{ type: types.SET_INITIAL_STATE, payload: '1' }],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setLoading', () => {
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should commit set main loading', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
testAction(
|
|
|
|
actions.setLoading,
|
|
|
|
true,
|
|
|
|
null,
|
|
|
|
[{ type: types.SET_MAIN_LOADING, payload: true }],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('requestDeletePackage', () => {
|
|
|
|
const payload = {
|
|
|
|
_links: {
|
|
|
|
delete_api_path: 'foo',
|
|
|
|
},
|
|
|
|
};
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should perform a delete operation on _links.delete_api_path', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
mock.onDelete(payload._links.delete_api_path).replyOnce(200);
|
|
|
|
Api.projectPackages = jest.fn().mockResolvedValue({ data: 'foo' });
|
|
|
|
|
|
|
|
testAction(
|
|
|
|
actions.requestDeletePackage,
|
|
|
|
payload,
|
|
|
|
{ pagination: { page: 1 } },
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
{ type: 'setLoading', payload: true },
|
|
|
|
{ type: 'requestPackagesList', payload: { page: 1 } },
|
|
|
|
],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should stop the loading and call create flash on api error', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
mock.onDelete(payload._links.delete_api_path).replyOnce(400);
|
|
|
|
testAction(
|
|
|
|
actions.requestDeletePackage,
|
|
|
|
payload,
|
|
|
|
null,
|
|
|
|
[],
|
2020-12-23 12:10:26 +00:00
|
|
|
[
|
|
|
|
{ type: 'setLoading', payload: true },
|
|
|
|
{ type: 'setLoading', payload: false },
|
|
|
|
],
|
2020-07-21 12:09:30 +00:00
|
|
|
() => {
|
|
|
|
expect(createFlash).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
property | actionPayload
|
|
|
|
${'_links'} | ${{}}
|
|
|
|
${'delete_api_path'} | ${{ _links: {} }}
|
|
|
|
`('should reject and createFlash when $property is missing', ({ actionPayload }, done) => {
|
2020-12-24 00:10:25 +00:00
|
|
|
testAction(actions.requestDeletePackage, actionPayload, null, [], []).catch((e) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
expect(e).toEqual(new Error(MISSING_DELETE_PATH_ERROR));
|
|
|
|
expect(createFlash).toHaveBeenCalledWith(DELETE_PACKAGE_ERROR_MESSAGE);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setSorting', () => {
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should commit SET_SORTING', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
testAction(
|
|
|
|
actions.setSorting,
|
|
|
|
'foo',
|
|
|
|
null,
|
|
|
|
[{ type: types.SET_SORTING, payload: 'foo' }],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setFilter', () => {
|
2020-12-24 00:10:25 +00:00
|
|
|
it('should commit SET_FILTER', (done) => {
|
2020-07-21 12:09:30 +00:00
|
|
|
testAction(
|
|
|
|
actions.setFilter,
|
|
|
|
'foo',
|
|
|
|
null,
|
|
|
|
[{ type: types.SET_FILTER, payload: 'foo' }],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|