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

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

834 lines
24 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2021-03-11 19:13:27 +05:30
import eventHub from '~/ide/eventhub';
import { createRouter } from '~/ide/ide_router';
import service from '~/ide/services';
2020-03-13 15:44:24 +05:30
import { createStore } from '~/ide/stores';
2018-10-15 14:42:47 +05:30
import * as actions from '~/ide/stores/actions/file';
import * as types from '~/ide/stores/mutation_types';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2022-08-13 15:12:31 +05:30
import { stubPerformanceWebAPI } from 'helpers/performance';
2021-03-08 18:12:59 +05:30
import { file, createTriggerRenameAction, createTriggerUpdatePayload } from '../../helpers';
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
const ORIGINAL_CONTENT = 'original content';
2019-07-31 22:56:46 +05:30
const RELATIVE_URL_ROOT = '/gitlab';
2018-05-09 12:01:36 +05:30
describe('IDE store file actions', () => {
2018-11-08 19:23:39 +05:30
let mock;
2019-07-31 22:56:46 +05:30
let originalGon;
2020-03-13 15:44:24 +05:30
let store;
2020-06-23 00:09:42 +05:30
let router;
2018-11-08 19:23:39 +05:30
2018-05-09 12:01:36 +05:30
beforeEach(() => {
2022-08-13 15:12:31 +05:30
stubPerformanceWebAPI();
2018-11-08 19:23:39 +05:30
mock = new MockAdapter(axios);
2019-07-31 22:56:46 +05:30
originalGon = window.gon;
window.gon = {
...window.gon,
relative_url_root: RELATIVE_URL_ROOT,
};
2018-11-08 19:23:39 +05:30
2020-03-13 15:44:24 +05:30
store = createStore();
2020-11-24 15:15:51 +05:30
store.state.currentProjectId = 'test/test';
2021-09-04 01:27:46 +05:30
store.state.currentBranchId = 'main';
2020-11-24 15:15:51 +05:30
2020-06-23 00:09:42 +05:30
router = createRouter(store);
2020-03-13 15:44:24 +05:30
jest.spyOn(store, 'commit');
jest.spyOn(store, 'dispatch');
jest.spyOn(router, 'push').mockImplementation(() => {});
2018-05-09 12:01:36 +05:30
});
afterEach(() => {
2018-11-08 19:23:39 +05:30
mock.restore();
2019-07-31 22:56:46 +05:30
window.gon = originalGon;
2018-05-09 12:01:36 +05:30
});
describe('closeFile', () => {
let localFile;
beforeEach(() => {
localFile = file('testFile');
localFile.active = true;
localFile.opened = true;
store.state.openFiles.push(localFile);
store.state.entries[localFile.path] = localFile;
});
2020-07-28 23:09:34 +05:30
it('closes open files', () => {
return store.dispatch('closeFile', localFile).then(() => {
expect(localFile.opened).toBeFalsy();
expect(localFile.active).toBeFalsy();
expect(store.state.openFiles.length).toBe(0);
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('closes file even if file has changes', () => {
2018-05-09 12:01:36 +05:30
store.state.changedFiles.push(localFile);
2020-07-28 23:09:34 +05:30
return store
2018-05-09 12:01:36 +05:30
.dispatch('closeFile', localFile)
2022-04-04 11:22:00 +05:30
.then(nextTick)
2018-05-09 12:01:36 +05:30
.then(() => {
expect(store.state.openFiles.length).toBe(0);
expect(store.state.changedFiles.length).toBe(1);
2020-07-28 23:09:34 +05:30
});
2018-05-09 12:01:36 +05:30
});
2021-03-08 18:12:59 +05:30
it('switches to the next available file before closing the current one ', () => {
2020-11-24 15:15:51 +05:30
const f = file('newOpenFile');
2018-05-09 12:01:36 +05:30
store.state.openFiles.push(f);
store.state.entries[f.path] = f;
2020-07-28 23:09:34 +05:30
return store
2018-05-09 12:01:36 +05:30
.dispatch('closeFile', localFile)
2022-04-04 11:22:00 +05:30
.then(nextTick)
2018-05-09 12:01:36 +05:30
.then(() => {
2021-09-04 01:27:46 +05:30
expect(router.push).toHaveBeenCalledWith('/project/test/test/tree/main/-/newOpenFile/');
2020-07-28 23:09:34 +05:30
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('removes file if it pending', () => {
2021-03-08 18:12:59 +05:30
store.state.openFiles = [
{
...localFile,
pending: true,
},
];
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
return store.dispatch('closeFile', localFile).then(() => {
expect(store.state.openFiles.length).toBe(0);
});
2018-05-09 12:01:36 +05:30
});
});
describe('setFileActive', () => {
let localFile;
let scrollToTabSpy;
let oldScrollToTab;
beforeEach(() => {
2020-03-13 15:44:24 +05:30
scrollToTabSpy = jest.fn();
2018-05-09 12:01:36 +05:30
oldScrollToTab = store._actions.scrollToTab; // eslint-disable-line
store._actions.scrollToTab = [scrollToTabSpy]; // eslint-disable-line
localFile = file('setThisActive');
store.state.entries[localFile.path] = localFile;
});
afterEach(() => {
store._actions.scrollToTab = oldScrollToTab; // eslint-disable-line
});
2019-07-07 11:18:12 +05:30
it('calls scrollToTab', () => {
2020-03-13 15:44:24 +05:30
const dispatch = jest.fn();
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
actions.setFileActive(
{ commit() {}, state: store.state, getters: store.getters, dispatch },
localFile.path,
);
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
expect(dispatch).toHaveBeenCalledWith('scrollToTab');
2018-05-09 12:01:36 +05:30
});
2019-07-07 11:18:12 +05:30
it('commits SET_FILE_ACTIVE', () => {
2020-03-13 15:44:24 +05:30
const commit = jest.fn();
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
actions.setFileActive(
{ commit, state: store.state, getters: store.getters, dispatch() {} },
localFile.path,
);
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
expect(commit).toHaveBeenCalledWith('SET_FILE_ACTIVE', {
path: localFile.path,
active: true,
});
2018-05-09 12:01:36 +05:30
});
2019-07-07 11:18:12 +05:30
it('sets current active file to not active', () => {
2018-05-09 12:01:36 +05:30
const f = file('newActive');
store.state.entries[f.path] = f;
localFile.active = true;
store.state.openFiles.push(localFile);
2020-03-13 15:44:24 +05:30
const commit = jest.fn();
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
actions.setFileActive(
{ commit, state: store.state, getters: store.getters, dispatch() {} },
f.path,
);
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
expect(commit).toHaveBeenCalledWith('SET_FILE_ACTIVE', {
path: localFile.path,
active: false,
});
2018-05-09 12:01:36 +05:30
});
});
describe('getFileData', () => {
let localFile;
beforeEach(() => {
2020-03-13 15:44:24 +05:30
jest.spyOn(service, 'getFileData');
2018-05-09 12:01:36 +05:30
localFile = file(`newCreate-${Math.random()}`);
store.state.entries[localFile.path] = localFile;
2019-12-26 22:10:19 +05:30
store.state.currentProjectId = 'test/test';
2021-09-04 01:27:46 +05:30
store.state.currentBranchId = 'main';
2019-12-26 22:10:19 +05:30
store.state.projects['test/test'] = {
branches: {
2021-09-04 01:27:46 +05:30
main: {
2019-12-26 22:10:19 +05:30
commit: {
id: '7297abc',
},
},
},
};
2018-05-09 12:01:36 +05:30
});
2020-03-13 15:44:24 +05:30
describe('call to service', () => {
2021-03-08 18:12:59 +05:30
const callExpectation = (serviceCalled) => {
2020-03-13 15:44:24 +05:30
store.dispatch('getFileData', { path: localFile.path });
if (serviceCalled) {
expect(service.getFileData).toHaveBeenCalled();
} else {
expect(service.getFileData).not.toHaveBeenCalled();
}
};
beforeEach(() => {
service.getFileData.mockImplementation(() => new Promise(() => {}));
});
it("isn't called if file.raw exists", () => {
localFile.raw = 'raw data';
callExpectation(false);
});
it("isn't called if file is a tempFile", () => {
localFile.raw = '';
localFile.tempFile = true;
callExpectation(false);
});
it('is called if file is a tempFile but also renamed', () => {
localFile.raw = '';
localFile.tempFile = true;
localFile.prevPath = 'old_path';
callExpectation(true);
});
it('is called if tempFile but file was deleted and readded', () => {
localFile.raw = '';
localFile.tempFile = true;
localFile.prevPath = 'old_path';
store.state.stagedFiles = [{ ...localFile, deleted: true }];
callExpectation(true);
});
});
2018-11-08 19:23:39 +05:30
describe('success', () => {
beforeEach(() => {
2020-03-13 15:44:24 +05:30
mock.onGet(`${RELATIVE_URL_ROOT}/test/test/-/7297abc/${localFile.path}`).replyOnce(
2018-11-08 19:23:39 +05:30
200,
{
raw_path: 'raw_path',
},
{
'page-title': 'testing getFileData',
},
);
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('calls the service', () => {
return store.dispatch('getFileData', { path: localFile.path }).then(() => {
expect(service.getFileData).toHaveBeenCalledWith(
`${RELATIVE_URL_ROOT}/test/test/-/7297abc/${localFile.path}`,
);
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('sets document title with the branchId', () => {
return store.dispatch('getFileData', { path: localFile.path }).then(() => {
2021-09-04 01:27:46 +05:30
expect(document.title).toBe(`${localFile.path} · main · test/test · GitLab`);
2020-07-28 23:09:34 +05:30
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('sets the file as active', () => {
return store.dispatch('getFileData', { path: localFile.path }).then(() => {
expect(localFile.active).toBeTruthy();
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('sets the file not as active if we pass makeFileActive false', () => {
return store
2018-11-08 19:23:39 +05:30
.dispatch('getFileData', { path: localFile.path, makeFileActive: false })
.then(() => {
expect(localFile.active).toBeFalsy();
2020-07-28 23:09:34 +05:30
});
2018-11-08 19:23:39 +05:30
});
2020-07-28 23:09:34 +05:30
it('does not update the page title with the path of the file if makeFileActive is false', () => {
document.title = 'dummy title';
return store
.dispatch('getFileData', { path: localFile.path, makeFileActive: false })
2018-11-08 19:23:39 +05:30
.then(() => {
2020-07-28 23:09:34 +05:30
expect(document.title).toBe(`dummy title`);
});
});
2018-11-08 19:23:39 +05:30
2020-07-28 23:09:34 +05:30
it('adds the file to open files', () => {
return store.dispatch('getFileData', { path: localFile.path }).then(() => {
expect(store.state.openFiles.length).toBe(1);
expect(store.state.openFiles[0].name).toBe(localFile.name);
});
2018-11-08 19:23:39 +05:30
});
2021-01-03 14:25:43 +05:30
it('does not toggle loading if toggleLoading=false', () => {
expect(localFile.loading).toBe(false);
return store
.dispatch('getFileData', {
path: localFile.path,
makeFileActive: false,
toggleLoading: false,
})
.then(() => {
expect(localFile.loading).toBe(true);
});
});
2018-05-09 12:01:36 +05:30
});
2019-09-30 21:07:59 +05:30
describe('Re-named success', () => {
beforeEach(() => {
localFile = file(`newCreate-${Math.random()}`);
localFile.prevPath = 'old-dull-file';
localFile.path = 'new-shiny-file';
store.state.entries[localFile.path] = localFile;
2020-03-13 15:44:24 +05:30
mock.onGet(`${RELATIVE_URL_ROOT}/test/test/-/7297abc/old-dull-file`).replyOnce(
2019-09-30 21:07:59 +05:30
200,
{
raw_path: 'raw_path',
},
{
'page-title': 'testing old-dull-file',
},
);
});
2020-07-28 23:09:34 +05:30
it('sets document title considering `prevPath` on a file', () => {
return store.dispatch('getFileData', { path: localFile.path }).then(() => {
2021-09-04 01:27:46 +05:30
expect(document.title).toBe(`new-shiny-file · main · test/test · GitLab`);
2020-07-28 23:09:34 +05:30
});
2019-09-30 21:07:59 +05:30
});
});
2018-11-08 19:23:39 +05:30
describe('error', () => {
beforeEach(() => {
2020-03-13 15:44:24 +05:30
mock.onGet(`${RELATIVE_URL_ROOT}/test/test/-/7297abc/${localFile.path}`).networkError();
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
it('dispatches error action', () => {
const dispatch = jest.fn();
2018-11-08 19:23:39 +05:30
2020-03-13 15:44:24 +05:30
return actions
2019-12-26 22:10:19 +05:30
.getFileData(
{ state: store.state, commit() {}, dispatch, getters: store.getters },
{ path: localFile.path },
)
2018-11-08 19:23:39 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith('setErrorMessage', {
2020-03-13 15:44:24 +05:30
text: 'An error occurred while loading the file.',
action: expect.any(Function),
2018-11-08 19:23:39 +05:30
actionText: 'Please try again',
actionPayload: {
path: localFile.path,
makeFileActive: true,
},
});
2020-03-13 15:44:24 +05:30
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
});
});
describe('getRawFileData', () => {
let tmpFile;
beforeEach(() => {
2020-03-13 15:44:24 +05:30
jest.spyOn(service, 'getRawFileData');
2018-05-09 12:01:36 +05:30
2020-04-22 19:07:51 +05:30
tmpFile = { ...file('tmpFile'), rawPath: 'raw_path' };
2018-05-09 12:01:36 +05:30
store.state.entries[tmpFile.path] = tmpFile;
});
2018-11-08 19:23:39 +05:30
describe('success', () => {
beforeEach(() => {
mock.onGet(/(.*)/).replyOnce(200, 'raw');
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('calls getRawFileData service method', () => {
return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
expect(service.getRawFileData).toHaveBeenCalledWith(tmpFile);
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('updates file raw data', () => {
return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
expect(tmpFile.raw).toBe('raw');
});
2018-11-08 19:23:39 +05:30
});
2020-07-28 23:09:34 +05:30
it('calls also getBaseRawFileData service method', () => {
2020-03-13 15:44:24 +05:30
jest.spyOn(service, 'getBaseRawFileData').mockReturnValue(Promise.resolve('baseraw'));
2018-11-08 19:23:39 +05:30
2018-11-18 11:00:15 +05:30
store.state.currentProjectId = 'gitlab-org/gitlab-ce';
store.state.currentMergeRequestId = '1';
store.state.projects = {
'gitlab-org/gitlab-ce': {
mergeRequests: {
1: {
baseCommitSha: 'SHA',
},
},
},
};
2018-11-08 19:23:39 +05:30
tmpFile.mrChange = { new_file: false };
2020-07-28 23:09:34 +05:30
return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
2020-11-24 15:15:51 +05:30
expect(service.getBaseRawFileData).toHaveBeenCalledWith(
tmpFile,
'gitlab-org/gitlab-ce',
'SHA',
);
2020-07-28 23:09:34 +05:30
expect(tmpFile.baseRaw).toBe('baseraw');
});
});
describe('sets file loading to true', () => {
let loadingWhenGettingRawData;
let loadingWhenGettingBaseRawData;
beforeEach(() => {
loadingWhenGettingRawData = undefined;
loadingWhenGettingBaseRawData = undefined;
2021-03-08 18:12:59 +05:30
jest.spyOn(service, 'getRawFileData').mockImplementation((f) => {
2020-07-28 23:09:34 +05:30
loadingWhenGettingRawData = f.loading;
return Promise.resolve('raw');
});
2021-03-08 18:12:59 +05:30
jest.spyOn(service, 'getBaseRawFileData').mockImplementation((f) => {
2020-07-28 23:09:34 +05:30
loadingWhenGettingBaseRawData = f.loading;
return Promise.resolve('rawBase');
});
});
2018-11-08 19:23:39 +05:30
2020-07-28 23:09:34 +05:30
it('when getting raw file data', async () => {
expect(tmpFile.loading).toBe(false);
await store.dispatch('getRawFileData', { path: tmpFile.path });
expect(loadingWhenGettingRawData).toBe(true);
expect(tmpFile.loading).toBe(false);
});
it('when getting base raw file data', async () => {
tmpFile.mrChange = { new_file: false };
expect(tmpFile.loading).toBe(false);
await store.dispatch('getRawFileData', { path: tmpFile.path });
expect(loadingWhenGettingBaseRawData).toBe(true);
expect(tmpFile.loading).toBe(false);
});
it('when file was already loading', async () => {
tmpFile.loading = true;
await store.dispatch('getRawFileData', { path: tmpFile.path });
expect(loadingWhenGettingRawData).toBe(true);
expect(tmpFile.loading).toBe(false);
});
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
});
2018-11-08 19:23:39 +05:30
describe('return JSON', () => {
beforeEach(() => {
mock.onGet(/(.*)/).replyOnce(200, JSON.stringify({ test: '123' }));
});
2018-05-09 12:01:36 +05:30
2020-07-28 23:09:34 +05:30
it('does not parse returned JSON', () => {
return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
expect(tmpFile.raw).toEqual('{"test":"123"}');
});
2018-11-08 19:23:39 +05:30
});
});
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
describe('error', () => {
beforeEach(() => {
mock.onGet(/(.*)/).networkError();
});
2020-03-13 15:44:24 +05:30
it('dispatches error action', () => {
const dispatch = jest.fn();
2018-11-08 19:23:39 +05:30
2020-03-13 15:44:24 +05:30
return actions
.getRawFileData(
{ state: store.state, commit() {}, dispatch, getters: store.getters },
{ path: tmpFile.path },
)
2018-11-08 19:23:39 +05:30
.catch(() => {
expect(dispatch).toHaveBeenCalledWith('setErrorMessage', {
2020-03-13 15:44:24 +05:30
text: 'An error occurred while loading the file content.',
action: expect.any(Function),
2018-11-08 19:23:39 +05:30
actionText: 'Please try again',
actionPayload: {
path: tmpFile.path,
},
});
});
});
2020-07-28 23:09:34 +05:30
it('toggles loading off after error', async () => {
await expect(store.dispatch('getRawFileData', { path: tmpFile.path })).rejects.toThrow();
expect(tmpFile.loading).toBe(false);
});
2018-05-09 12:01:36 +05:30
});
});
describe('changeFileContent', () => {
let tmpFile;
2021-03-08 18:12:59 +05:30
let onFilesChange;
2018-05-09 12:01:36 +05:30
beforeEach(() => {
tmpFile = file('tmpFile');
2019-12-26 22:10:19 +05:30
tmpFile.content = '\n';
tmpFile.raw = '\n';
2018-05-09 12:01:36 +05:30
store.state.entries[tmpFile.path] = tmpFile;
2021-03-08 18:12:59 +05:30
onFilesChange = jest.fn();
eventHub.$on('ide.files.change', onFilesChange);
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('updates file content', () => {
2021-02-22 17:27:13 +05:30
const content = 'content\n';
return store.dispatch('changeFileContent', { path: tmpFile.path, content }).then(() => {
2020-07-28 23:09:34 +05:30
expect(tmpFile.content).toBe('content\n');
});
2019-12-26 22:10:19 +05:30
});
2021-02-22 17:27:13 +05:30
it('does nothing if path does not exist', () => {
const content = 'content\n';
return store
.dispatch('changeFileContent', { path: 'not/a/real_file.txt', content })
.then(() => {
expect(tmpFile.content).toBe('\n');
});
});
2020-07-28 23:09:34 +05:30
it('adds file into stagedFiles array', () => {
return store
2018-05-09 12:01:36 +05:30
.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content',
})
.then(() => {
2020-03-13 15:44:24 +05:30
expect(store.state.stagedFiles.length).toBe(1);
2020-07-28 23:09:34 +05:30
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('adds file not more than once into stagedFiles array', () => {
return store
2018-05-09 12:01:36 +05:30
.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content',
})
.then(() =>
store.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content 123',
}),
)
.then(() => {
2020-03-13 15:44:24 +05:30
expect(store.state.stagedFiles.length).toBe(1);
2020-07-28 23:09:34 +05:30
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('removes file from changedFiles array if not changed', () => {
return store
2018-05-09 12:01:36 +05:30
.dispatch('changeFileContent', {
path: tmpFile.path,
2019-12-26 22:10:19 +05:30
content: 'content\n',
2018-05-09 12:01:36 +05:30
})
.then(() =>
store.dispatch('changeFileContent', {
path: tmpFile.path,
2019-12-26 22:10:19 +05:30
content: '\n',
2018-05-09 12:01:36 +05:30
}),
)
.then(() => {
expect(store.state.changedFiles.length).toBe(0);
2020-07-28 23:09:34 +05:30
});
2018-05-09 12:01:36 +05:30
});
2021-03-08 18:12:59 +05:30
it('triggers ide.files.change', async () => {
expect(onFilesChange).not.toHaveBeenCalled();
await store.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content\n',
});
expect(onFilesChange).toHaveBeenCalledWith(createTriggerUpdatePayload(tmpFile.path));
});
2018-05-09 12:01:36 +05:30
});
2020-03-13 15:44:24 +05:30
describe('with changed file', () => {
2018-05-09 12:01:36 +05:30
let tmpFile;
beforeEach(() => {
2020-03-13 15:44:24 +05:30
tmpFile = file('tempFile');
2018-05-09 12:01:36 +05:30
tmpFile.content = 'testing';
2020-03-13 15:44:24 +05:30
tmpFile.raw = ORIGINAL_CONTENT;
2018-05-09 12:01:36 +05:30
store.state.changedFiles.push(tmpFile);
store.state.entries[tmpFile.path] = tmpFile;
});
2020-03-13 15:44:24 +05:30
describe('restoreOriginalFile', () => {
it('resets file content', () =>
store.dispatch('restoreOriginalFile', tmpFile.path).then(() => {
expect(tmpFile.content).toBe(ORIGINAL_CONTENT);
}));
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
it('closes temp file and deletes it', () => {
tmpFile.tempFile = true;
tmpFile.opened = true;
tmpFile.parentPath = 'parentFile';
store.state.entries.parentFile = file('parentFile');
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
actions.restoreOriginalFile(store, tmpFile.path);
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
expect(store.dispatch).toHaveBeenCalledWith('closeFile', tmpFile);
expect(store.dispatch).toHaveBeenCalledWith('deleteEntry', tmpFile.path);
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
describe('with renamed file', () => {
beforeEach(() => {
Object.assign(tmpFile, {
prevPath: 'parentPath/old_name',
prevName: 'old_name',
prevParentPath: 'parentPath',
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
store.state.entries.parentPath = file('parentPath');
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
actions.restoreOriginalFile(store, tmpFile.path);
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
it('renames the file to its original name and closes it if it was open', () => {
expect(store.dispatch).toHaveBeenCalledWith('closeFile', tmpFile);
expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
path: 'tempFile',
name: 'old_name',
parentPath: 'parentPath',
});
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
it('resets file content', () => {
expect(tmpFile.content).toBe(ORIGINAL_CONTENT);
});
});
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
describe('discardFileChanges', () => {
beforeEach(() => {
jest.spyOn(eventHub, '$on').mockImplementation(() => {});
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
describe('with regular file', () => {
beforeEach(() => {
actions.discardFileChanges(store, tmpFile.path);
});
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
it('restores original file', () => {
expect(store.dispatch).toHaveBeenCalledWith('restoreOriginalFile', tmpFile.path);
});
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
it('removes file from changedFiles array', () => {
expect(store.state.changedFiles.length).toBe(0);
});
it('does not push a new route', () => {
expect(router.push).not.toHaveBeenCalled();
});
it('emits eventHub event to dispose cached model', () => {
actions.discardFileChanges(store, tmpFile.path);
expect(eventHub.$emit).toHaveBeenCalledWith(
`editor.update.model.new.content.${tmpFile.key}`,
ORIGINAL_CONTENT,
);
expect(eventHub.$emit).toHaveBeenCalledWith(
`editor.update.model.dispose.unstaged-${tmpFile.key}`,
ORIGINAL_CONTENT,
);
});
});
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
describe('with active file', () => {
beforeEach(() => {
tmpFile.active = true;
store.state.openFiles.push(tmpFile);
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
actions.discardFileChanges(store, tmpFile.path);
});
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
it('pushes route for active file', () => {
2021-09-04 01:27:46 +05:30
expect(router.push).toHaveBeenCalledWith('/project/test/test/tree/main/-/tempFile/');
2020-03-13 15:44:24 +05:30
});
});
2018-10-15 14:42:47 +05:30
});
});
describe('stageChange', () => {
2020-03-13 15:44:24 +05:30
it('calls STAGE_CHANGE with file path', () => {
const f = { ...file('path'), content: 'old' };
store.state.entries[f.path] = f;
actions.stageChange(store, 'path');
expect(store.commit).toHaveBeenCalledWith(
types.STAGE_CHANGE,
expect.objectContaining({ path: 'path' }),
2018-10-15 14:42:47 +05:30
);
2020-03-13 15:44:24 +05:30
expect(store.commit).toHaveBeenCalledWith(types.SET_LAST_COMMIT_MSG, '');
2018-10-15 14:42:47 +05:30
});
});
describe('unstageChange', () => {
2020-03-13 15:44:24 +05:30
it('calls UNSTAGE_CHANGE with file path', () => {
const f = { ...file('path'), content: 'old' };
store.state.entries[f.path] = f;
store.state.stagedFiles.push({ f, content: 'new' });
actions.unstageChange(store, 'path');
expect(store.commit).toHaveBeenCalledWith(
types.UNSTAGE_CHANGE,
expect.objectContaining({ path: 'path' }),
2018-10-15 14:42:47 +05:30
);
});
2018-05-09 12:01:36 +05:30
});
describe('openPendingTab', () => {
let f;
beforeEach(() => {
f = {
...file(),
projectId: '123',
};
store.state.entries[f.path] = f;
});
2020-07-28 23:09:34 +05:30
it('makes file pending in openFiles', () => {
return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then(() => {
expect(store.state.openFiles[0].pending).toBe(true);
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('returns true when opened', () => {
2021-03-08 18:12:59 +05:30
return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then((added) => {
2020-07-28 23:09:34 +05:30
expect(added).toBe(true);
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('returns false when already opened', () => {
2018-10-15 14:42:47 +05:30
store.state.openFiles.push({
...f,
active: true,
key: `pending-${f.key}`,
});
2021-03-08 18:12:59 +05:30
return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then((added) => {
2020-07-28 23:09:34 +05:30
expect(added).toBe(false);
});
2018-10-15 14:42:47 +05:30
});
2020-07-28 23:09:34 +05:30
it('pushes router URL when added', () => {
return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then(() => {
2021-09-04 01:27:46 +05:30
expect(router.push).toHaveBeenCalledWith('/project/test/test/tree/main/');
2020-07-28 23:09:34 +05:30
});
2018-05-09 12:01:36 +05:30
});
});
describe('removePendingTab', () => {
let f;
beforeEach(() => {
2020-03-13 15:44:24 +05:30
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
2018-05-09 12:01:36 +05:30
f = {
...file('pendingFile'),
pending: true,
};
});
2020-07-28 23:09:34 +05:30
it('removes pending file from open files', () => {
2018-05-09 12:01:36 +05:30
store.state.openFiles.push(f);
2020-07-28 23:09:34 +05:30
return store.dispatch('removePendingTab', f).then(() => {
expect(store.state.openFiles.length).toBe(0);
});
2018-05-09 12:01:36 +05:30
});
2020-07-28 23:09:34 +05:30
it('emits event to dispose model', () => {
return store.dispatch('removePendingTab', f).then(() => {
expect(eventHub.$emit).toHaveBeenCalledWith(`editor.update.model.dispose.${f.key}`);
});
2018-05-09 12:01:36 +05:30
});
});
2019-09-04 21:01:54 +05:30
describe('triggerFilesChange', () => {
2021-01-29 00:20:46 +05:30
const { payload: renamePayload } = createTriggerRenameAction('test', '123');
2019-09-04 21:01:54 +05:30
beforeEach(() => {
2020-03-13 15:44:24 +05:30
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
2019-09-04 21:01:54 +05:30
});
2021-01-29 00:20:46 +05:30
it.each`
args | payload
${[]} | ${{}}
${[renamePayload]} | ${renamePayload}
`('emits event that files have changed (args=$args)', ({ args, payload }) => {
return store.dispatch('triggerFilesChange', ...args).then(() => {
expect(eventHub.$emit).toHaveBeenCalledWith('ide.files.change', payload);
2020-07-28 23:09:34 +05:30
});
2019-09-04 21:01:54 +05:30
});
});
2018-05-09 12:01:36 +05:30
});