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

220 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2020-06-23 00:09:42 +05:30
import testAction from 'helpers/vuex_action_helper';
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 { createStore } from '~/ide/stores';
2018-05-09 12:01:36 +05:30
import service from '~/ide/services';
2020-06-23 00:09:42 +05:30
import { createRouter } from '~/ide/ide_router';
import { file, createEntriesFromPaths } from '../../helpers';
2020-07-28 23:09:34 +05:30
import { TEST_HOST } from 'jest/helpers/test_constants';
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',
branch: 'master',
branchId: 'master',
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';
store.state.currentBranchId = 'master';
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', () => {
return (
store
.dispatch('getFiles', basicCallParameters)
// getFiles actions calls lodash.defer
.then(() => jest.runOnlyPendingTimers())
.then(() => {
expect(service.getFiles).toHaveBeenCalledWith('foo/abcproject', '12345678');
})
);
2018-11-08 19:23:39 +05:30
});
it('adds data into tree', done => {
store
.dispatch('getFiles', basicCallParameters)
2019-07-07 11:18:12 +05:30
.then(() => {
// The populating of the tree is deferred for performance reasons.
2019-12-04 20:38:33 +05:30
// See this merge request for details: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/25700
2020-06-23 00:09:42 +05:30
jest.advanceTimersByTime(1);
2019-07-07 11:18:12 +05:30
})
2018-11-08 19:23:39 +05:30
.then(() => {
projectTree = store.state.trees['abcproject/master'];
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', () => {
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: {
'master-testing': {
commit: {
id: '12345',
},
},
},
2018-11-08 19:23:39 +05:30
},
};
2019-12-26 22:10:19 +05:30
const getters = {
findBranch: () => store.state.projects['abc/def'].branches['master-testing'],
};
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',
branchId: 'master-testing',
},
)
.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',
actionPayload: { projectId: 'abc/def', branchId: 'master-testing' },
});
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;
});
it('toggles the tree open', done => {
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
});
2018-11-08 19:23:39 +05:30
it('opens the parents', done => {
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', () => {
it('sets tree correctly if there are no opened files yet', done => {
const treeFile = file({ name: 'README.md' });
store.state.trees['abcproject/master'] = {};
testAction(
setDirectoryData,
{ projectId: 'abcproject', branchId: 'master', treeList: [treeFile] },
store.state,
[
{
type: types.SET_DIRECTORY_DATA,
payload: {
treePath: 'abcproject/master',
data: [treeFile],
},
},
{
type: types.TOGGLE_LOADING,
payload: {
entry: {},
forceValue: false,
},
},
],
[],
done,
);
});
});
2018-05-09 12:01:36 +05:30
});