2018-03-20 14:16:38 +00:00
|
|
|
import mutations from '~/ide/stores/mutations/file';
|
|
|
|
import state from '~/ide/stores/state';
|
2019-07-23 22:13:36 +00:00
|
|
|
import { FILE_VIEW_MODE_PREVIEW } from '~/ide/constants';
|
2018-03-20 14:12:48 +00:00
|
|
|
import { file } from '../../helpers';
|
|
|
|
|
2018-03-26 12:18:24 +00:00
|
|
|
describe('IDE store file mutations', () => {
|
2018-03-20 14:12:48 +00:00
|
|
|
let localState;
|
|
|
|
let localFile;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
localState = state();
|
2019-12-07 00:07:51 +00:00
|
|
|
localFile = { ...file(), type: 'blob' };
|
2018-03-20 14:12:48 +00:00
|
|
|
|
|
|
|
localState.entries[localFile.path] = localFile;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SET_FILE_ACTIVE', () => {
|
|
|
|
it('sets the file active', () => {
|
|
|
|
mutations.SET_FILE_ACTIVE(localState, {
|
|
|
|
path: localFile.path,
|
|
|
|
active: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.active).toBeTruthy();
|
|
|
|
});
|
2018-03-27 15:39:24 +00:00
|
|
|
|
|
|
|
it('sets pending tab as not active', () => {
|
2019-12-07 00:07:51 +00:00
|
|
|
localState.openFiles.push({ ...localFile, pending: true, active: true });
|
2018-03-27 15:39:24 +00:00
|
|
|
|
|
|
|
mutations.SET_FILE_ACTIVE(localState, {
|
|
|
|
path: localFile.path,
|
|
|
|
active: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localState.openFiles[0].active).toBe(false);
|
|
|
|
});
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('TOGGLE_FILE_OPEN', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mutations.TOGGLE_FILE_OPEN(localState, localFile.path);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds into opened files', () => {
|
|
|
|
expect(localFile.opened).toBeTruthy();
|
|
|
|
expect(localState.openFiles.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes from opened files', () => {
|
|
|
|
mutations.TOGGLE_FILE_OPEN(localState, localFile.path);
|
|
|
|
|
|
|
|
expect(localFile.opened).toBeFalsy();
|
|
|
|
expect(localState.openFiles.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SET_FILE_DATA', () => {
|
|
|
|
it('sets extra file data', () => {
|
|
|
|
mutations.SET_FILE_DATA(localState, {
|
|
|
|
data: {
|
|
|
|
blame_path: 'blame',
|
|
|
|
commits_path: 'commits',
|
|
|
|
permalink: 'permalink',
|
|
|
|
raw_path: 'raw',
|
|
|
|
binary: true,
|
|
|
|
render_error: 'render_error',
|
|
|
|
},
|
|
|
|
file: localFile,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.blamePath).toBe('blame');
|
|
|
|
expect(localFile.commitsPath).toBe('commits');
|
|
|
|
expect(localFile.permalink).toBe('permalink');
|
|
|
|
expect(localFile.rawPath).toBe('raw');
|
|
|
|
expect(localFile.binary).toBeTruthy();
|
|
|
|
expect(localFile.renderError).toBe('render_error');
|
2018-03-26 12:18:24 +00:00
|
|
|
expect(localFile.raw).toBeNull();
|
|
|
|
expect(localFile.baseRaw).toBeNull();
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|
2019-07-03 12:14:44 +00:00
|
|
|
|
|
|
|
it('sets extra file data to all arrays concerned', () => {
|
|
|
|
localState.stagedFiles = [localFile];
|
|
|
|
localState.changedFiles = [localFile];
|
|
|
|
localState.openFiles = [localFile];
|
|
|
|
|
|
|
|
const rawPath = 'foo/bar/blah.md';
|
|
|
|
|
|
|
|
mutations.SET_FILE_DATA(localState, {
|
|
|
|
data: {
|
|
|
|
raw_path: rawPath,
|
|
|
|
},
|
|
|
|
file: localFile,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localState.stagedFiles[0].rawPath).toEqual(rawPath);
|
|
|
|
expect(localState.changedFiles[0].rawPath).toEqual(rawPath);
|
|
|
|
expect(localState.openFiles[0].rawPath).toEqual(rawPath);
|
|
|
|
expect(localFile.rawPath).toEqual(rawPath);
|
|
|
|
});
|
2019-07-19 11:18:31 +00:00
|
|
|
|
|
|
|
it('does not mutate certain props on the file', () => {
|
|
|
|
const path = 'New Path';
|
|
|
|
const name = 'New Name';
|
|
|
|
localFile.path = path;
|
|
|
|
localFile.name = name;
|
|
|
|
|
|
|
|
localState.stagedFiles = [localFile];
|
|
|
|
localState.changedFiles = [localFile];
|
|
|
|
localState.openFiles = [localFile];
|
|
|
|
|
|
|
|
mutations.SET_FILE_DATA(localState, {
|
|
|
|
data: {
|
|
|
|
path: 'Old Path',
|
|
|
|
name: 'Old Name',
|
|
|
|
raw: 'Old Raw',
|
|
|
|
base_raw: 'Old Base Raw',
|
|
|
|
},
|
|
|
|
file: localFile,
|
|
|
|
});
|
|
|
|
|
|
|
|
[
|
|
|
|
localState.stagedFiles[0],
|
|
|
|
localState.changedFiles[0],
|
|
|
|
localState.openFiles[0],
|
|
|
|
localFile,
|
|
|
|
].forEach(f => {
|
|
|
|
expect(f).toEqual(
|
2019-12-07 00:07:51 +00:00
|
|
|
expect.objectContaining({
|
2019-07-19 11:18:31 +00:00
|
|
|
path,
|
|
|
|
name,
|
|
|
|
raw: null,
|
|
|
|
baseRaw: null,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('SET_FILE_RAW_DATA', () => {
|
|
|
|
it('sets raw data', () => {
|
|
|
|
mutations.SET_FILE_RAW_DATA(localState, {
|
|
|
|
file: localFile,
|
|
|
|
raw: 'testing',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.raw).toBe('testing');
|
|
|
|
});
|
2018-07-26 14:56:56 +00:00
|
|
|
|
|
|
|
it('adds raw data to open pending file', () => {
|
2019-12-07 00:07:51 +00:00
|
|
|
localState.openFiles.push({ ...localFile, pending: true });
|
2018-07-26 14:56:56 +00:00
|
|
|
|
|
|
|
mutations.SET_FILE_RAW_DATA(localState, {
|
|
|
|
file: localFile,
|
|
|
|
raw: 'testing',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localState.openFiles[0].raw).toBe('testing');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not add raw data to open pending tempFile file', () => {
|
2019-12-07 00:07:51 +00:00
|
|
|
localState.openFiles.push({ ...localFile, pending: true, tempFile: true });
|
2018-07-26 14:56:56 +00:00
|
|
|
|
|
|
|
mutations.SET_FILE_RAW_DATA(localState, {
|
|
|
|
file: localFile,
|
|
|
|
raw: 'testing',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localState.openFiles[0].raw).not.toBe('testing');
|
|
|
|
});
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|
|
|
|
|
2018-03-26 12:18:24 +00:00
|
|
|
describe('SET_FILE_BASE_RAW_DATA', () => {
|
|
|
|
it('sets raw data from base branch', () => {
|
|
|
|
mutations.SET_FILE_BASE_RAW_DATA(localState, {
|
|
|
|
file: localFile,
|
|
|
|
baseRaw: 'testing',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.baseRaw).toBe('testing');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-20 14:12:48 +00:00
|
|
|
describe('UPDATE_FILE_CONTENT', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
localFile.raw = 'test';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets content', () => {
|
|
|
|
mutations.UPDATE_FILE_CONTENT(localState, {
|
|
|
|
path: localFile.path,
|
|
|
|
content: 'test',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.content).toBe('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets changed if content does not match raw', () => {
|
|
|
|
mutations.UPDATE_FILE_CONTENT(localState, {
|
|
|
|
path: localFile.path,
|
|
|
|
content: 'testing',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.content).toBe('testing');
|
|
|
|
expect(localFile.changed).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets changed if file is a temp file', () => {
|
|
|
|
localFile.tempFile = true;
|
|
|
|
|
|
|
|
mutations.UPDATE_FILE_CONTENT(localState, {
|
|
|
|
path: localFile.path,
|
|
|
|
content: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.changed).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-28 08:40:48 +00:00
|
|
|
describe('SET_FILE_MERGE_REQUEST_CHANGE', () => {
|
2018-03-26 12:18:24 +00:00
|
|
|
it('sets file mr change', () => {
|
2018-03-28 08:40:48 +00:00
|
|
|
mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
|
2018-03-26 12:18:24 +00:00
|
|
|
file: localFile,
|
2019-12-07 00:07:51 +00:00
|
|
|
mrChange: {
|
|
|
|
diff: 'ABC',
|
|
|
|
},
|
2018-03-26 12:18:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.mrChange.diff).toBe('ABC');
|
|
|
|
});
|
2018-06-13 09:35:52 +00:00
|
|
|
|
|
|
|
it('has diffMode replaced by default', () => {
|
|
|
|
mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
|
|
|
|
file: localFile,
|
|
|
|
mrChange: {
|
|
|
|
diff: 'ABC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.mrChange.diffMode).toBe('replaced');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has diffMode new', () => {
|
|
|
|
mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
|
|
|
|
file: localFile,
|
|
|
|
mrChange: {
|
|
|
|
diff: 'ABC',
|
|
|
|
new_file: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.mrChange.diffMode).toBe('new');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has diffMode deleted', () => {
|
|
|
|
mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
|
|
|
|
file: localFile,
|
|
|
|
mrChange: {
|
|
|
|
diff: 'ABC',
|
|
|
|
deleted_file: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.mrChange.diffMode).toBe('deleted');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has diffMode renamed', () => {
|
|
|
|
mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
|
|
|
|
file: localFile,
|
|
|
|
mrChange: {
|
|
|
|
diff: 'ABC',
|
|
|
|
renamed_file: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.mrChange.diffMode).toBe('renamed');
|
|
|
|
});
|
2018-03-26 12:18:24 +00:00
|
|
|
});
|
|
|
|
|
2018-03-20 14:12:48 +00:00
|
|
|
describe('DISCARD_FILE_CHANGES', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
localFile.content = 'test';
|
|
|
|
localFile.changed = true;
|
2018-07-26 14:56:56 +00:00
|
|
|
localState.currentProjectId = 'gitlab-ce';
|
|
|
|
localState.currentBranchId = 'master';
|
|
|
|
localState.trees['gitlab-ce/master'] = {
|
|
|
|
tree: [],
|
|
|
|
};
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('resets content and changed', () => {
|
|
|
|
mutations.DISCARD_FILE_CHANGES(localState, localFile.path);
|
|
|
|
|
|
|
|
expect(localFile.content).toBe('');
|
|
|
|
expect(localFile.changed).toBeFalsy();
|
|
|
|
});
|
2018-07-26 14:56:56 +00:00
|
|
|
|
|
|
|
it('adds to root tree if deleted', () => {
|
|
|
|
localFile.deleted = true;
|
|
|
|
|
|
|
|
mutations.DISCARD_FILE_CHANGES(localState, localFile.path);
|
|
|
|
|
2019-12-07 00:07:51 +00:00
|
|
|
expect(localState.trees['gitlab-ce/master'].tree).toEqual([{ ...localFile, deleted: false }]);
|
2018-07-26 14:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds to parent tree if deleted', () => {
|
|
|
|
localFile.deleted = true;
|
|
|
|
localFile.parentPath = 'parentPath';
|
|
|
|
localState.entries.parentPath = {
|
|
|
|
tree: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
mutations.DISCARD_FILE_CHANGES(localState, localFile.path);
|
|
|
|
|
2019-12-07 00:07:51 +00:00
|
|
|
expect(localState.entries.parentPath.tree).toEqual([{ ...localFile, deleted: false }]);
|
2018-07-26 14:56:56 +00:00
|
|
|
});
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ADD_FILE_TO_CHANGED', () => {
|
|
|
|
it('adds file into changed files array', () => {
|
|
|
|
mutations.ADD_FILE_TO_CHANGED(localState, localFile.path);
|
|
|
|
|
|
|
|
expect(localState.changedFiles.length).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('REMOVE_FILE_FROM_CHANGED', () => {
|
|
|
|
it('removes files from changed files array', () => {
|
|
|
|
localState.changedFiles.push(localFile);
|
|
|
|
|
|
|
|
mutations.REMOVE_FILE_FROM_CHANGED(localState, localFile.path);
|
|
|
|
|
|
|
|
expect(localState.changedFiles.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-21 10:05:08 +00:00
|
|
|
describe('STAGE_CHANGE', () => {
|
2019-10-04 15:06:38 +00:00
|
|
|
beforeEach(() => {
|
2018-03-21 12:43:59 +00:00
|
|
|
mutations.STAGE_CHANGE(localState, localFile.path);
|
2019-10-04 15:06:38 +00:00
|
|
|
});
|
2018-03-21 10:05:08 +00:00
|
|
|
|
2019-10-04 15:06:38 +00:00
|
|
|
it('adds file into stagedFiles array', () => {
|
2018-03-21 10:05:08 +00:00
|
|
|
expect(localState.stagedFiles.length).toBe(1);
|
2018-03-21 12:43:59 +00:00
|
|
|
expect(localState.stagedFiles[0]).toEqual(localFile);
|
2018-03-21 10:05:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates stagedFile if it is already staged', () => {
|
2018-03-21 12:43:59 +00:00
|
|
|
localFile.raw = 'testing 123';
|
2018-03-21 10:05:08 +00:00
|
|
|
|
2018-03-21 12:43:59 +00:00
|
|
|
mutations.STAGE_CHANGE(localState, localFile.path);
|
2018-03-21 10:05:08 +00:00
|
|
|
|
|
|
|
expect(localState.stagedFiles.length).toBe(1);
|
|
|
|
expect(localState.stagedFiles[0].raw).toEqual('testing 123');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('UNSTAGE_CHANGE', () => {
|
|
|
|
let f;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-12-07 00:07:51 +00:00
|
|
|
f = { ...file(), type: 'blob', staged: true };
|
2018-03-21 10:05:08 +00:00
|
|
|
|
|
|
|
localState.stagedFiles.push(f);
|
|
|
|
localState.changedFiles.push(f);
|
2018-03-21 12:43:59 +00:00
|
|
|
localState.entries[f.path] = f;
|
2018-03-21 10:05:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes from stagedFiles array', () => {
|
2018-03-21 12:43:59 +00:00
|
|
|
mutations.UNSTAGE_CHANGE(localState, f.path);
|
2018-03-21 10:05:08 +00:00
|
|
|
|
|
|
|
expect(localState.stagedFiles.length).toBe(0);
|
2018-03-21 12:43:59 +00:00
|
|
|
expect(localState.changedFiles.length).toBe(1);
|
2018-03-21 10:05:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-20 14:12:48 +00:00
|
|
|
describe('TOGGLE_FILE_CHANGED', () => {
|
|
|
|
it('updates file changed status', () => {
|
|
|
|
mutations.TOGGLE_FILE_CHANGED(localState, {
|
|
|
|
file: localFile,
|
|
|
|
changed: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localFile.changed).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2018-03-27 15:39:24 +00:00
|
|
|
|
2018-04-05 11:12:40 +00:00
|
|
|
describe('SET_FILE_VIEWMODE', () => {
|
|
|
|
it('updates file view mode', () => {
|
|
|
|
mutations.SET_FILE_VIEWMODE(localState, {
|
|
|
|
file: localFile,
|
2019-07-23 22:13:36 +00:00
|
|
|
viewMode: FILE_VIEW_MODE_PREVIEW,
|
2018-04-05 11:12:40 +00:00
|
|
|
});
|
|
|
|
|
2019-07-23 22:13:36 +00:00
|
|
|
expect(localFile.viewMode).toBe(FILE_VIEW_MODE_PREVIEW);
|
2018-04-05 11:12:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-27 15:39:24 +00:00
|
|
|
describe('ADD_PENDING_TAB', () => {
|
|
|
|
beforeEach(() => {
|
2019-12-07 00:07:51 +00:00
|
|
|
const f = { ...file('openFile'), path: 'openFile', active: true, opened: true };
|
2018-03-27 15:39:24 +00:00
|
|
|
|
|
|
|
localState.entries[f.path] = f;
|
|
|
|
localState.openFiles.push(f);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds file into openFiles as pending', () => {
|
2019-12-07 00:07:51 +00:00
|
|
|
mutations.ADD_PENDING_TAB(localState, {
|
|
|
|
file: localFile,
|
|
|
|
});
|
2018-03-27 15:39:24 +00:00
|
|
|
|
|
|
|
expect(localState.openFiles.length).toBe(1);
|
2018-04-27 16:55:39 +00:00
|
|
|
expect(localState.openFiles[0].pending).toBe(true);
|
|
|
|
expect(localState.openFiles[0].key).toBe(`pending-${localFile.key}`);
|
2018-03-27 15:39:24 +00:00
|
|
|
});
|
|
|
|
|
2018-04-27 16:55:39 +00:00
|
|
|
it('only allows 1 open pending file', () => {
|
|
|
|
const newFile = file('test');
|
|
|
|
localState.entries[newFile.path] = newFile;
|
2018-03-27 15:39:24 +00:00
|
|
|
|
2019-12-07 00:07:51 +00:00
|
|
|
mutations.ADD_PENDING_TAB(localState, {
|
|
|
|
file: localFile,
|
|
|
|
});
|
2018-03-27 15:39:24 +00:00
|
|
|
|
2018-04-27 16:55:39 +00:00
|
|
|
expect(localState.openFiles.length).toBe(1);
|
2018-03-27 15:39:24 +00:00
|
|
|
|
2019-12-07 00:07:51 +00:00
|
|
|
mutations.ADD_PENDING_TAB(localState, {
|
|
|
|
file: file('test'),
|
|
|
|
});
|
2018-03-27 15:39:24 +00:00
|
|
|
|
2018-04-27 16:55:39 +00:00
|
|
|
expect(localState.openFiles.length).toBe(1);
|
|
|
|
expect(localState.openFiles[0].name).toBe('test');
|
2018-03-27 15:39:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('REMOVE_PENDING_TAB', () => {
|
|
|
|
it('removes pending tab from openFiles', () => {
|
|
|
|
localFile.key = 'testing';
|
|
|
|
localState.openFiles.push(localFile);
|
|
|
|
|
|
|
|
mutations.REMOVE_PENDING_TAB(localState, localFile);
|
|
|
|
|
|
|
|
expect(localState.openFiles.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2018-03-20 14:12:48 +00:00
|
|
|
});
|