Commit 1935282f authored by Thomas Randolph's avatar Thomas Randolph

Update whitespace toggle to use GlFormCheckbox

parent cf14e853
...@@ -26,6 +26,9 @@ export default { ...@@ -26,6 +26,9 @@ export default {
toggleFileByFile() { toggleFileByFile() {
this.setFileByFile({ fileByFile: !this.viewDiffsFileByFile }); this.setFileByFile({ fileByFile: !this.viewDiffsFileByFile });
}, },
toggleWhitespace(updatedSetting) {
this.setShowWhitespace({ showWhitespace: updatedSetting, pushState: true });
},
}, },
}; };
</script> </script>
...@@ -80,26 +83,21 @@ export default { ...@@ -80,26 +83,21 @@ export default {
</gl-button> </gl-button>
</gl-button-group> </gl-button-group>
</div> </div>
<div class="gl-mt-3 gl-px-3"> <gl-form-checkbox
<label class="gl-mb-0"> data-testid="show-whitespace"
<input class="gl-mt-3 gl-pl-3"
id="show-whitespace" :checked="showWhitespace"
type="checkbox" @input="toggleWhitespace"
:checked="showWhitespace" >
@change="setShowWhitespace({ showWhitespace: $event.target.checked, pushState: true })" {{ $options.i18n.whitespace }}
/> </gl-form-checkbox>
{{ $options.i18n.whitespace }} <gl-form-checkbox
</label> data-testid="file-by-file"
</div> class="gl-pl-3 gl-mb-0"
<div class="gl-mt-3 gl-px-3"> :checked="viewDiffsFileByFile"
<gl-form-checkbox @input="toggleFileByFile"
data-testid="file-by-file" >
class="gl-mb-0" {{ $options.i18n.fileByFile }}
:checked="viewDiffsFileByFile" </gl-form-checkbox>
@input="toggleFileByFile"
>
{{ $options.i18n.fileByFile }}
</gl-form-checkbox>
</div>
</gl-dropdown> </gl-dropdown>
</template> </template>
...@@ -21,13 +21,13 @@ RSpec.describe 'Merge request > User toggles whitespace changes', :js do ...@@ -21,13 +21,13 @@ RSpec.describe 'Merge request > User toggles whitespace changes', :js do
describe 'clicking "Hide whitespace changes" button' do describe 'clicking "Hide whitespace changes" button' do
it 'toggles the "Hide whitespace changes" button' do it 'toggles the "Hide whitespace changes" button' do
find('#show-whitespace').click find('[data-testid="show-whitespace"]').click
visit diffs_project_merge_request_path(project, merge_request) visit diffs_project_merge_request_path(project, merge_request)
find('.js-show-diff-settings').click find('.js-show-diff-settings').click
expect(find('#show-whitespace')).not_to be_checked expect(find('[data-testid="show-whitespace"]')).not_to be_checked
end end
end end
end end
import { mount, createLocalVue } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import SettingsDropdown from '~/diffs/components/settings_dropdown.vue'; import SettingsDropdown from '~/diffs/components/settings_dropdown.vue';
import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants'; import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants';
import eventHub from '~/diffs/event_hub'; import eventHub from '~/diffs/event_hub';
import diffModule from '~/diffs/store/modules';
const localVue = createLocalVue(); import { extendedWrapper } from '../../__helpers__/vue_test_utils_helper';
localVue.use(Vuex);
import createDiffsStore from '../create_diffs_store';
describe('Diff settings dropdown component', () => { describe('Diff settings dropdown component', () => {
let wrapper; let wrapper;
let vm; let vm;
let actions; let store;
Vue.use(Vuex);
function createComponent(extendStore = () => {}) { function createComponent(extendStore = () => {}) {
const store = new Vuex.Store({ store = createDiffsStore();
modules: {
diffs: {
namespaced: true,
actions,
state: diffModule().state,
getters: diffModule().getters,
},
},
});
extendStore(store); extendStore(store);
wrapper = mount(SettingsDropdown, { wrapper = extendedWrapper(
localVue, mount(SettingsDropdown, {
store, store,
}); }),
);
vm = wrapper.vm; vm = wrapper.vm;
} }
function getFileByFileCheckbox(vueWrapper) { function getFileByFileCheckbox(vueWrapper) {
return vueWrapper.find('[data-testid="file-by-file"]'); return vueWrapper.findByTestId('file-by-file');
}
function setup({ storeUpdater } = {}) {
createComponent(storeUpdater);
jest.spyOn(store, 'dispatch').mockImplementation(() => {});
} }
beforeEach(() => { beforeEach(() => {
actions = { setup();
setInlineDiffViewType: jest.fn(),
setParallelDiffViewType: jest.fn(),
setRenderTreeList: jest.fn(),
setShowWhitespace: jest.fn(),
setFileByFile: jest.fn(),
};
}); });
afterEach(() => { afterEach(() => {
store.dispatch.mockRestore();
wrapper.destroy(); wrapper.destroy();
}); });
describe('tree view buttons', () => { describe('tree view buttons', () => {
it('list view button dispatches setRenderTreeList with false', () => { it('list view button dispatches setRenderTreeList with false', () => {
createComponent();
wrapper.find('.js-list-view').trigger('click'); wrapper.find('.js-list-view').trigger('click');
expect(actions.setRenderTreeList).toHaveBeenCalledWith(expect.anything(), false); expect(store.dispatch).toHaveBeenCalledWith('diffs/setRenderTreeList', false);
}); });
it('tree view button dispatches setRenderTreeList with true', () => { it('tree view button dispatches setRenderTreeList with true', () => {
createComponent();
wrapper.find('.js-tree-view').trigger('click'); wrapper.find('.js-tree-view').trigger('click');
expect(actions.setRenderTreeList).toHaveBeenCalledWith(expect.anything(), true); expect(store.dispatch).toHaveBeenCalledWith('diffs/setRenderTreeList', true);
}); });
it('sets list button as selected when renderTreeList is false', () => { it('sets list button as selected when renderTreeList is false', () => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) =>
renderTreeList: false, Object.assign(origStore.state.diffs, { renderTreeList: false }),
});
}); });
expect(wrapper.find('.js-list-view').classes('selected')).toBe(true); expect(wrapper.find('.js-list-view').classes('selected')).toBe(true);
...@@ -81,10 +72,8 @@ describe('Diff settings dropdown component', () => { ...@@ -81,10 +72,8 @@ describe('Diff settings dropdown component', () => {
}); });
it('sets tree button as selected when renderTreeList is true', () => { it('sets tree button as selected when renderTreeList is true', () => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) => Object.assign(origStore.state.diffs, { renderTreeList: true }),
renderTreeList: true,
});
}); });
expect(wrapper.find('.js-list-view').classes('selected')).toBe(false); expect(wrapper.find('.js-list-view').classes('selected')).toBe(false);
...@@ -94,10 +83,9 @@ describe('Diff settings dropdown component', () => { ...@@ -94,10 +83,9 @@ describe('Diff settings dropdown component', () => {
describe('compare changes', () => { describe('compare changes', () => {
it('sets inline button as selected', () => { it('sets inline button as selected', () => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) =>
diffViewType: INLINE_DIFF_VIEW_TYPE, Object.assign(origStore.state.diffs, { diffViewType: INLINE_DIFF_VIEW_TYPE }),
});
}); });
expect(wrapper.find('.js-inline-diff-button').classes('selected')).toBe(true); expect(wrapper.find('.js-inline-diff-button').classes('selected')).toBe(true);
...@@ -105,10 +93,9 @@ describe('Diff settings dropdown component', () => { ...@@ -105,10 +93,9 @@ describe('Diff settings dropdown component', () => {
}); });
it('sets parallel button as selected', () => { it('sets parallel button as selected', () => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) =>
diffViewType: PARALLEL_DIFF_VIEW_TYPE, Object.assign(origStore.state.diffs, { diffViewType: PARALLEL_DIFF_VIEW_TYPE }),
});
}); });
expect(wrapper.find('.js-inline-diff-button').classes('selected')).toBe(false); expect(wrapper.find('.js-inline-diff-button').classes('selected')).toBe(false);
...@@ -116,53 +103,49 @@ describe('Diff settings dropdown component', () => { ...@@ -116,53 +103,49 @@ describe('Diff settings dropdown component', () => {
}); });
it('calls setInlineDiffViewType when clicking inline button', () => { it('calls setInlineDiffViewType when clicking inline button', () => {
createComponent();
wrapper.find('.js-inline-diff-button').trigger('click'); wrapper.find('.js-inline-diff-button').trigger('click');
expect(actions.setInlineDiffViewType).toHaveBeenCalled(); expect(store.dispatch).toHaveBeenCalledWith('diffs/setInlineDiffViewType', expect.anything());
}); });
it('calls setParallelDiffViewType when clicking parallel button', () => { it('calls setParallelDiffViewType when clicking parallel button', () => {
createComponent();
wrapper.find('.js-parallel-diff-button').trigger('click'); wrapper.find('.js-parallel-diff-button').trigger('click');
expect(actions.setParallelDiffViewType).toHaveBeenCalled(); expect(store.dispatch).toHaveBeenCalledWith(
'diffs/setParallelDiffViewType',
expect.anything(),
);
}); });
}); });
describe('whitespace toggle', () => { describe('whitespace toggle', () => {
it('does not set as checked when showWhitespace is false', () => { it('does not set as checked when showWhitespace is false', () => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) =>
showWhitespace: false, Object.assign(origStore.state.diffs, { showWhitespace: false }),
});
}); });
expect(wrapper.find('#show-whitespace').element.checked).toBe(false); expect(wrapper.findByTestId('show-whitespace').element.checked).toBe(false);
}); });
it('sets as checked when showWhitespace is true', () => { it('sets as checked when showWhitespace is true', () => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) => Object.assign(origStore.state.diffs, { showWhitespace: true }),
showWhitespace: true,
});
}); });
expect(wrapper.find('#show-whitespace').element.checked).toBe(true); expect(wrapper.findByTestId('show-whitespace').element.checked).toBe(true);
}); });
it('calls setShowWhitespace on change', () => { it('calls setShowWhitespace on change', async () => {
createComponent(); const checkbox = wrapper.findByTestId('show-whitespace');
const { checked } = checkbox.element;
const checkbox = wrapper.find('#show-whitespace'); checkbox.trigger('click');
checkbox.element.checked = true; await vm.$nextTick();
checkbox.trigger('change');
expect(actions.setShowWhitespace).toHaveBeenCalledWith(expect.anything(), { expect(store.dispatch).toHaveBeenCalledWith('diffs/setShowWhitespace', {
showWhitespace: true, showWhitespace: !checked,
pushState: true, pushState: true,
}); });
}); });
...@@ -179,15 +162,12 @@ describe('Diff settings dropdown component', () => { ...@@ -179,15 +162,12 @@ describe('Diff settings dropdown component', () => {
${false} | ${false} ${false} | ${false}
`( `(
'sets the checkbox to { checked: $checked } if the fileByFile setting is $fileByFile', 'sets the checkbox to { checked: $checked } if the fileByFile setting is $fileByFile',
async ({ fileByFile, checked }) => { ({ fileByFile, checked }) => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) =>
viewDiffsFileByFile: fileByFile, Object.assign(origStore.state.diffs, { viewDiffsFileByFile: fileByFile }),
});
}); });
await vm.$nextTick();
expect(getFileByFileCheckbox(wrapper).element.checked).toBe(checked); expect(getFileByFileCheckbox(wrapper).element.checked).toBe(checked);
}, },
); );
...@@ -199,19 +179,16 @@ describe('Diff settings dropdown component', () => { ...@@ -199,19 +179,16 @@ describe('Diff settings dropdown component', () => {
`( `(
'when the file by file setting starts as $start, toggling the checkbox should call setFileByFile with $setting', 'when the file by file setting starts as $start, toggling the checkbox should call setFileByFile with $setting',
async ({ start, setting }) => { async ({ start, setting }) => {
createComponent((store) => { setup({
Object.assign(store.state.diffs, { storeUpdater: (origStore) =>
viewDiffsFileByFile: start, Object.assign(origStore.state.diffs, { viewDiffsFileByFile: start }),
});
}); });
await vm.$nextTick();
getFileByFileCheckbox(wrapper).trigger('click'); getFileByFileCheckbox(wrapper).trigger('click');
await vm.$nextTick(); await vm.$nextTick();
expect(actions.setFileByFile).toHaveBeenLastCalledWith(expect.anything(), { expect(store.dispatch).toHaveBeenCalledWith('diffs/setFileByFile', {
fileByFile: setting, fileByFile: setting,
}); });
}, },
......
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