Commit 1dcf1f84 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch '27244-discard-fix' into 'master'

Fix "Discard" for newly-created and renamed files

See merge request gitlab-org/gitlab!21905
parents d1e4b32c b03965a8
...@@ -191,16 +191,22 @@ export const discardFileChanges = ({ dispatch, state, commit, getters }, path) = ...@@ -191,16 +191,22 @@ export const discardFileChanges = ({ dispatch, state, commit, getters }, path) =
dispatch('restoreTree', file.parentPath); dispatch('restoreTree', file.parentPath);
} }
commit(types.DISCARD_FILE_CHANGES, path); if (file.tempFile || file.prevPath) {
commit(types.REMOVE_FILE_FROM_CHANGED, path); dispatch('closeFile', file);
if (file.prevPath) { if (file.tempFile) {
dispatch('discardFileChanges', file.prevPath); dispatch('deleteEntry', file.path);
} else {
dispatch('renameEntry', {
path: file.path,
name: file.prevName,
parentPath: file.prevParentPath,
});
} }
} else {
commit(types.DISCARD_FILE_CHANGES, path);
if (file.tempFile && file.opened) { if (getters.activeFile && file.path === getters.activeFile.path) {
commit(types.TOGGLE_FILE_OPEN, path);
} else if (getters.activeFile && file.path === getters.activeFile.path) {
dispatch('updateDelayViewerUpdated', true) dispatch('updateDelayViewerUpdated', true)
.then(() => { .then(() => {
router.push(`/project${file.url}`); router.push(`/project${file.url}`);
...@@ -209,6 +215,9 @@ export const discardFileChanges = ({ dispatch, state, commit, getters }, path) = ...@@ -209,6 +215,9 @@ export const discardFileChanges = ({ dispatch, state, commit, getters }, path) =
throw e; throw e;
}); });
} }
}
commit(types.REMOVE_FILE_FROM_CHANGED, path);
eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.content); eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.content);
eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content); eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content);
......
---
title: Fix "Discard" for newly-created and renamed files
merge_request: 21905
author:
type: fixed
...@@ -581,11 +581,13 @@ describe('IDE store file actions', () => { ...@@ -581,11 +581,13 @@ describe('IDE store file actions', () => {
jest.spyOn(eventHub, '$on').mockImplementation(() => {}); jest.spyOn(eventHub, '$on').mockImplementation(() => {});
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
tmpFile = file(); tmpFile = file('tempFile');
tmpFile.content = 'testing'; tmpFile.content = 'testing';
store.state.changedFiles.push(tmpFile); store.state.changedFiles.push(tmpFile);
store.state.entries[tmpFile.path] = tmpFile; store.state.entries[tmpFile.path] = tmpFile;
jest.spyOn(store, 'dispatch');
}); });
it('resets file content', done => { it('resets file content', done => {
...@@ -610,33 +612,35 @@ describe('IDE store file actions', () => { ...@@ -610,33 +612,35 @@ describe('IDE store file actions', () => {
.catch(done.fail); .catch(done.fail);
}); });
it('closes temp file', done => { it('closes temp file and deletes it', () => {
tmpFile.tempFile = true; tmpFile.tempFile = true;
tmpFile.opened = true; tmpFile.opened = true;
tmpFile.parentPath = 'parentFile';
store.state.entries.parentFile = file('parentFile');
store actions.discardFileChanges(store, tmpFile.path);
.dispatch('discardFileChanges', tmpFile.path)
.then(() => {
expect(tmpFile.opened).toBeFalsy();
done(); expect(store.dispatch).toHaveBeenCalledWith('closeFile', tmpFile);
}) expect(store.dispatch).toHaveBeenCalledWith('deleteEntry', tmpFile.path);
.catch(done.fail);
}); });
it('does not re-open a closed temp file', done => { it('renames the file to its original name and closes it if it was open', () => {
tmpFile.tempFile = true; Object.assign(tmpFile, {
prevPath: 'parentPath/old_name',
prevName: 'old_name',
prevParentPath: 'parentPath',
});
expect(tmpFile.opened).toBeFalsy(); store.state.entries.parentPath = file('parentPath');
store actions.discardFileChanges(store, tmpFile.path);
.dispatch('discardFileChanges', tmpFile.path)
.then(() => {
expect(tmpFile.opened).toBeFalsy();
done(); expect(store.dispatch).toHaveBeenCalledWith('closeFile', tmpFile);
}) expect(store.dispatch).toHaveBeenCalledWith('renameEntry', {
.catch(done.fail); path: 'tempFile',
name: 'old_name',
parentPath: 'parentPath',
});
}); });
it('pushes route for active file', done => { it('pushes route for active file', done => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment