Commit c05c48e8 authored by Tim Zallmann's avatar Tim Zallmann

Merge branch '202196-show-image-file-size-in-diff-view' into 'master'

Show image file size in diff view

See merge request gitlab-org/gitlab!25734
parents 98e21104 71ac09ac
...@@ -112,6 +112,7 @@ export default { ...@@ -112,6 +112,7 @@ export default {
<div class="image"> <div class="image">
<image-viewer <image-viewer
:path="imagePath" :path="imagePath"
:file-size="isNew ? newSize : oldSize"
:inner-css-classes="[ :inner-css-classes="[
'frame', 'frame',
{ {
......
---
title: All image diffs (except for renamed files) show the image file size in the
diff
merge_request: 25734
author:
type: added
import Vue from 'vue'; import Vue from 'vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import { compileToFunctions } from 'vue-template-compiler';
import { mount } from '@vue/test-utils';
import { GREEN_BOX_IMAGE_URL, RED_BOX_IMAGE_URL } from 'spec/test_constants'; import { GREEN_BOX_IMAGE_URL, RED_BOX_IMAGE_URL } from 'spec/test_constants';
import imageDiffViewer from '~/vue_shared/components/diff_viewer/viewers/image_diff_viewer.vue'; import imageDiffViewer from '~/vue_shared/components/diff_viewer/viewers/image_diff_viewer.vue';
...@@ -9,50 +11,52 @@ describe('ImageDiffViewer', () => { ...@@ -9,50 +11,52 @@ describe('ImageDiffViewer', () => {
newPath: GREEN_BOX_IMAGE_URL, newPath: GREEN_BOX_IMAGE_URL,
oldPath: RED_BOX_IMAGE_URL, oldPath: RED_BOX_IMAGE_URL,
}; };
const allProps = {
...requiredProps,
oldSize: 2048,
newSize: 1024,
};
let wrapper;
let vm; let vm;
function createComponent(props) { function createComponent(props) {
const ImageDiffViewer = Vue.extend(imageDiffViewer); const ImageDiffViewer = Vue.extend(imageDiffViewer);
vm = mountComponent(ImageDiffViewer, props); wrapper = mount(ImageDiffViewer, { propsData: props });
vm = wrapper.vm;
} }
const triggerEvent = (eventName, el = vm.$el, clientX = 0) => { const triggerEvent = (eventName, el = vm.$el, clientX = 0) => {
const event = document.createEvent('MouseEvents'); const event = new MouseEvent(eventName, {
event.initMouseEvent( bubbles: true,
eventName, cancelable: true,
true, view: window,
true, detail: 1,
window, screenX: clientX,
1,
clientX, clientX,
0, });
clientX,
0, // JSDOM does not implement experimental APIs
false, event.pageX = clientX;
false,
false,
false,
0,
null,
);
el.dispatchEvent(event); el.dispatchEvent(event);
}; };
const dragSlider = (sliderElement, dragPixel = 20) => { const dragSlider = (sliderElement, doc, dragPixel) => {
triggerEvent('mousedown', sliderElement); triggerEvent('mousedown', sliderElement);
triggerEvent('mousemove', document.body, dragPixel); triggerEvent('mousemove', doc.body, dragPixel);
triggerEvent('mouseup', document.body); triggerEvent('mouseup', doc.body);
}; };
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
}); });
it('renders image diff for replaced', done => { it('renders image diff for replaced', done => {
createComponent(requiredProps); createComponent({ ...allProps });
vm.$nextTick(() => {
const metaInfoElements = vm.$el.querySelectorAll('.image-info');
setTimeout(() => {
expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL); expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL);
expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(RED_BOX_IMAGE_URL); expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(RED_BOX_IMAGE_URL);
...@@ -66,35 +70,35 @@ describe('ImageDiffViewer', () => { ...@@ -66,35 +70,35 @@ describe('ImageDiffViewer', () => {
'Onion skin', 'Onion skin',
); );
expect(metaInfoElements.length).toBe(2);
expect(metaInfoElements[0]).toHaveText('2.00 KiB');
expect(metaInfoElements[1]).toHaveText('1.00 KiB');
done(); done();
}); });
}); });
it('renders image diff for new', done => { it('renders image diff for new', done => {
createComponent( createComponent({ ...allProps, diffMode: 'new', oldPath: '' });
Object.assign({}, requiredProps, {
diffMode: 'new', setImmediate(() => {
oldPath: '', const metaInfoElement = vm.$el.querySelector('.image-info');
}),
);
setTimeout(() => {
expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL); expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL);
expect(metaInfoElement).toHaveText('1.00 KiB');
done(); done();
}); });
}); });
it('renders image diff for deleted', done => { it('renders image diff for deleted', done => {
createComponent( createComponent({ ...allProps, diffMode: 'deleted', newPath: '' });
Object.assign({}, requiredProps, {
diffMode: 'deleted', setImmediate(() => {
newPath: '', const metaInfoElement = vm.$el.querySelector('.image-info');
}),
);
setTimeout(() => {
expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(RED_BOX_IMAGE_URL); expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(RED_BOX_IMAGE_URL);
expect(metaInfoElement).toHaveText('2.00 KiB');
done(); done();
}); });
...@@ -105,26 +109,40 @@ describe('ImageDiffViewer', () => { ...@@ -105,26 +109,40 @@ describe('ImageDiffViewer', () => {
components: { components: {
imageDiffViewer, imageDiffViewer,
}, },
template: ` data: {
<image-diff-viewer diff-mode="renamed" new-path="${GREEN_BOX_IMAGE_URL}" old-path=""> ...allProps,
diffMode: 'renamed',
},
...compileToFunctions(`
<image-diff-viewer
:diff-mode="diffMode"
:new-path="newPath"
:old-path="oldPath"
:new-size="newSize"
:old-size="oldSize"
>
<span slot="image-overlay" class="overlay">test</span> <span slot="image-overlay" class="overlay">test</span>
</image-diff-viewer> </image-diff-viewer>
`, `),
}).$mount(); }).$mount();
setTimeout(() => { setImmediate(() => {
const metaInfoElement = vm.$el.querySelector('.image-info');
expect(vm.$el.querySelector('img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL); expect(vm.$el.querySelector('img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL);
expect(vm.$el.querySelector('.overlay')).not.toBe(null); expect(vm.$el.querySelector('.overlay')).not.toBe(null);
expect(metaInfoElement).toHaveText('2.00 KiB');
done(); done();
}); });
}); });
describe('swipeMode', () => { describe('swipeMode', () => {
beforeEach(done => { beforeEach(done => {
createComponent(requiredProps); createComponent({ ...requiredProps });
setTimeout(() => { setImmediate(() => {
done(); done();
}); });
}); });
...@@ -141,9 +159,9 @@ describe('ImageDiffViewer', () => { ...@@ -141,9 +159,9 @@ describe('ImageDiffViewer', () => {
describe('onionSkin', () => { describe('onionSkin', () => {
beforeEach(done => { beforeEach(done => {
createComponent(requiredProps); createComponent({ ...requiredProps });
setTimeout(() => { setImmediate(() => {
done(); done();
}); });
}); });
...@@ -163,7 +181,7 @@ describe('ImageDiffViewer', () => { ...@@ -163,7 +181,7 @@ describe('ImageDiffViewer', () => {
vm.$el.querySelector('.view-modes-menu li:nth-child(3)').click(); vm.$el.querySelector('.view-modes-menu li:nth-child(3)').click();
vm.$nextTick(() => { vm.$nextTick(() => {
dragSlider(vm.$el.querySelector('.dragger')); dragSlider(vm.$el.querySelector('.dragger'), document, 20);
vm.$nextTick(() => { vm.$nextTick(() => {
expect(vm.$el.querySelector('.dragger').style.left).toBe('20px'); expect(vm.$el.querySelector('.dragger').style.left).toBe('20px');
......
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