2018-03-20 10:16:38 -04:00
|
|
|
import * as getters from '~/ide/stores/getters';
|
2019-12-17 13:07:48 -05:00
|
|
|
import { createStore } from '~/ide/stores';
|
2018-03-20 10:12:48 -04:00
|
|
|
import { file } from '../helpers';
|
|
|
|
|
2020-02-14 10:09:08 -05:00
|
|
|
const TEST_PROJECT_ID = 'test_project';
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
describe('IDE store getters', () => {
|
2018-03-20 10:12:48 -04:00
|
|
|
let localState;
|
2019-12-17 13:07:48 -05:00
|
|
|
let localStore;
|
2018-03-20 10:12:48 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-12-17 13:07:48 -05:00
|
|
|
localStore = createStore();
|
|
|
|
localState = localStore.state;
|
2018-03-20 10:12:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('activeFile', () => {
|
|
|
|
it('returns the current active file', () => {
|
|
|
|
localState.openFiles.push(file());
|
|
|
|
localState.openFiles.push(file('active'));
|
|
|
|
localState.openFiles[1].active = true;
|
|
|
|
|
|
|
|
expect(getters.activeFile(localState).name).toBe('active');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined if no active files are found', () => {
|
|
|
|
localState.openFiles.push(file());
|
|
|
|
localState.openFiles.push(file('active'));
|
|
|
|
|
|
|
|
expect(getters.activeFile(localState)).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('modifiedFiles', () => {
|
|
|
|
it('returns a list of modified files', () => {
|
|
|
|
localState.openFiles.push(file());
|
|
|
|
localState.changedFiles.push(file('changed'));
|
|
|
|
localState.changedFiles[0].changed = true;
|
|
|
|
|
|
|
|
const modifiedFiles = getters.modifiedFiles(localState);
|
|
|
|
|
|
|
|
expect(modifiedFiles.length).toBe(1);
|
|
|
|
expect(modifiedFiles[0].name).toBe('changed');
|
|
|
|
});
|
|
|
|
});
|
2018-03-26 08:18:24 -04:00
|
|
|
|
|
|
|
describe('currentMergeRequest', () => {
|
|
|
|
it('returns Current Merge Request', () => {
|
|
|
|
localState.currentProjectId = 'abcproject';
|
|
|
|
localState.currentMergeRequestId = 1;
|
|
|
|
localState.projects.abcproject = {
|
|
|
|
mergeRequests: {
|
2019-12-06 19:07:51 -05:00
|
|
|
1: {
|
|
|
|
mergeId: 1,
|
|
|
|
},
|
2018-03-26 08:18:24 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.currentMergeRequest(localState).mergeId).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if no active Merge Request was found', () => {
|
|
|
|
localState.currentProjectId = 'otherproject';
|
|
|
|
|
|
|
|
expect(getters.currentMergeRequest(localState)).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
2018-04-13 06:08:03 -04:00
|
|
|
|
|
|
|
describe('allBlobs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
Object.assign(localState.entries, {
|
2019-12-06 19:07:51 -05:00
|
|
|
index: {
|
|
|
|
type: 'blob',
|
|
|
|
name: 'index',
|
|
|
|
lastOpenedAt: 0,
|
|
|
|
},
|
|
|
|
app: {
|
|
|
|
type: 'blob',
|
|
|
|
name: 'blob',
|
|
|
|
lastOpenedAt: 0,
|
|
|
|
},
|
|
|
|
folder: {
|
|
|
|
type: 'folder',
|
|
|
|
name: 'folder',
|
|
|
|
lastOpenedAt: 0,
|
|
|
|
},
|
2018-04-13 06:08:03 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns only blobs', () => {
|
|
|
|
expect(getters.allBlobs(localState).length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns list sorted by lastOpenedAt', () => {
|
|
|
|
localState.entries.app.lastOpenedAt = new Date().getTime();
|
|
|
|
|
|
|
|
expect(getters.allBlobs(localState)[0].name).toBe('blob');
|
|
|
|
});
|
|
|
|
});
|
2018-05-01 04:29:08 -04:00
|
|
|
|
|
|
|
describe('getChangesInFolder', () => {
|
|
|
|
it('returns length of changed files for a path', () => {
|
|
|
|
localState.changedFiles.push(
|
|
|
|
{
|
|
|
|
path: 'test/index',
|
|
|
|
name: 'index',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'app/123',
|
|
|
|
name: '123',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(getters.getChangesInFolder(localState)('test')).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns length of changed & staged files for a path', () => {
|
|
|
|
localState.changedFiles.push(
|
|
|
|
{
|
|
|
|
path: 'test/index',
|
|
|
|
name: 'index',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'testing/123',
|
|
|
|
name: '123',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
localState.stagedFiles.push(
|
|
|
|
{
|
|
|
|
path: 'test/123',
|
|
|
|
name: '123',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'test/index',
|
|
|
|
name: 'index',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'testing/12345',
|
|
|
|
name: '12345',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(getters.getChangesInFolder(localState)('test')).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns length of changed & tempFiles files for a path', () => {
|
|
|
|
localState.changedFiles.push(
|
|
|
|
{
|
|
|
|
path: 'test/index',
|
|
|
|
name: 'index',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'test/newfile',
|
|
|
|
name: 'newfile',
|
|
|
|
tempFile: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(getters.getChangesInFolder(localState)('test')).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
2018-05-07 13:42:56 -04:00
|
|
|
|
|
|
|
describe('lastCommit', () => {
|
|
|
|
it('returns the last commit of the current branch on the current project', () => {
|
|
|
|
const commitTitle = 'Example commit title';
|
|
|
|
const localGetters = {
|
2018-06-25 07:05:46 -04:00
|
|
|
currentProject: {
|
|
|
|
name: 'test-project',
|
|
|
|
},
|
2018-06-25 06:22:46 -04:00
|
|
|
currentBranch: {
|
|
|
|
commit: {
|
|
|
|
title: commitTitle,
|
2018-05-07 13:42:56 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
localState.currentBranchId = 'example-branch';
|
|
|
|
|
|
|
|
expect(getters.lastCommit(localState, localGetters).title).toBe(commitTitle);
|
|
|
|
});
|
|
|
|
});
|
2018-06-25 09:51:45 -04:00
|
|
|
|
|
|
|
describe('currentBranch', () => {
|
|
|
|
it('returns current projects branch', () => {
|
2019-11-16 16:06:10 -05:00
|
|
|
localState.currentProjectId = 'abcproject';
|
|
|
|
localState.currentBranchId = 'master';
|
|
|
|
localState.projects.abcproject = {
|
|
|
|
name: 'abcproject',
|
|
|
|
branches: {
|
|
|
|
master: {
|
|
|
|
name: 'master',
|
2018-06-25 09:51:45 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2019-11-16 16:06:10 -05:00
|
|
|
const localGetters = {
|
2019-12-06 19:07:51 -05:00
|
|
|
findBranch: jest.fn(),
|
2019-11-16 16:06:10 -05:00
|
|
|
};
|
|
|
|
getters.currentBranch(localState, localGetters);
|
|
|
|
|
|
|
|
expect(localGetters.findBranch).toHaveBeenCalledWith('abcproject', 'master');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findProject', () => {
|
|
|
|
it('returns the project matching the id', () => {
|
|
|
|
localState.currentProjectId = 'abcproject';
|
|
|
|
localState.projects.abcproject = {
|
|
|
|
name: 'abcproject',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.findProject(localState)('abcproject').name).toBe('abcproject');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findBranch', () => {
|
|
|
|
let result;
|
|
|
|
|
|
|
|
it('returns the selected branch from a project', () => {
|
|
|
|
localState.currentProjectId = 'abcproject';
|
2018-06-25 09:51:45 -04:00
|
|
|
localState.currentBranchId = 'master';
|
2019-11-16 16:06:10 -05:00
|
|
|
localState.projects.abcproject = {
|
|
|
|
name: 'abcproject',
|
|
|
|
branches: {
|
|
|
|
master: {
|
|
|
|
name: 'master',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const localGetters = {
|
|
|
|
findProject: () => localState.projects.abcproject,
|
|
|
|
};
|
2018-06-25 09:51:45 -04:00
|
|
|
|
2019-11-16 16:06:10 -05:00
|
|
|
result = getters.findBranch(localState, localGetters)('abcproject', 'master');
|
|
|
|
|
|
|
|
expect(result.name).toBe('master');
|
2018-06-25 09:51:45 -04:00
|
|
|
});
|
|
|
|
});
|
2018-06-13 12:06:35 -04:00
|
|
|
|
2019-06-03 17:00:31 -04:00
|
|
|
describe('isOnDefaultBranch', () => {
|
|
|
|
it('returns false when no project exists', () => {
|
|
|
|
const localGetters = {
|
|
|
|
currentProject: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.isOnDefaultBranch({}, localGetters)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns true when project's default branch matches current branch", () => {
|
|
|
|
const localGetters = {
|
|
|
|
currentProject: {
|
|
|
|
default_branch: 'master',
|
|
|
|
},
|
|
|
|
branchName: 'master',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.isOnDefaultBranch({}, localGetters)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns false when project's default branch doesn't match current branch", () => {
|
|
|
|
const localGetters = {
|
|
|
|
currentProject: {
|
|
|
|
default_branch: 'master',
|
|
|
|
},
|
|
|
|
branchName: 'feature',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.isOnDefaultBranch({}, localGetters)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-13 12:06:35 -04:00
|
|
|
describe('packageJson', () => {
|
|
|
|
it('returns package.json entry', () => {
|
2019-12-06 19:07:51 -05:00
|
|
|
localState.entries['package.json'] = {
|
|
|
|
name: 'package.json',
|
|
|
|
};
|
2018-06-13 12:06:35 -04:00
|
|
|
|
|
|
|
expect(getters.packageJson(localState)).toEqual({
|
|
|
|
name: 'package.json',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-08-22 19:23:06 -04:00
|
|
|
|
|
|
|
describe('canPushToBranch', () => {
|
|
|
|
it('returns false when no currentBranch exists', () => {
|
|
|
|
const localGetters = {
|
|
|
|
currentProject: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.canPushToBranch({}, localGetters)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true when can_push to currentBranch', () => {
|
|
|
|
const localGetters = {
|
|
|
|
currentProject: {
|
|
|
|
default_branch: 'master',
|
|
|
|
},
|
2019-12-06 19:07:51 -05:00
|
|
|
currentBranch: {
|
|
|
|
can_push: true,
|
|
|
|
},
|
2019-08-22 19:23:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.canPushToBranch({}, localGetters)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false when !can_push to currentBranch', () => {
|
|
|
|
const localGetters = {
|
|
|
|
currentProject: {
|
|
|
|
default_branch: 'master',
|
|
|
|
},
|
2019-12-06 19:07:51 -05:00
|
|
|
currentBranch: {
|
|
|
|
can_push: false,
|
|
|
|
},
|
2019-08-22 19:23:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
expect(getters.canPushToBranch({}, localGetters)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2019-12-17 13:07:48 -05:00
|
|
|
|
|
|
|
describe('isFileDeletedAndReadded', () => {
|
|
|
|
const f = { ...file('sample'), content: 'sample', raw: 'sample' };
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
{
|
|
|
|
entry: { ...f, tempFile: true },
|
|
|
|
staged: { ...f, deleted: true },
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, content: 'changed' },
|
|
|
|
staged: { ...f, content: 'changed' },
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, content: 'changed' },
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
])(
|
|
|
|
'checks staged and unstaged files to see if a file was deleted and readded (case %#)',
|
|
|
|
({ entry, staged, output }) => {
|
|
|
|
Object.assign(localState, {
|
|
|
|
entries: {
|
|
|
|
[entry.path]: entry,
|
|
|
|
},
|
|
|
|
stagedFiles: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (staged) localState.stagedFiles.push(staged);
|
|
|
|
|
|
|
|
expect(localStore.getters.isFileDeletedAndReadded(entry.path)).toBe(output);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getDiffInfo', () => {
|
|
|
|
const f = { ...file('sample'), content: 'sample', raw: 'sample' };
|
|
|
|
it.each([
|
|
|
|
{
|
|
|
|
entry: { ...f, tempFile: true },
|
|
|
|
staged: { ...f, deleted: true },
|
|
|
|
output: { deleted: false, changed: false, tempFile: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, tempFile: true, content: 'changed', raw: '' },
|
|
|
|
staged: { ...f, deleted: true },
|
|
|
|
output: { deleted: false, changed: true, tempFile: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, content: 'changed' },
|
|
|
|
output: { changed: true },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, content: 'sample' },
|
|
|
|
staged: { ...f, content: 'changed' },
|
|
|
|
output: { changed: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, deleted: true },
|
|
|
|
output: { deleted: true, changed: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, prevPath: 'old_path' },
|
|
|
|
output: { renamed: true, changed: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
entry: { ...f, prevPath: 'old_path', content: 'changed' },
|
|
|
|
output: { renamed: true, changed: true },
|
|
|
|
},
|
|
|
|
])(
|
|
|
|
'compares changes in a file entry and returns a resulting diff info (case %#)',
|
|
|
|
({ entry, staged, output }) => {
|
|
|
|
Object.assign(localState, {
|
|
|
|
entries: {
|
|
|
|
[entry.path]: entry,
|
|
|
|
},
|
|
|
|
stagedFiles: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (staged) localState.stagedFiles.push(staged);
|
|
|
|
|
|
|
|
expect(localStore.getters.getDiffInfo(entry.path)).toEqual(expect.objectContaining(output));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2020-02-14 10:09:08 -05:00
|
|
|
|
|
|
|
describe('findProjectPermissions', () => {
|
|
|
|
it('returns false if project not found', () => {
|
|
|
|
expect(localStore.getters.findProjectPermissions(TEST_PROJECT_ID)).toEqual({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('finds permission in given project', () => {
|
|
|
|
const userPermissions = {
|
|
|
|
readMergeRequest: true,
|
|
|
|
createMergeRequestsIn: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
localState.projects[TEST_PROJECT_ID] = { userPermissions };
|
|
|
|
|
|
|
|
expect(localStore.getters.findProjectPermissions(TEST_PROJECT_ID)).toBe(userPermissions);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
|
|
|
getterName | permissionKey
|
|
|
|
${'canReadMergeRequests'} | ${'readMergeRequest'}
|
|
|
|
${'canCreateMergeRequests'} | ${'createMergeRequestIn'}
|
|
|
|
`('$getterName', ({ getterName, permissionKey }) => {
|
|
|
|
it.each([true, false])('finds permission for current project (%s)', val => {
|
|
|
|
localState.projects[TEST_PROJECT_ID] = {
|
|
|
|
userPermissions: {
|
|
|
|
[permissionKey]: val,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
localState.currentProjectId = TEST_PROJECT_ID;
|
|
|
|
|
|
|
|
expect(localStore.getters[getterName]).toBe(val);
|
|
|
|
});
|
|
|
|
});
|
2018-03-20 10:12:48 -04:00
|
|
|
});
|