debian-mirror-gitlab/spec/frontend/ide/stores/actions/tree_spec.js

209 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-08 18:12:59 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2021-03-11 19:13:27 +05:30
import testAction from 'helpers/vuex_action_helper';
import { createRouter } from '~/ide/ide_router';
import service from '~/ide/services';
import { createStore } from '~/ide/stores';
2019-07-07 11:18:12 +05:30
import { showTreeEntry, getFiles, setDirectoryData } from '~/ide/stores/actions/tree';
2018-11-08 19:23:39 +05:30
import * as types from '~/ide/stores/mutation_types';
import axios from '~/lib/utils/axios_utils';
2020-06-23 00:09:42 +05:30
import { file, createEntriesFromPaths } from '../../helpers';
2018-05-09 12:01:36 +05:30
describe('Multi-file store tree actions', () => {
let projectTree;
2018-11-08 19:23:39 +05:30
let mock;
2020-06-23 00:09:42 +05:30
let store;
let router;
2018-05-09 12:01:36 +05:30
const basicCallParameters = {
endpoint: 'rootEndpoint',
projectId: 'abcproject',
2021-09-04 01:27:46 +05:30
branch: 'main',
branchId: 'main',
2020-03-13 15:44:24 +05:30
ref: '12345678',
2018-05-09 12:01:36 +05:30
};
beforeEach(() => {
2020-06-23 00:09:42 +05:30
store = createStore();
router = createRouter(store);
jest.spyOn(router, 'push').mockImplementation();
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
mock = new MockAdapter(axios);
2018-05-09 12:01:36 +05:30
store.state.currentProjectId = 'abcproject';
2021-09-04 01:27:46 +05:30
store.state.currentBranchId = 'main';
2018-05-09 12:01:36 +05:30
store.state.projects.abcproject = {
web_url: '',
2020-05-24 23:13:21 +05:30
path_with_namespace: 'foo/abcproject',
2018-05-09 12:01:36 +05:30
};
});
afterEach(() => {
2018-11-08 19:23:39 +05:30
mock.restore();
2018-05-09 12:01:36 +05:30
});
describe('getFiles', () => {
2018-11-08 19:23:39 +05:30
describe('success', () => {
beforeEach(() => {
2020-06-23 00:09:42 +05:30
jest.spyOn(service, 'getFiles');
2018-11-08 19:23:39 +05:30
mock
.onGet(/(.*)/)
.replyOnce(200, [
'file.txt',
'folder/fileinfolder.js',
'folder/subfolder/fileinsubfolder.js',
]);
});
2020-06-23 00:09:42 +05:30
it('calls service getFiles', () => {
2021-03-08 18:12:59 +05:30
return store.dispatch('getFiles', basicCallParameters).then(() => {
expect(service.getFiles).toHaveBeenCalledWith('foo/abcproject', '12345678');
});
2018-11-08 19:23:39 +05:30
});
2021-03-08 18:12:59 +05:30
it('adds data into tree', (done) => {
2018-11-08 19:23:39 +05:30
store
.dispatch('getFiles', basicCallParameters)
.then(() => {
2021-09-04 01:27:46 +05:30
projectTree = store.state.trees['abcproject/main'];
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(projectTree.tree.length).toBe(2);
expect(projectTree.tree[0].type).toBe('tree');
expect(projectTree.tree[0].tree[1].name).toBe('fileinfolder.js');
expect(projectTree.tree[1].type).toBe('blob');
expect(projectTree.tree[0].tree[0].tree[0].type).toBe('blob');
expect(projectTree.tree[0].tree[0].tree[0].name).toBe('fileinsubfolder.js');
done();
})
.catch(done.fail);
});
2018-05-09 12:01:36 +05:30
});
2018-11-08 19:23:39 +05:30
describe('error', () => {
2021-03-08 18:12:59 +05:30
it('dispatches error action', (done) => {
2020-06-23 00:09:42 +05:30
const dispatch = jest.fn();
2018-11-08 19:23:39 +05:30
store.state.projects = {
'abc/def': {
2020-07-28 23:09:34 +05:30
web_url: `${TEST_HOST}/files`,
2019-12-26 22:10:19 +05:30
branches: {
2021-09-04 01:27:46 +05:30
'main-testing': {
2019-12-26 22:10:19 +05:30
commit: {
id: '12345',
},
},
},
2018-11-08 19:23:39 +05:30
},
};
2019-12-26 22:10:19 +05:30
const getters = {
2021-09-04 01:27:46 +05:30
findBranch: () => store.state.projects['abc/def'].branches['main-testing'],
2019-12-26 22:10:19 +05:30
};
2018-11-08 19:23:39 +05:30
mock.onGet(/(.*)/).replyOnce(500);
getFiles(
{
commit() {},
dispatch,
state: store.state,
2019-12-26 22:10:19 +05:30
getters,
2018-11-08 19:23:39 +05:30
},
{
projectId: 'abc/def',
2021-09-04 01:27:46 +05:30
branchId: 'main-testing',
2018-11-08 19:23:39 +05:30
},
)
.then(done.fail)
.catch(() => {
expect(dispatch).toHaveBeenCalledWith('setErrorMessage', {
2020-03-13 15:44:24 +05:30
text: 'An error occurred while loading all the files.',
2020-06-23 00:09:42 +05:30
action: expect.any(Function),
2018-11-08 19:23:39 +05:30
actionText: 'Please try again',
2021-09-04 01:27:46 +05:30
actionPayload: { projectId: 'abc/def', branchId: 'main-testing' },
2018-11-08 19:23:39 +05:30
});
done();
});
});
2018-05-09 12:01:36 +05:30
});
});
describe('toggleTreeOpen', () => {
let tree;
beforeEach(() => {
tree = file('testing', '1', 'tree');
store.state.entries[tree.path] = tree;
});
2021-03-08 18:12:59 +05:30
it('toggles the tree open', (done) => {
2018-05-09 12:01:36 +05:30
store
.dispatch('toggleTreeOpen', tree.path)
.then(() => {
expect(tree.opened).toBeTruthy();
done();
})
.catch(done.fail);
});
});
2018-11-08 19:23:39 +05:30
describe('showTreeEntry', () => {
2018-05-09 12:01:36 +05:30
beforeEach(() => {
2018-11-08 19:23:39 +05:30
const paths = [
'grandparent',
'ancestor',
'grandparent/parent',
'grandparent/aunt',
'grandparent/parent/child.txt',
'grandparent/aunt/cousing.txt',
];
Object.assign(store.state.entries, createEntriesFromPaths(paths));
2018-05-09 12:01:36 +05:30
});
2021-03-08 18:12:59 +05:30
it('opens the parents', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
showTreeEntry,
'grandparent/parent/child.txt',
store.state,
2018-11-18 11:00:15 +05:30
[{ type: types.SET_TREE_OPEN, payload: 'grandparent/parent' }],
[{ type: 'showTreeEntry', payload: 'grandparent/parent' }],
2018-11-08 19:23:39 +05:30
done,
);
2018-05-09 12:01:36 +05:30
});
});
2019-07-07 11:18:12 +05:30
describe('setDirectoryData', () => {
2021-03-08 18:12:59 +05:30
it('sets tree correctly if there are no opened files yet', (done) => {
2019-07-07 11:18:12 +05:30
const treeFile = file({ name: 'README.md' });
2021-09-04 01:27:46 +05:30
store.state.trees['abcproject/main'] = {};
2019-07-07 11:18:12 +05:30
testAction(
setDirectoryData,
2021-09-04 01:27:46 +05:30
{ projectId: 'abcproject', branchId: 'main', treeList: [treeFile] },
2019-07-07 11:18:12 +05:30
store.state,
[
{
type: types.SET_DIRECTORY_DATA,
payload: {
2021-09-04 01:27:46 +05:30
treePath: 'abcproject/main',
2019-07-07 11:18:12 +05:30
data: [treeFile],
},
},
{
type: types.TOGGLE_LOADING,
payload: {
entry: {},
forceValue: false,
},
},
],
[],
done,
);
});
});
2018-05-09 12:01:36 +05:30
});