debian-mirror-gitlab/spec/frontend/diffs/components/diff_file_spec.js

263 lines
8 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import Vue from 'vue';
2020-04-22 19:07:51 +05:30
import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import { mockTracking, triggerEvent } from 'helpers/tracking_helper';
2020-10-24 23:57:45 +05:30
import { createStore } from '~/mr_notes/stores';
2020-01-01 13:55:28 +05:30
import DiffFileComponent from '~/diffs/components/diff_file.vue';
import { diffViewerModes, diffViewerErrors } from '~/ide/constants';
2019-12-26 22:10:19 +05:30
import diffFileMockDataReadable from '../mock_data/diff_file';
import diffFileMockDataUnreadable from '../mock_data/diff_file_unreadable';
2018-11-08 19:23:39 +05:30
describe('DiffFile', () => {
let vm;
2020-03-13 15:44:24 +05:30
let trackingSpy;
2018-11-08 19:23:39 +05:30
beforeEach(() => {
2019-10-12 21:52:04 +05:30
vm = createComponentWithStore(Vue.extend(DiffFileComponent), createStore(), {
2019-12-26 22:10:19 +05:30
file: JSON.parse(JSON.stringify(diffFileMockDataReadable)),
2018-11-08 19:23:39 +05:30
canCurrentUserFork: false,
2020-07-28 23:09:34 +05:30
viewDiffsFileByFile: false,
2018-11-08 19:23:39 +05:30
}).$mount();
2020-04-22 19:07:51 +05:30
trackingSpy = mockTracking('_category_', vm.$el, jest.spyOn);
2018-11-08 19:23:39 +05:30
});
2019-10-12 21:52:04 +05:30
afterEach(() => {
vm.$destroy();
});
2020-04-08 14:13:33 +05:30
const findDiffContent = () => vm.$el.querySelector('.diff-content');
const isVisible = el => el.style.display !== 'none';
2018-11-08 19:23:39 +05:30
describe('template', () => {
2019-10-12 21:52:04 +05:30
it('should render component with file header, file content components', done => {
2018-11-08 19:23:39 +05:30
const el = vm.$el;
2019-02-15 15:39:39 +05:30
const { file_hash, file_path } = vm.file;
2018-11-08 19:23:39 +05:30
2019-02-15 15:39:39 +05:30
expect(el.id).toEqual(file_hash);
2018-11-08 19:23:39 +05:30
expect(el.classList.contains('diff-file')).toEqual(true);
2018-11-20 20:47:30 +05:30
2018-11-08 19:23:39 +05:30
expect(el.querySelectorAll('.diff-content.hidden').length).toEqual(0);
expect(el.querySelector('.js-file-title')).toBeDefined();
2021-01-03 14:25:43 +05:30
expect(el.querySelector('[data-testid="diff-file-copy-clipboard"]')).toBeDefined();
2019-02-15 15:39:39 +05:30
expect(el.querySelector('.file-title-name').innerText.indexOf(file_path)).toBeGreaterThan(-1);
2018-11-08 19:23:39 +05:30
expect(el.querySelector('.js-syntax-highlight')).toBeDefined();
2018-11-20 20:47:30 +05:30
vm.file.renderIt = true;
2019-10-12 21:52:04 +05:30
vm.$nextTick()
.then(() => {
2020-11-24 15:15:51 +05:30
expect(el.querySelectorAll('.line_content').length).toBe(8);
2019-10-12 21:52:04 +05:30
expect(el.querySelectorAll('.js-line-expansion-content').length).toBe(1);
2021-01-03 14:25:43 +05:30
triggerEvent('[data-testid="diff-file-copy-clipboard"]');
2020-03-13 15:44:24 +05:30
})
.then(done)
.catch(done.fail);
});
it('should track a click event on copy to clip board button', done => {
const el = vm.$el;
2021-01-03 14:25:43 +05:30
expect(el.querySelector('[data-testid="diff-file-copy-clipboard"]')).toBeDefined();
2020-03-13 15:44:24 +05:30
vm.file.renderIt = true;
vm.$nextTick()
.then(() => {
2021-01-03 14:25:43 +05:30
triggerEvent('[data-testid="diff-file-copy-clipboard"]');
2020-03-13 15:44:24 +05:30
expect(trackingSpy).toHaveBeenCalledWith('_category_', 'click_copy_file_button', {
label: 'diff_copy_file_path_button',
property: 'diff_copy_file',
});
2019-10-12 21:52:04 +05:30
})
.then(done)
.catch(done.fail);
2018-11-08 19:23:39 +05:30
});
describe('collapsed', () => {
it('should not have file content', done => {
2020-04-08 14:13:33 +05:30
expect(isVisible(findDiffContent())).toBe(true);
2019-07-07 11:18:12 +05:30
expect(vm.isCollapsed).toEqual(false);
vm.isCollapsed = true;
2018-11-20 20:47:30 +05:30
vm.file.renderIt = true;
2018-11-08 19:23:39 +05:30
vm.$nextTick(() => {
2020-04-08 14:13:33 +05:30
expect(isVisible(findDiffContent())).toBe(false);
2018-11-08 19:23:39 +05:30
done();
});
});
it('should have collapsed text and link', done => {
2019-07-07 11:18:12 +05:30
vm.renderIt = true;
vm.isCollapsed = true;
2018-11-20 20:47:30 +05:30
vm.$nextTick(() => {
expect(vm.$el.innerText).toContain('This diff is collapsed');
expect(vm.$el.querySelectorAll('.js-click-to-expand').length).toEqual(1);
done();
});
});
it('should have collapsed text and link even before rendered', done => {
2019-07-07 11:18:12 +05:30
vm.renderIt = false;
vm.isCollapsed = true;
2018-11-08 19:23:39 +05:30
vm.$nextTick(() => {
expect(vm.$el.innerText).toContain('This diff is collapsed');
expect(vm.$el.querySelectorAll('.js-click-to-expand').length).toEqual(1);
done();
});
});
2019-12-26 22:10:19 +05:30
it('should be collapsable for unreadable files', done => {
vm.$destroy();
vm = createComponentWithStore(Vue.extend(DiffFileComponent), createStore(), {
file: JSON.parse(JSON.stringify(diffFileMockDataUnreadable)),
canCurrentUserFork: false,
2020-07-28 23:09:34 +05:30
viewDiffsFileByFile: false,
2019-12-26 22:10:19 +05:30
}).$mount();
vm.renderIt = false;
vm.isCollapsed = true;
vm.$nextTick(() => {
expect(vm.$el.innerText).toContain('This diff is collapsed');
expect(vm.$el.querySelectorAll('.js-click-to-expand').length).toEqual(1);
done();
});
});
2019-02-15 15:39:39 +05:30
it('should be collapsed for renamed files', done => {
2019-07-07 11:18:12 +05:30
vm.renderIt = true;
vm.isCollapsed = false;
2019-02-15 15:39:39 +05:30
vm.file.highlighted_diff_lines = null;
2019-07-07 11:18:12 +05:30
vm.file.viewer.name = diffViewerModes.renamed;
2019-02-15 15:39:39 +05:30
vm.$nextTick(() => {
expect(vm.$el.innerText).not.toContain('This diff is collapsed');
done();
});
});
it('should be collapsed for mode changed files', done => {
2019-07-07 11:18:12 +05:30
vm.renderIt = true;
vm.isCollapsed = false;
2019-02-15 15:39:39 +05:30
vm.file.highlighted_diff_lines = null;
2019-07-07 11:18:12 +05:30
vm.file.viewer.name = diffViewerModes.mode_changed;
2019-02-15 15:39:39 +05:30
vm.$nextTick(() => {
expect(vm.$el.innerText).not.toContain('This diff is collapsed');
done();
});
});
2018-11-08 19:23:39 +05:30
it('should have loading icon while loading a collapsed diffs', done => {
2019-07-07 11:18:12 +05:30
vm.isCollapsed = true;
2018-11-08 19:23:39 +05:30
vm.isLoadingCollapsedDiff = true;
vm.$nextTick(() => {
expect(vm.$el.querySelectorAll('.diff-content.loading').length).toEqual(1);
done();
});
});
2019-07-07 11:18:12 +05:30
it('should update store state', done => {
2020-04-22 19:07:51 +05:30
jest.spyOn(vm.$store, 'dispatch').mockImplementation(() => {});
2019-07-07 11:18:12 +05:30
vm.isCollapsed = true;
vm.$nextTick(() => {
expect(vm.$store.dispatch).toHaveBeenCalledWith('diffs/setFileCollapsed', {
filePath: vm.file.file_path,
collapsed: true,
});
done();
});
});
it('updates local state when changing file state', done => {
2021-01-03 14:25:43 +05:30
vm.file.viewer.automaticallyCollapsed = true;
2019-07-07 11:18:12 +05:30
vm.$nextTick(() => {
expect(vm.isCollapsed).toBe(true);
done();
});
});
2018-11-08 19:23:39 +05:30
});
});
describe('too large diff', () => {
it('should have too large warning and blob link', done => {
const BLOB_LINK = '/file/view/path';
2019-07-07 11:18:12 +05:30
vm.file.viewer.error = diffViewerErrors.too_large;
vm.file.viewer.error_message =
'This source diff could not be displayed because it is too large';
2019-02-15 15:39:39 +05:30
vm.file.view_path = BLOB_LINK;
2019-07-07 11:18:12 +05:30
vm.file.renderIt = true;
2018-11-08 19:23:39 +05:30
vm.$nextTick(() => {
expect(vm.$el.innerText).toContain(
'This source diff could not be displayed because it is too large',
);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
done();
});
});
});
2018-12-13 13:39:08 +05:30
describe('watch collapsed', () => {
it('calls handleLoadCollapsedDiff if collapsed changed & file has no lines', done => {
2020-04-22 19:07:51 +05:30
jest.spyOn(vm, 'handleLoadCollapsedDiff').mockImplementation(() => {});
2018-12-13 13:39:08 +05:30
2020-11-24 15:15:51 +05:30
vm.file.highlighted_diff_lines = [];
2019-02-15 15:39:39 +05:30
vm.file.parallel_diff_lines = [];
2019-07-07 11:18:12 +05:30
vm.isCollapsed = true;
2018-12-13 13:39:08 +05:30
vm.$nextTick()
.then(() => {
2019-07-07 11:18:12 +05:30
vm.isCollapsed = false;
2018-12-13 13:39:08 +05:30
return vm.$nextTick();
})
.then(() => {
expect(vm.handleLoadCollapsedDiff).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
});
2019-12-26 22:10:19 +05:30
it('does not call handleLoadCollapsedDiff if collapsed changed & file is unreadable', done => {
vm.$destroy();
vm = createComponentWithStore(Vue.extend(DiffFileComponent), createStore(), {
file: JSON.parse(JSON.stringify(diffFileMockDataUnreadable)),
canCurrentUserFork: false,
2020-07-28 23:09:34 +05:30
viewDiffsFileByFile: false,
2019-12-26 22:10:19 +05:30
}).$mount();
2020-04-22 19:07:51 +05:30
jest.spyOn(vm, 'handleLoadCollapsedDiff').mockImplementation(() => {});
2019-12-26 22:10:19 +05:30
2020-11-24 15:15:51 +05:30
vm.file.highlighted_diff_lines = [];
vm.file.parallel_diff_lines = undefined;
2019-12-26 22:10:19 +05:30
vm.isCollapsed = true;
vm.$nextTick()
.then(() => {
vm.isCollapsed = false;
return vm.$nextTick();
})
.then(() => {
expect(vm.handleLoadCollapsedDiff).not.toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
});
2018-12-13 13:39:08 +05:30
});
2018-11-08 19:23:39 +05:30
});