diff options
-rw-r--r-- | app/assets/javascripts/ide/stores/actions/file.js | 39 | ||||
-rw-r--r-- | changelogs/unreleased/27244-discard-fix.yml | 5 | ||||
-rw-r--r-- | spec/frontend/ide/stores/actions/file_spec.js | 42 |
3 files changed, 52 insertions, 34 deletions
diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js index 27b8e32bf5e..d444f6be21d 100644 --- a/app/assets/javascripts/ide/stores/actions/file.js +++ b/app/assets/javascripts/ide/stores/actions/file.js @@ -191,25 +191,34 @@ export const discardFileChanges = ({ dispatch, state, commit, getters }, path) = dispatch('restoreTree', file.parentPath); } - commit(types.DISCARD_FILE_CHANGES, path); - commit(types.REMOVE_FILE_FROM_CHANGED, path); - - if (file.prevPath) { - dispatch('discardFileChanges', file.prevPath); - } + if (file.tempFile || file.prevPath) { + dispatch('closeFile', file); - if (file.tempFile && file.opened) { - commit(types.TOGGLE_FILE_OPEN, path); - } else if (getters.activeFile && file.path === getters.activeFile.path) { - dispatch('updateDelayViewerUpdated', true) - .then(() => { - router.push(`/project${file.url}`); - }) - .catch(e => { - throw e; + if (file.tempFile) { + dispatch('deleteEntry', file.path); + } else { + dispatch('renameEntry', { + path: file.path, + name: file.prevName, + parentPath: file.prevParentPath, }); + } + } else { + commit(types.DISCARD_FILE_CHANGES, path); + + if (getters.activeFile && file.path === getters.activeFile.path) { + dispatch('updateDelayViewerUpdated', true) + .then(() => { + router.push(`/project${file.url}`); + }) + .catch(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.dispose.unstaged-${file.key}`, file.content); }; diff --git a/changelogs/unreleased/27244-discard-fix.yml b/changelogs/unreleased/27244-discard-fix.yml new file mode 100644 index 00000000000..3c86a6d3f85 --- /dev/null +++ b/changelogs/unreleased/27244-discard-fix.yml @@ -0,0 +1,5 @@ +--- +title: Fix "Discard" for newly-created and renamed files +merge_request: 21905 +author: +type: fixed diff --git a/spec/frontend/ide/stores/actions/file_spec.js b/spec/frontend/ide/stores/actions/file_spec.js index e9a657ffbfc..4ec8f6b7dff 100644 --- a/spec/frontend/ide/stores/actions/file_spec.js +++ b/spec/frontend/ide/stores/actions/file_spec.js @@ -581,11 +581,13 @@ describe('IDE store file actions', () => { jest.spyOn(eventHub, '$on').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); - tmpFile = file(); + tmpFile = file('tempFile'); tmpFile.content = 'testing'; store.state.changedFiles.push(tmpFile); store.state.entries[tmpFile.path] = tmpFile; + + jest.spyOn(store, 'dispatch'); }); it('resets file content', done => { @@ -610,33 +612,35 @@ describe('IDE store file actions', () => { .catch(done.fail); }); - it('closes temp file', done => { + it('closes temp file and deletes it', () => { tmpFile.tempFile = true; tmpFile.opened = true; + tmpFile.parentPath = 'parentFile'; + store.state.entries.parentFile = file('parentFile'); - store - .dispatch('discardFileChanges', tmpFile.path) - .then(() => { - expect(tmpFile.opened).toBeFalsy(); + actions.discardFileChanges(store, tmpFile.path); - done(); - }) - .catch(done.fail); + expect(store.dispatch).toHaveBeenCalledWith('closeFile', tmpFile); + expect(store.dispatch).toHaveBeenCalledWith('deleteEntry', tmpFile.path); }); - it('does not re-open a closed temp file', done => { - tmpFile.tempFile = true; + it('renames the file to its original name and closes it if it was open', () => { + Object.assign(tmpFile, { + prevPath: 'parentPath/old_name', + prevName: 'old_name', + prevParentPath: 'parentPath', + }); - expect(tmpFile.opened).toBeFalsy(); + store.state.entries.parentPath = file('parentPath'); - store - .dispatch('discardFileChanges', tmpFile.path) - .then(() => { - expect(tmpFile.opened).toBeFalsy(); + actions.discardFileChanges(store, tmpFile.path); - done(); - }) - .catch(done.fail); + expect(store.dispatch).toHaveBeenCalledWith('closeFile', tmpFile); + expect(store.dispatch).toHaveBeenCalledWith('renameEntry', { + path: 'tempFile', + name: 'old_name', + parentPath: 'parentPath', + }); }); it('pushes route for active file', done => { |