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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

196 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-08-13 15:12:31 +05:30
import { stubPerformanceWebAPI } from 'helpers/performance';
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(() => {
2022-08-13 15:12:31 +05:30
stubPerformanceWebAPI();
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
});
2022-06-21 17:19:12 +05:30
it('adds data into tree', async () => {
await store.dispatch('getFiles', basicCallParameters);
projectTree = store.state.trees['abcproject/main'];
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');
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
});
2018-11-08 19:23:39 +05:30
describe('error', () => {
2022-06-21 17:19:12 +05:30
it('dispatches error action', async () => {
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);
2022-06-21 17:19:12 +05:30
await expect(
getFiles(
{
commit() {},
dispatch,
state: store.state,
getters,
},
{
projectId: 'abc/def',
branchId: 'main-testing',
},
),
).rejects.toEqual(new Error('Request failed with status code 500'));
expect(dispatch).toHaveBeenCalledWith('setErrorMessage', {
text: 'An error occurred while loading all the files.',
action: expect.any(Function),
actionText: 'Please try again',
actionPayload: { projectId: 'abc/def', branchId: 'main-testing' },
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
});
});
describe('toggleTreeOpen', () => {
let tree;
beforeEach(() => {
tree = file('testing', '1', 'tree');
store.state.entries[tree.path] = tree;
});
2022-06-21 17:19:12 +05:30
it('toggles the tree open', async () => {
await store.dispatch('toggleTreeOpen', tree.path);
2022-08-27 11:52:29 +05:30
expect(tree.opened).toBe(true);
2018-05-09 12:01:36 +05:30
});
});
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
});
2022-06-21 17:19:12 +05:30
it('opens the parents', () => {
return testAction(
2018-11-08 19:23:39 +05:30
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
);
2018-05-09 12:01:36 +05:30
});
});
2019-07-07 11:18:12 +05:30
describe('setDirectoryData', () => {
2022-06-21 17:19:12 +05:30
it('sets tree correctly if there are no opened files yet', () => {
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
2022-06-21 17:19:12 +05:30
return testAction(
2019-07-07 11:18:12 +05:30
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,
},
},
],
[],
);
});
});
2018-05-09 12:01:36 +05:30
});