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

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

453 lines
12 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';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
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
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2022-01-26 12:08:38 +05:30
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(() => {
2022-11-25 23:54:43 +05:30
createAlert.mockRestore();
2022-01-26 12:08:38 +05:30
});
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]}
2023-05-27 22:25:52 +05:30
${'alerts an error if the request fails'} | ${TEST_PROJECT_ID} | ${false} | ${[]}
2022-01-26 12:08:38 +05:30
`('$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();
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalled();
2022-01-26 12:08:38 +05:30
}
});
});
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
2022-06-21 17:19:12 +05:30
it('calls the service', async () => {
await store.dispatch('refreshLastCommitData', {
projectId: store.state.currentProjectId,
branchId: store.state.currentBranchId,
});
expect(service.getBranchData).toHaveBeenCalledWith('abc/def', 'main');
2018-10-15 14:42:47 +05:30
});
2022-06-21 17:19:12 +05:30
it('commits getBranchData', () => {
return testAction(
2018-10-15 14:42:47 +05:30
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
);
});
});
describe('showBranchNotFoundError', () => {
2022-06-21 17:19:12 +05:30
it('dispatches setErrorMessage', () => {
return testAction(
2018-11-08 19:23:39 +05:30
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
);
});
});
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
2022-06-21 17:19:12 +05:30
it('calls API', async () => {
await createNewBranchFromDefault(
2018-11-08 19:23:39 +05:30
{
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',
2022-06-21 17:19:12 +05:30
);
expect(api.createBranch).toHaveBeenCalledWith('project-path', {
ref: 'main',
branch: 'new-branch-name',
});
2018-11-08 19:23:39 +05:30
});
2022-06-21 17:19:12 +05:30
it('clears error message', async () => {
2020-06-23 00:09:42 +05:30
const dispatchSpy = jest.fn().mockName('dispatch');
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
await createNewBranchFromDefault(
2018-11-08 19:23:39 +05:30
{
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',
2022-06-21 17:19:12 +05:30
);
expect(dispatchSpy).toHaveBeenCalledWith('setErrorMessage', null);
2018-11-08 19:23:39 +05:30
});
2022-06-21 17:19:12 +05:30
it('reloads window', async () => {
await createNewBranchFromDefault(
2018-11-08 19:23:39 +05:30
{
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',
2022-06-21 17:19:12 +05:30
);
expect(window.location.reload).toHaveBeenCalled();
2018-11-08 19:23:39 +05:30
});
});
2020-04-22 19:07:51 +05:30
describe('loadEmptyBranch', () => {
2022-06-21 17:19:12 +05:30
it('creates a blank tree and sets loading state to false', () => {
return 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
);
});
2022-06-21 17:19:12 +05:30
it('does nothing, if tree already exists', () => {
2021-09-04 01:27:46 +05:30
const trees = { [`${TEST_PROJECT_ID}/main`]: [] };
2020-04-22 19:07:51 +05:30
2022-06-21 17:19:12 +05:30
return 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
);
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
2022-06-21 17:19:12 +05:30
it('when empty repo, loads empty branch', () => {
2020-04-22 19:07:51 +05:30
const mockGetters = { emptyRepo: true };
2022-06-21 17:19:12 +05:30
return testAction(
2020-04-22 19:07:51 +05:30
loadBranch,
{ projectId, branchId },
{ ...store.state, ...mockGetters },
[],
[{ type: 'loadEmptyBranch', payload: { projectId, branchId } }],
);
});
2022-06-21 17:19:12 +05:30
it('when branch already exists, does nothing', () => {
2020-04-22 19:07:51 +05:30
store.state.projects[projectId].branches[branchId] = {};
2022-06-21 17:19:12 +05:30
return testAction(loadBranch, { projectId, branchId }, store.state, [], []);
2020-04-22 19:07:51 +05:30
});
2022-06-21 17:19:12 +05:30
it('fetches branch data', async () => {
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
2022-06-21 17:19:12 +05:30
await loadBranch(
2020-03-13 15:44:24 +05:30
{ getters: mockGetters, state: store.state, dispatch: store.dispatch },
{ projectId, branchId },
2022-06-21 17:19:12 +05:30
);
expect(store.dispatch.mock.calls).toEqual([
['getBranchData', { projectId, branchId }],
['getMergeRequestsForBranch', { projectId, branchId }],
['getFiles', { projectId, branchId, ref }],
]);
2019-12-21 20:55:43 +05:30
});
2022-06-21 17:19:12 +05:30
it('shows an error if branch can not be fetched', async () => {
2020-06-23 00:09:42 +05:30
jest.spyOn(store, 'dispatch').mockReturnValue(Promise.reject());
2019-12-21 20:55:43 +05:30
2022-06-21 17:19:12 +05:30
await expect(loadBranch(store, { projectId, branchId })).rejects.toBeUndefined();
expect(store.dispatch.mock.calls).toEqual([
['getBranchData', { projectId, branchId }],
['showBranchNotFoundError', branchId],
]);
2019-12-21 20:55:43 +05:30
});
});
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
});
2022-06-21 17:19:12 +05:30
it('dispatches branch actions', async () => {
await openBranch(store, branch);
expect(store.dispatch.mock.calls).toEqual([
['setCurrentBranchId', branchId],
['loadBranch', { projectId, branchId }],
['loadFile', { basePath: undefined }],
]);
2019-09-04 21:01:54 +05:30
});
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
2022-06-21 17:19:12 +05:30
it('dispatches correct branch actions', async () => {
const val = await openBranch(store, branch);
expect(store.dispatch.mock.calls).toEqual([
['setCurrentBranchId', branchId],
['loadBranch', { projectId, branchId }],
]);
expect(val).toEqual(
new Error(
`An error occurred while getting files for - <strong>${projectId}/${branchId}</strong>`,
),
);
2019-09-04 21:01:54 +05:30
});
2019-07-07 11:18:12 +05:30
});
2018-11-20 20:47:30 +05:30
});
2018-10-15 14:42:47 +05:30
});