debian-mirror-gitlab/spec/javascripts/ide/stores/actions/project_spec.js

425 lines
10 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
2018-10-15 14:42:47 +05:30
import {
refreshLastCommitData,
2018-11-08 19:23:39 +05:30
showBranchNotFoundError,
createNewBranchFromDefault,
2019-09-04 21:01:54 +05:30
showEmptyState,
2018-11-20 20:47:30 +05:30
openBranch,
2019-12-21 20:55:43 +05:30
loadFile,
loadBranch,
2018-10-15 14:42:47 +05:30
} from '~/ide/stores/actions';
2019-12-21 20:55:43 +05:30
import { createStore } from '~/ide/stores';
2018-10-15 14:42:47 +05:30
import service from '~/ide/services';
2018-11-08 19:23:39 +05:30
import api from '~/api';
import router from '~/ide/ide_router';
2018-10-15 14:42:47 +05:30
import { resetStore } from '../../helpers';
import testAction from '../../../helpers/vuex_action_helper';
describe('IDE store project actions', () => {
2018-11-08 19:23:39 +05:30
let mock;
2019-12-21 20:55:43 +05:30
let store;
2018-11-08 19:23:39 +05:30
2018-10-15 14:42:47 +05:30
beforeEach(() => {
2019-12-21 20:55:43 +05:30
store = createStore();
2018-11-08 19:23:39 +05:30
mock = new MockAdapter(axios);
store.state.projects['abc/def'] = {
branches: {},
};
2018-10-15 14:42:47 +05:30
});
afterEach(() => {
2018-11-08 19:23:39 +05:30
mock.restore();
2018-10-15 14:42:47 +05:30
resetStore(store);
});
describe('refreshLastCommitData', () => {
beforeEach(() => {
2018-11-08 19:23:39 +05:30
store.state.currentProjectId = 'abc/def';
2018-10-15 14:42:47 +05:30
store.state.currentBranchId = 'master';
2018-11-08 19:23:39 +05:30
store.state.projects['abc/def'] = {
id: 4,
2018-10-15 14:42:47 +05:30
branches: {
master: {
commit: null,
},
},
};
spyOn(service, 'getBranchData').and.returnValue(
Promise.resolve({
data: {
commit: { id: '123' },
},
}),
);
2018-11-08 19:23:39 +05:30
});
2018-10-15 14:42:47 +05:30
2018-11-08 19:23:39 +05:30
it('calls the service', done => {
2018-10-15 14:42:47 +05:30
store
.dispatch('refreshLastCommitData', {
projectId: store.state.currentProjectId,
branchId: store.state.currentBranchId,
})
.then(() => {
2018-11-08 19:23:39 +05:30
expect(service.getBranchData).toHaveBeenCalledWith('abc/def', 'master');
2018-10-15 14:42:47 +05:30
done();
})
.catch(done.fail);
});
it('commits getBranchData', done => {
testAction(
refreshLastCommitData,
2018-11-08 19:23:39 +05:30
{
projectId: store.state.currentProjectId,
branchId: store.state.currentBranchId,
},
store.state,
2018-11-18 11:00:15 +05:30
// mutations
2018-11-08 19:23:39 +05:30
[
{
type: 'SET_BRANCH_COMMIT',
payload: {
projectId: 'abc/def',
branchId: 'master',
commit: { id: '123' },
},
},
2018-11-18 11:00:15 +05:30
],
// action
[],
2018-11-08 19:23:39 +05:30
done,
);
});
});
describe('showBranchNotFoundError', () => {
it('dispatches setErrorMessage', done => {
testAction(
showBranchNotFoundError,
'master',
null,
[],
[
{
type: 'setErrorMessage',
payload: {
text: "Branch <strong>master</strong> was not found in this project's repository.",
action: jasmine.any(Function),
actionText: 'Create branch',
actionPayload: 'master',
},
2018-10-15 14:42:47 +05:30
},
2018-11-08 19:23:39 +05:30
],
2018-10-15 14:42:47 +05:30
done,
);
});
});
2018-11-08 19:23:39 +05:30
describe('createNewBranchFromDefault', () => {
it('calls API', done => {
spyOn(api, 'createBranch').and.returnValue(Promise.resolve());
spyOn(router, 'push');
createNewBranchFromDefault(
{
state: {
currentProjectId: 'project-path',
},
getters: {
currentProject: {
default_branch: 'master',
},
},
dispatch() {},
},
'new-branch-name',
)
.then(() => {
expect(api.createBranch).toHaveBeenCalledWith('project-path', {
ref: 'master',
branch: 'new-branch-name',
});
})
.then(done)
.catch(done.fail);
});
it('clears error message', done => {
const dispatchSpy = jasmine.createSpy('dispatch');
spyOn(api, 'createBranch').and.returnValue(Promise.resolve());
spyOn(router, 'push');
createNewBranchFromDefault(
{
state: {
currentProjectId: 'project-path',
},
getters: {
currentProject: {
default_branch: 'master',
},
},
dispatch: dispatchSpy,
},
'new-branch-name',
)
.then(() => {
expect(dispatchSpy).toHaveBeenCalledWith('setErrorMessage', null);
})
.then(done)
.catch(done.fail);
});
it('reloads window', done => {
spyOn(api, 'createBranch').and.returnValue(Promise.resolve());
spyOn(router, 'push');
createNewBranchFromDefault(
{
state: {
currentProjectId: 'project-path',
},
getters: {
currentProject: {
default_branch: 'master',
},
},
dispatch() {},
},
'new-branch-name',
)
.then(() => {
expect(router.push).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
});
});
2019-09-04 21:01:54 +05:30
describe('showEmptyState', () => {
it('commits proper mutations when supplied error is 404', done => {
testAction(
showEmptyState,
{
err: {
response: {
status: 404,
},
},
projectId: 'abc/def',
branchId: 'master',
},
store.state,
[
2018-11-08 19:23:39 +05:30
{
2019-09-04 21:01:54 +05:30
type: 'CREATE_TREE',
payload: {
treePath: 'abc/def/master',
},
2018-11-08 19:23:39 +05:30
},
{
2019-09-04 21:01:54 +05:30
type: 'TOGGLE_LOADING',
payload: {
entry: store.state.trees['abc/def/master'],
forceValue: false,
},
2018-11-08 19:23:39 +05:30
},
2019-09-04 21:01:54 +05:30
],
[],
done,
);
2018-11-08 19:23:39 +05:30
});
});
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
describe('loadFile', () => {
beforeEach(() => {
Object.assign(store.state, {
entries: {
foo: { pending: false },
'foo/bar-pending': { pending: true },
'foo/bar': { pending: false },
},
});
spyOn(store, 'dispatch');
});
it('does nothing, if basePath is not given', () => {
loadFile(store, { basePath: undefined });
expect(store.dispatch).not.toHaveBeenCalled();
});
it('handles tree entry action, if basePath is given and the entry is not pending', () => {
loadFile(store, { basePath: 'foo/bar/' });
expect(store.dispatch).toHaveBeenCalledWith(
'handleTreeEntryAction',
store.state.entries['foo/bar'],
);
});
it('does not handle tree entry action, if entry is pending', () => {
loadFile(store, { basePath: 'foo/bar-pending/' });
expect(store.dispatch).not.toHaveBeenCalledWith('handleTreeEntryAction', jasmine.anything());
});
it('creates a new temp file supplied via URL if the file does not exist yet', () => {
loadFile(store, { basePath: 'not-existent.md' });
expect(store.dispatch.calls.count()).toBe(1);
expect(store.dispatch).not.toHaveBeenCalledWith('handleTreeEntryAction', jasmine.anything());
expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
name: 'not-existent.md',
type: 'blob',
});
});
});
describe('loadBranch', () => {
const projectId = 'abc/def';
const branchId = '123-lorem';
it('fetches branch data', done => {
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
loadBranch(store, { projectId, branchId })
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
['getBranchData', { projectId, branchId }],
['getMergeRequestsForBranch', { projectId, branchId }],
['getFiles', { projectId, branchId }],
]);
})
.then(done)
.catch(done.fail);
});
it('shows an error if branch can not be fetched', done => {
spyOn(store, 'dispatch').and.returnValue(Promise.reject());
loadBranch(store, { projectId, branchId })
.then(done.fail)
.catch(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
['getBranchData', { projectId, branchId }],
['showBranchNotFoundError', branchId],
]);
done();
});
});
});
2018-11-20 20:47:30 +05:30
describe('openBranch', () => {
2019-12-21 20:55:43 +05:30
const projectId = 'abc/def';
const branchId = '123-lorem';
2018-11-20 20:47:30 +05:30
const branch = {
2019-12-21 20:55:43 +05:30
projectId,
branchId,
2018-11-20 20:47:30 +05:30
};
beforeEach(() => {
2019-12-21 20:55:43 +05:30
Object.assign(store.state, {
entries: {
foo: { pending: false },
'foo/bar-pending': { pending: true },
'foo/bar': { pending: false },
},
});
});
it('loads file right away if the branch has already been fetched', done => {
spyOn(store, 'dispatch');
Object.assign(store.state, {
projects: {
[projectId]: {
branches: {
[branchId]: { foo: 'bar' },
},
},
},
});
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([['loadFile', { basePath: undefined }]]);
})
.then(done)
.catch(done.fail);
2018-11-20 20:47:30 +05:30
});
2019-09-04 21:01:54 +05:30
describe('empty repo', () => {
beforeEach(() => {
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
Object.assign(store.state, {
currentProjectId: 'abc/def',
projects: {
'abc/def': {
empty_repo: true,
},
},
});
2019-09-04 21:01:54 +05:30
});
afterEach(() => {
resetStore(store);
});
it('dispatches showEmptyState action right away', done => {
openBranch(store, branch)
.then(() => {
2019-12-21 20:55:43 +05:30
expect(store.dispatch.calls.allArgs()).toEqual([['showEmptyState', branch]]);
2019-09-04 21:01:54 +05:30
done();
})
.catch(done.fail);
});
2018-11-20 20:47:30 +05:30
});
2019-09-04 21:01:54 +05:30
describe('existing branch', () => {
beforeEach(() => {
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
});
it('dispatches branch actions', done => {
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
2019-12-21 20:55:43 +05:30
['setCurrentBranchId', branchId],
['loadBranch', { projectId, branchId }],
['loadFile', { basePath: undefined }],
2019-09-04 21:01:54 +05:30
]);
})
.then(done)
.catch(done.fail);
});
2018-11-20 20:47:30 +05:30
});
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
describe('non-existent branch', () => {
beforeEach(() => {
spyOn(store, 'dispatch').and.returnValue(Promise.reject());
});
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
it('dispatches correct branch actions', done => {
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
2019-12-21 20:55:43 +05:30
['setCurrentBranchId', branchId],
['loadBranch', { projectId, branchId }],
2019-09-04 21:01:54 +05:30
]);
})
.then(done)
.catch(done.fail);
});
2019-07-07 11:18:12 +05:30
});
2018-11-20 20:47:30 +05:30
});
2018-10-15 14:42:47 +05:30
});