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

398 lines
10 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2020-10-24 23:57:45 +05:30
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
2021-03-11 19:13:27 +05:30
import testAction from 'helpers/vuex_action_helper';
import api from '~/api';
import service from '~/ide/services';
2020-06-23 00:09:42 +05:30
import { createStore } from '~/ide/stores';
2018-10-15 14:42:47 +05:30
import {
refreshLastCommitData,
2018-11-08 19:23:39 +05:30
showBranchNotFoundError,
createNewBranchFromDefault,
2020-04-22 19:07:51 +05:30
loadEmptyBranch,
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';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2018-10-15 14:42:47 +05:30
2020-04-22 19:07:51 +05:30
const TEST_PROJECT_ID = 'abc/def';
2018-10-15 14:42:47 +05:30
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);
2020-04-22 19:07:51 +05:30
store.state.projects[TEST_PROJECT_ID] = {
2018-11-08 19:23:39 +05:30
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
});
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,
},
},
};
2020-06-23 00:09:42 +05:30
jest.spyOn(service, 'getBranchData').mockResolvedValue({
data: {
commit: { id: '123' },
},
});
2018-11-08 19:23:39 +05:30
});
2018-10-15 14:42:47 +05:30
2021-03-08 18:12:59 +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);
});
2021-03-08 18:12:59 +05:30
it('commits getBranchData', (done) => {
2018-10-15 14:42:47 +05:30
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: {
2020-04-22 19:07:51 +05:30
projectId: TEST_PROJECT_ID,
2018-11-08 19:23:39 +05:30
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', () => {
2021-03-08 18:12:59 +05:30
it('dispatches setErrorMessage', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
showBranchNotFoundError,
'master',
null,
[],
[
{
type: 'setErrorMessage',
payload: {
text: "Branch <strong>master</strong> was not found in this project's repository.",
2020-06-23 00:09:42 +05:30
action: expect.any(Function),
2018-11-08 19:23:39 +05:30
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', () => {
2020-06-23 00:09:42 +05:30
useMockLocationHelper();
beforeEach(() => {
jest.spyOn(api, 'createBranch').mockResolvedValue();
});
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
it('calls API', (done) => {
2018-11-08 19:23:39 +05:30
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);
});
2021-03-08 18:12:59 +05:30
it('clears error message', (done) => {
2020-06-23 00:09:42 +05:30
const dispatchSpy = jest.fn().mockName('dispatch');
2018-11-08 19:23:39 +05:30
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);
});
2021-03-08 18:12:59 +05:30
it('reloads window', (done) => {
2018-11-08 19:23:39 +05:30
createNewBranchFromDefault(
{
state: {
currentProjectId: 'project-path',
},
getters: {
currentProject: {
default_branch: 'master',
},
},
dispatch() {},
},
'new-branch-name',
)
.then(() => {
2020-06-23 00:09:42 +05:30
expect(window.location.reload).toHaveBeenCalled();
2018-11-08 19:23:39 +05:30
})
.then(done)
.catch(done.fail);
});
});
2020-04-22 19:07:51 +05:30
describe('loadEmptyBranch', () => {
2021-03-08 18:12:59 +05:30
it('creates a blank tree and sets loading state to false', (done) => {
2019-09-04 21:01:54 +05:30
testAction(
2020-04-22 19:07:51 +05:30
loadEmptyBranch,
{ projectId: TEST_PROJECT_ID, branchId: 'master' },
2019-09-04 21:01:54 +05:30
store.state,
[
2020-04-22 19:07:51 +05:30
{ type: 'CREATE_TREE', payload: { treePath: `${TEST_PROJECT_ID}/master` } },
2018-11-08 19:23:39 +05:30
{
2019-09-04 21:01:54 +05:30
type: 'TOGGLE_LOADING',
2020-04-22 19:07:51 +05:30
payload: { entry: store.state.trees[`${TEST_PROJECT_ID}/master`], forceValue: false },
2018-11-08 19:23:39 +05:30
},
2019-09-04 21:01:54 +05:30
],
2020-06-23 00:09:42 +05:30
expect.any(Object),
2020-03-13 15:44:24 +05:30
done,
);
});
2021-03-08 18:12:59 +05:30
it('does nothing, if tree already exists', (done) => {
2020-04-22 19:07:51 +05:30
const trees = { [`${TEST_PROJECT_ID}/master`]: [] };
2020-03-13 15:44:24 +05:30
testAction(
2020-04-22 19:07:51 +05:30
loadEmptyBranch,
{ projectId: TEST_PROJECT_ID, branchId: 'master' },
{ trees },
[],
[],
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 },
},
});
2020-06-23 00:09:42 +05:30
jest.spyOn(store, 'dispatch').mockImplementation();
2019-12-21 20:55:43 +05:30
});
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/' });
2020-06-23 00:09:42 +05:30
expect(store.dispatch).not.toHaveBeenCalledWith('handleTreeEntryAction', expect.anything());
2019-12-21 20:55:43 +05:30
});
it('creates a new temp file supplied via URL if the file does not exist yet', () => {
loadFile(store, { basePath: 'not-existent.md' });
2020-06-23 00:09:42 +05:30
expect(store.dispatch.mock.calls).toHaveLength(1);
2019-12-21 20:55:43 +05:30
2020-06-23 00:09:42 +05:30
expect(store.dispatch).not.toHaveBeenCalledWith('handleTreeEntryAction', expect.anything());
2019-12-21 20:55:43 +05:30
expect(store.dispatch).toHaveBeenCalledWith('createTempEntry', {
name: 'not-existent.md',
type: 'blob',
});
});
});
describe('loadBranch', () => {
2020-04-22 19:07:51 +05:30
const projectId = TEST_PROJECT_ID;
2019-12-21 20:55:43 +05:30
const branchId = '123-lorem';
2020-03-13 15:44:24 +05:30
const ref = 'abcd2322';
2019-12-21 20:55:43 +05:30
2021-03-08 18:12:59 +05:30
it('when empty repo, loads empty branch', (done) => {
2020-04-22 19:07:51 +05:30
const mockGetters = { emptyRepo: true };
testAction(
loadBranch,
{ projectId, branchId },
{ ...store.state, ...mockGetters },
[],
[{ type: 'loadEmptyBranch', payload: { projectId, branchId } }],
done,
);
});
2021-03-08 18:12:59 +05:30
it('when branch already exists, does nothing', (done) => {
2020-04-22 19:07:51 +05:30
store.state.projects[projectId].branches[branchId] = {};
testAction(loadBranch, { projectId, branchId }, store.state, [], [], done);
});
2021-03-08 18:12:59 +05:30
it('fetches branch data', (done) => {
2020-03-13 15:44:24 +05:30
const mockGetters = { findBranch: () => ({ commit: { id: ref } }) };
2020-06-23 00:09:42 +05:30
jest.spyOn(store, 'dispatch').mockResolvedValue();
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
loadBranch(
{ getters: mockGetters, state: store.state, dispatch: store.dispatch },
{ projectId, branchId },
)
2019-12-21 20:55:43 +05:30
.then(() => {
2020-06-23 00:09:42 +05:30
expect(store.dispatch.mock.calls).toEqual([
2019-12-21 20:55:43 +05:30
['getBranchData', { projectId, branchId }],
['getMergeRequestsForBranch', { projectId, branchId }],
2020-03-13 15:44:24 +05:30
['getFiles', { projectId, branchId, ref }],
2019-12-21 20:55:43 +05:30
]);
})
.then(done)
.catch(done.fail);
});
2021-03-08 18:12:59 +05:30
it('shows an error if branch can not be fetched', (done) => {
2020-06-23 00:09:42 +05:30
jest.spyOn(store, 'dispatch').mockReturnValue(Promise.reject());
2019-12-21 20:55:43 +05:30
loadBranch(store, { projectId, branchId })
.then(done.fail)
.catch(() => {
2020-06-23 00:09:42 +05:30
expect(store.dispatch.mock.calls).toEqual([
2019-12-21 20:55:43 +05:30
['getBranchData', { projectId, branchId }],
['showBranchNotFoundError', branchId],
]);
done();
});
});
});
2018-11-20 20:47:30 +05:30
describe('openBranch', () => {
2020-04-22 19:07:51 +05:30
const projectId = TEST_PROJECT_ID;
2019-12-21 20:55:43 +05:30
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 },
},
});
});
2019-09-04 21:01:54 +05:30
describe('existing branch', () => {
beforeEach(() => {
2020-06-23 00:09:42 +05:30
jest.spyOn(store, 'dispatch').mockResolvedValue();
2019-09-04 21:01:54 +05:30
});
2021-03-08 18:12:59 +05:30
it('dispatches branch actions', (done) => {
2019-09-04 21:01:54 +05:30
openBranch(store, branch)
.then(() => {
2020-06-23 00:09:42 +05:30
expect(store.dispatch.mock.calls).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(() => {
2020-06-23 00:09:42 +05:30
jest.spyOn(store, 'dispatch').mockReturnValue(Promise.reject());
2019-09-04 21:01:54 +05:30
});
2019-07-07 11:18:12 +05:30
2021-03-08 18:12:59 +05:30
it('dispatches correct branch actions', (done) => {
2019-09-04 21:01:54 +05:30
openBranch(store, branch)
2021-03-08 18:12:59 +05:30
.then((val) => {
2020-06-23 00:09:42 +05:30
expect(store.dispatch.mock.calls).toEqual([
2019-12-21 20:55:43 +05:30
['setCurrentBranchId', branchId],
['loadBranch', { projectId, branchId }],
2019-09-04 21:01:54 +05:30
]);
2020-04-22 19:07:51 +05:30
expect(val).toEqual(
new Error(
`An error occurred while getting files for - <strong>${projectId}/${branchId}</strong>`,
),
);
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
});