2021-02-24 16:11:16 -05:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
|
|
|
|
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
|
|
|
|
|
2019-01-24 09:48:03 -05:00
|
|
|
import SettingsDropdown from '~/diffs/components/settings_dropdown.vue';
|
2021-02-22 10:10:48 -05:00
|
|
|
import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants';
|
2020-12-02 16:09:44 -05:00
|
|
|
import eventHub from '~/diffs/event_hub';
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
import createDiffsStore from '../create_diffs_store';
|
2019-11-26 19:06:23 -05:00
|
|
|
|
2020-09-02 05:10:23 -04:00
|
|
|
describe('Diff settings dropdown component', () => {
|
2020-12-02 16:09:44 -05:00
|
|
|
let wrapper;
|
2021-02-24 16:11:16 -05:00
|
|
|
let store;
|
2019-01-24 09:48:03 -05:00
|
|
|
|
|
|
|
function createComponent(extendStore = () => {}) {
|
2021-02-24 16:11:16 -05:00
|
|
|
store = createDiffsStore();
|
2019-01-24 09:48:03 -05:00
|
|
|
|
|
|
|
extendStore(store);
|
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
wrapper = extendedWrapper(
|
|
|
|
mount(SettingsDropdown, {
|
|
|
|
store,
|
|
|
|
}),
|
|
|
|
);
|
2020-12-02 16:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getFileByFileCheckbox(vueWrapper) {
|
2021-02-24 16:11:16 -05:00
|
|
|
return vueWrapper.findByTestId('file-by-file');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup({ storeUpdater } = {}) {
|
|
|
|
createComponent(storeUpdater);
|
|
|
|
jest.spyOn(store, 'dispatch').mockImplementation(() => {});
|
2019-01-24 09:48:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup();
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2021-02-24 16:11:16 -05:00
|
|
|
store.dispatch.mockRestore();
|
2020-12-02 16:09:44 -05:00
|
|
|
wrapper.destroy();
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('tree view buttons', () => {
|
|
|
|
it('list view button dispatches setRenderTreeList with false', () => {
|
2020-12-02 16:09:44 -05:00
|
|
|
wrapper.find('.js-list-view').trigger('click');
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('diffs/setRenderTreeList', {
|
|
|
|
renderTreeList: false,
|
|
|
|
});
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tree view button dispatches setRenderTreeList with true', () => {
|
2020-12-02 16:09:44 -05:00
|
|
|
wrapper.find('.js-tree-view').trigger('click');
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('diffs/setRenderTreeList', {
|
|
|
|
renderTreeList: true,
|
|
|
|
});
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-09-02 05:10:23 -04:00
|
|
|
it('sets list button as selected when renderTreeList is false', () => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) =>
|
|
|
|
Object.assign(origStore.state.diffs, { renderTreeList: false }),
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-12-02 16:09:44 -05:00
|
|
|
expect(wrapper.find('.js-list-view').classes('selected')).toBe(true);
|
|
|
|
expect(wrapper.find('.js-tree-view').classes('selected')).toBe(false);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-09-02 05:10:23 -04:00
|
|
|
it('sets tree button as selected when renderTreeList is true', () => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) => Object.assign(origStore.state.diffs, { renderTreeList: true }),
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-12-02 16:09:44 -05:00
|
|
|
expect(wrapper.find('.js-list-view').classes('selected')).toBe(false);
|
|
|
|
expect(wrapper.find('.js-tree-view').classes('selected')).toBe(true);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('compare changes', () => {
|
2020-09-02 05:10:23 -04:00
|
|
|
it('sets inline button as selected', () => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) =>
|
|
|
|
Object.assign(origStore.state.diffs, { diffViewType: INLINE_DIFF_VIEW_TYPE }),
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-12-02 16:09:44 -05:00
|
|
|
expect(wrapper.find('.js-inline-diff-button').classes('selected')).toBe(true);
|
|
|
|
expect(wrapper.find('.js-parallel-diff-button').classes('selected')).toBe(false);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-09-02 05:10:23 -04:00
|
|
|
it('sets parallel button as selected', () => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) =>
|
|
|
|
Object.assign(origStore.state.diffs, { diffViewType: PARALLEL_DIFF_VIEW_TYPE }),
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2020-12-02 16:09:44 -05:00
|
|
|
expect(wrapper.find('.js-inline-diff-button').classes('selected')).toBe(false);
|
|
|
|
expect(wrapper.find('.js-parallel-diff-button').classes('selected')).toBe(true);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls setInlineDiffViewType when clicking inline button', () => {
|
2020-12-02 16:09:44 -05:00
|
|
|
wrapper.find('.js-inline-diff-button').trigger('click');
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('diffs/setInlineDiffViewType', expect.anything());
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls setParallelDiffViewType when clicking parallel button', () => {
|
2020-12-02 16:09:44 -05:00
|
|
|
wrapper.find('.js-parallel-diff-button').trigger('click');
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith(
|
|
|
|
'diffs/setParallelDiffViewType',
|
|
|
|
expect.anything(),
|
|
|
|
);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('whitespace toggle', () => {
|
|
|
|
it('does not set as checked when showWhitespace is false', () => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) =>
|
|
|
|
Object.assign(origStore.state.diffs, { showWhitespace: false }),
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
expect(wrapper.findByTestId('show-whitespace').element.checked).toBe(false);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sets as checked when showWhitespace is true', () => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) => Object.assign(origStore.state.diffs, { showWhitespace: true }),
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
expect(wrapper.findByTestId('show-whitespace').element.checked).toBe(true);
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
it('calls setShowWhitespace on change', async () => {
|
|
|
|
const checkbox = wrapper.findByTestId('show-whitespace');
|
|
|
|
const { checked } = checkbox.element;
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2022-07-14 17:09:55 -04:00
|
|
|
await checkbox.setChecked(false);
|
2019-01-24 09:48:03 -05:00
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('diffs/setShowWhitespace', {
|
|
|
|
showWhitespace: !checked,
|
2020-09-04 08:08:27 -04:00
|
|
|
});
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|
|
|
|
});
|
2020-12-02 16:09:44 -05:00
|
|
|
|
|
|
|
describe('file-by-file toggle', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(eventHub, '$emit');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
fileByFile | checked
|
|
|
|
${true} | ${true}
|
|
|
|
${false} | ${false}
|
|
|
|
`(
|
2020-12-04 13:09:55 -05:00
|
|
|
'sets the checkbox to { checked: $checked } if the fileByFile setting is $fileByFile',
|
2021-02-24 16:11:16 -05:00
|
|
|
({ fileByFile, checked }) => {
|
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) =>
|
|
|
|
Object.assign(origStore.state.diffs, { viewDiffsFileByFile: fileByFile }),
|
2020-12-02 16:09:44 -05:00
|
|
|
});
|
|
|
|
|
2020-12-04 13:09:55 -05:00
|
|
|
expect(getFileByFileCheckbox(wrapper).element.checked).toBe(checked);
|
2020-12-02 16:09:44 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it.each`
|
2021-02-22 10:10:48 -05:00
|
|
|
start | setting
|
2020-12-02 16:09:44 -05:00
|
|
|
${true} | ${false}
|
|
|
|
${false} | ${true}
|
|
|
|
`(
|
2021-02-22 10:10:48 -05:00
|
|
|
'when the file by file setting starts as $start, toggling the checkbox should call setFileByFile with $setting',
|
|
|
|
async ({ start, setting }) => {
|
2021-02-24 16:11:16 -05:00
|
|
|
setup({
|
|
|
|
storeUpdater: (origStore) =>
|
|
|
|
Object.assign(origStore.state.diffs, { viewDiffsFileByFile: start }),
|
2020-12-02 16:09:44 -05:00
|
|
|
});
|
|
|
|
|
2022-07-14 17:09:55 -04:00
|
|
|
await getFileByFileCheckbox(wrapper).setChecked(setting);
|
2020-12-02 16:09:44 -05:00
|
|
|
|
2021-02-24 16:11:16 -05:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('diffs/setFileByFile', {
|
2021-02-22 10:10:48 -05:00
|
|
|
fileByFile: setting,
|
|
|
|
});
|
2020-12-02 16:09:44 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2019-01-24 09:48:03 -05:00
|
|
|
});
|