gitlab-org--gitlab-foss/spec/javascripts/ide/stores/actions/tree_spec.js

201 lines
5 KiB
JavaScript
Raw Normal View History

import Vue from 'vue';
2018-06-26 11:50:13 -04:00
import testAction from 'spec/helpers/vuex_action_helper';
import { showTreeEntry } from '~/ide/stores/actions/tree';
import * as types from '~/ide/stores/mutation_types';
2018-03-20 10:16:38 -04:00
import store from '~/ide/stores';
import service from '~/ide/services';
import router from '~/ide/ide_router';
2018-06-26 11:50:13 -04:00
import { file, resetStore, createEntriesFromPaths } from '../../helpers';
describe('Multi-file store tree actions', () => {
let projectTree;
const basicCallParameters = {
endpoint: 'rootEndpoint',
projectId: 'abcproject',
branch: 'master',
branchId: 'master',
};
beforeEach(() => {
spyOn(router, 'push');
store.state.currentProjectId = 'abcproject';
store.state.currentBranchId = 'master';
store.state.projects.abcproject = {
web_url: '',
branches: {
master: {
workingReference: '1',
},
},
};
});
afterEach(() => {
resetStore(store);
});
describe('getFiles', () => {
beforeEach(() => {
2018-03-20 10:16:38 -04:00
spyOn(service, 'getFiles').and.returnValue(
Promise.resolve({
json: () =>
Promise.resolve([
'file.txt',
'folder/fileinfolder.js',
'folder/subfolder/fileinsubfolder.js',
]),
}),
);
});
2018-03-20 10:16:38 -04:00
it('calls service getFiles', done => {
store
.dispatch('getFiles', basicCallParameters)
.then(() => {
expect(service.getFiles).toHaveBeenCalledWith('', 'master');
2018-03-20 10:16:38 -04:00
done();
})
.catch(done.fail);
});
2018-03-20 10:16:38 -04:00
it('adds data into tree', done => {
store
.dispatch('getFiles', basicCallParameters)
.then(() => {
projectTree = store.state.trees['abcproject/master'];
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');
2018-03-26 08:18:24 -04:00
expect(projectTree.tree[0].tree[0].tree[0].name).toBe('fileinsubfolder.js');
done();
2018-03-20 10:16:38 -04:00
})
.catch(done.fail);
});
});
describe('toggleTreeOpen', () => {
let tree;
beforeEach(() => {
tree = file('testing', '1', 'tree');
store.state.entries[tree.path] = tree;
});
2018-03-20 10:16:38 -04:00
it('toggles the tree open', done => {
store
.dispatch('toggleTreeOpen', tree.path)
.then(() => {
expect(tree.opened).toBeTruthy();
2018-03-20 10:16:38 -04:00
done();
})
.catch(done.fail);
});
});
2018-06-26 11:50:13 -04:00
describe('showTreeEntry', () => {
beforeEach(() => {
const paths = [
'grandparent',
'ancestor',
'grandparent/parent',
'grandparent/aunt',
'grandparent/parent/child.txt',
'grandparent/aunt/cousing.txt',
];
Object.assign(store.state.entries, createEntriesFromPaths(paths));
});
it('opens the parents', done => {
testAction(
showTreeEntry,
'grandparent/parent/child.txt',
store.state,
[
{ type: types.SET_TREE_OPEN, payload: 'grandparent/parent' },
{ type: types.SET_TREE_OPEN, payload: 'grandparent' },
],
[
{ type: 'showTreeEntry' },
],
done,
);
});
});
describe('getLastCommitData', () => {
beforeEach(() => {
2018-03-20 10:16:38 -04:00
spyOn(service, 'getTreeLastCommit').and.returnValue(
Promise.resolve({
headers: {
'more-logs-url': null,
},
2018-03-20 10:16:38 -04:00
json: () =>
Promise.resolve([
{
type: 'tree',
file_name: 'testing',
commit: {
message: 'commit message',
authored_date: '123',
},
},
]),
}),
);
store.state.trees['abcproject/mybranch'] = {
tree: [],
};
projectTree = store.state.trees['abcproject/mybranch'];
projectTree.tree.push(file('testing', '1', 'tree'));
projectTree.lastCommitPath = 'lastcommitpath';
});
2018-03-20 10:16:38 -04:00
it('calls service with lastCommitPath', done => {
store
.dispatch('getLastCommitData', projectTree)
.then(() => {
2018-03-26 08:18:24 -04:00
expect(service.getTreeLastCommit).toHaveBeenCalledWith('lastcommitpath');
done();
2018-03-20 10:16:38 -04:00
})
.catch(done.fail);
});
2018-03-20 10:16:38 -04:00
it('updates trees last commit data', done => {
store
.dispatch('getLastCommitData', projectTree)
.then(Vue.nextTick)
.then(() => {
expect(projectTree.tree[0].lastCommit.message).toBe('commit message');
done();
2018-03-20 10:16:38 -04:00
})
.catch(done.fail);
});
2018-03-20 10:16:38 -04:00
it('does not update entry if not found', done => {
projectTree.tree[0].name = 'a';
2018-03-20 10:16:38 -04:00
store
.dispatch('getLastCommitData', projectTree)
.then(Vue.nextTick)
.then(() => {
2018-03-26 08:18:24 -04:00
expect(projectTree.tree[0].lastCommit.message).not.toBe('commit message');
done();
2018-03-20 10:16:38 -04:00
})
.catch(done.fail);
});
});
});