Commit daabbaea authored by Himanshu Kapoor's avatar Himanshu Kapoor

Fix "Discard" button in Web IDE does nothing

Discard button in Web IDE commits pane was broken.
parent ae27abc9
<script> <script>
import $ from 'jquery';
import { mapActions } from 'vuex'; import { mapActions } from 'vuex';
import { __ } from '~/locale'; import { sprintf, __ } from '~/locale';
import { GlModal } from '@gitlab/ui';
import FileIcon from '~/vue_shared/components/file_icon.vue'; import FileIcon from '~/vue_shared/components/file_icon.vue';
import ChangedFileIcon from '~/vue_shared/components/changed_file_icon.vue'; import ChangedFileIcon from '~/vue_shared/components/changed_file_icon.vue';
export default { export default {
components: { components: {
GlModal,
FileIcon, FileIcon,
ChangedFileIcon, ChangedFileIcon,
}, },
...@@ -17,7 +18,13 @@ export default { ...@@ -17,7 +18,13 @@ export default {
}, },
}, },
computed: { computed: {
activeButtonText() { discardModalId() {
return `discard-file-${this.activeFile.path}`;
},
discardModalTitle() {
return sprintf(__('Discard changes to %{path}?'), { path: this.activeFile.path });
},
actionButtonText() {
return this.activeFile.staged ? __('Unstage') : __('Stage'); return this.activeFile.staged ? __('Unstage') : __('Stage');
}, },
isStaged() { isStaged() {
...@@ -25,7 +32,7 @@ export default { ...@@ -25,7 +32,7 @@ export default {
}, },
}, },
methods: { methods: {
...mapActions(['stageChange', 'unstageChange']), ...mapActions(['stageChange', 'unstageChange', 'discardFileChanges']),
actionButtonClicked() { actionButtonClicked() {
if (this.activeFile.staged) { if (this.activeFile.staged) {
this.unstageChange(this.activeFile.path); this.unstageChange(this.activeFile.path);
...@@ -34,7 +41,7 @@ export default { ...@@ -34,7 +41,7 @@ export default {
} }
}, },
showDiscardModal() { showDiscardModal() {
$(document.getElementById(`discard-file-${this.activeFile.path}`)).modal('show'); this.$refs.discardModal.show();
}, },
}, },
}; };
...@@ -53,6 +60,7 @@ export default { ...@@ -53,6 +60,7 @@ export default {
<div class="ml-auto"> <div class="ml-auto">
<button <button
v-if="!isStaged" v-if="!isStaged"
ref="discardButton"
type="button" type="button"
class="btn btn-remove btn-inverted append-right-8" class="btn btn-remove btn-inverted append-right-8"
@click="showDiscardModal" @click="showDiscardModal"
...@@ -60,6 +68,7 @@ export default { ...@@ -60,6 +68,7 @@ export default {
{{ __('Discard') }} {{ __('Discard') }}
</button> </button>
<button <button
ref="actionButton"
:class="{ :class="{
'btn-success': !isStaged, 'btn-success': !isStaged,
'btn-warning': isStaged, 'btn-warning': isStaged,
...@@ -68,8 +77,19 @@ export default { ...@@ -68,8 +77,19 @@ export default {
class="btn btn-inverted" class="btn btn-inverted"
@click="actionButtonClicked" @click="actionButtonClicked"
> >
{{ activeButtonText }} {{ actionButtonText }}
</button> </button>
</div> </div>
<gl-modal
ref="discardModal"
ok-variant="danger"
cancel-variant="light"
:ok-title="__('Discard changes')"
:modal-id="discardModalId"
:title="discardModalTitle"
@ok="discardFileChanges(activeFile.path)"
>
{{ __("You will lose all changes you've made to this file. This action cannot be undone.") }}
</gl-modal>
</div> </div>
</template> </template>
---
title: 'Fix issue: Discard button in Web IDE does nothing'
merge_request: 21902
author:
type: fixed
import Vuex from 'vuex';
import { mount, createLocalVue } from '@vue/test-utils';
import { createStore } from '~/ide/stores';
import EditorHeader from '~/ide/components/commit_sidebar/editor_header.vue';
import { file } from '../../helpers';
const localVue = createLocalVue();
localVue.use(Vuex);
describe('IDE commit editor header', () => {
let wrapper;
let f;
let store;
const findDiscardModal = () => wrapper.find({ ref: 'discardModal' });
const findDiscardButton = () => wrapper.find({ ref: 'discardButton' });
const findActionButton = () => wrapper.find({ ref: 'actionButton' });
beforeEach(() => {
f = file('file');
store = createStore();
wrapper = mount(EditorHeader, {
store,
localVue,
sync: false,
propsData: {
activeFile: f,
},
});
jest.spyOn(wrapper.vm, 'stageChange').mockImplementation();
jest.spyOn(wrapper.vm, 'unstageChange').mockImplementation();
jest.spyOn(wrapper.vm, 'discardFileChanges').mockImplementation();
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
it('renders button to discard & stage', () => {
expect(wrapper.vm.$el.querySelectorAll('.btn').length).toBe(2);
});
describe('discard button', () => {
let modal;
beforeEach(() => {
modal = findDiscardModal();
jest.spyOn(modal.vm, 'show');
findDiscardButton().trigger('click');
});
it('opens a dialog confirming discard', () => {
expect(modal.vm.show).toHaveBeenCalled();
});
it('calls discardFileChanges if dialog result is confirmed', () => {
modal.vm.$emit('ok');
expect(wrapper.vm.discardFileChanges).toHaveBeenCalledWith(f.path);
});
});
describe('stage/unstage button', () => {
it('unstages the file if it was already staged', () => {
f.staged = true;
findActionButton().trigger('click');
expect(wrapper.vm.unstageChange).toHaveBeenCalledWith(f.path);
});
it('stages the file if it was not staged', () => {
findActionButton().trigger('click');
expect(wrapper.vm.stageChange).toHaveBeenCalledWith(f.path);
});
});
});
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