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

491 lines
13 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';
2022-01-26 12:08:38 +05:30
import createFlash from '~/flash';
2021-03-11 19:13:27 +05:30
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 {
2022-01-26 12:08:38 +05:30
setProject,
fetchProjectPermissions,
2018-10-15 14:42:47 +05:30
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';
2022-01-26 12:08:38 +05:30
import { logError } from '~/lib/logger';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2018-10-15 14:42:47 +05:30
2022-01-26 12:08:38 +05:30
jest.mock('~/flash');
jest.mock('~/lib/logger');
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
});
2022-01-26 12:08:38 +05:30
describe('setProject', () => {
const project = { id: 'foo', path_with_namespace: TEST_PROJECT_ID };
const baseMutations = [
{
type: 'SET_PROJECT',
payload: {
projectPath: TEST_PROJECT_ID,
project,
},
},
{
type: 'SET_CURRENT_PROJECT',
payload: TEST_PROJECT_ID,
},
];
it.each`
desc | payload | expectedMutations
${'does not commit any action if project is not passed'} | ${undefined} | ${[]}
${'commits correct actions in the correct order by default'} | ${{ project }} | ${[...baseMutations]}
`('$desc', async ({ payload, expectedMutations } = {}) => {
await testAction({
action: setProject,
payload,
state: store.state,
expectedMutations,
expectedActions: [],
});
});
});
describe('fetchProjectPermissions', () => {
const permissionsData = {
userPermissions: {
bogus: true,
},
};
const permissionsMutations = [
{
type: 'UPDATE_PROJECT',
payload: {
projectPath: TEST_PROJECT_ID,
props: {
...permissionsData,
},
},
},
];
let spy;
beforeEach(() => {
spy = jest.spyOn(service, 'getProjectPermissionsData');
});
afterEach(() => {
createFlash.mockRestore();
});
it.each`
desc | projectPath | responseSuccess | expectedMutations
${'does not fetch permissions if project does not exist'} | ${undefined} | ${true} | ${[]}
${'fetches permission when project is specified'} | ${TEST_PROJECT_ID} | ${true} | ${[...permissionsMutations]}
${'flashes an error if the request fails'} | ${TEST_PROJECT_ID} | ${false} | ${[]}
`('$desc', async ({ projectPath, expectedMutations, responseSuccess } = {}) => {
store.state.currentProjectId = projectPath;
if (responseSuccess) {
spy.mockResolvedValue(permissionsData);
} else {
spy.mockRejectedValue();
}
await testAction({
action: fetchProjectPermissions,
state: store.state,
expectedMutations,
expectedActions: [],
});
if (!responseSuccess) {
expect(logError).toHaveBeenCalled();
expect(createFlash).toHaveBeenCalled();
}
});
});
2018-10-15 14:42:47 +05:30
describe('refreshLastCommitData', () => {
beforeEach(() => {
2018-11-08 19:23:39 +05:30
store.state.currentProjectId = 'abc/def';
2021-09-04 01:27:46 +05:30
store.state.currentBranchId = 'main';
2018-11-08 19:23:39 +05:30
store.state.projects['abc/def'] = {
id: 4,
2018-10-15 14:42:47 +05:30
branches: {
2021-09-04 01:27:46 +05:30
main: {
2018-10-15 14:42:47 +05:30
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(() => {
2021-09-04 01:27:46 +05:30
expect(service.getBranchData).toHaveBeenCalledWith('abc/def', 'main');
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,
2021-09-04 01:27:46 +05:30
branchId: 'main',
2018-11-08 19:23:39 +05:30
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,
2021-09-04 01:27:46 +05:30
'main',
2018-11-08 19:23:39 +05:30
null,
[],
[
{
type: 'setErrorMessage',
payload: {
2021-09-04 01:27:46 +05:30
text: "Branch <strong>main</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',
2021-09-04 01:27:46 +05:30
actionPayload: 'main',
2018-11-08 19:23:39 +05:30
},
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: {
2021-09-04 01:27:46 +05:30
default_branch: 'main',
2018-11-08 19:23:39 +05:30
},
},
dispatch() {},
},
'new-branch-name',
)
.then(() => {
expect(api.createBranch).toHaveBeenCalledWith('project-path', {
2021-09-04 01:27:46 +05:30
ref: 'main',
2018-11-08 19:23:39 +05:30
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: {
2021-09-04 01:27:46 +05:30
default_branch: 'main',
2018-11-08 19:23:39 +05:30
},
},
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: {
2021-09-04 01:27:46 +05:30
default_branch: 'main',
2018-11-08 19:23:39 +05:30
},
},
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,
2021-09-04 01:27:46 +05:30
{ projectId: TEST_PROJECT_ID, branchId: 'main' },
2019-09-04 21:01:54 +05:30
store.state,
[
2021-09-04 01:27:46 +05:30
{ type: 'CREATE_TREE', payload: { treePath: `${TEST_PROJECT_ID}/main` } },
2018-11-08 19:23:39 +05:30
{
2019-09-04 21:01:54 +05:30
type: 'TOGGLE_LOADING',
2021-09-04 01:27:46 +05:30
payload: { entry: store.state.trees[`${TEST_PROJECT_ID}/main`], 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) => {
2021-09-04 01:27:46 +05:30
const trees = { [`${TEST_PROJECT_ID}/main`]: [] };
2020-04-22 19:07:51 +05:30
2020-03-13 15:44:24 +05:30
testAction(
2020-04-22 19:07:51 +05:30
loadEmptyBranch,
2021-09-04 01:27:46 +05:30
{ projectId: TEST_PROJECT_ID, branchId: 'main' },
2020-04-22 19:07:51 +05:30
{ 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
});