Commit 3709b852 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Merge branch '349389-fix-image-diff-saving' into 'master'

Fix occasional failure of adding image diff note

See merge request gitlab-org/gitlab!77864
parents 7854d4e8 a2e81cec
...@@ -4,8 +4,8 @@ import { isArray } from 'lodash'; ...@@ -4,8 +4,8 @@ import { isArray } from 'lodash';
import { mapActions, mapGetters } from 'vuex'; import { mapActions, mapGetters } from 'vuex';
import imageDiffMixin from 'ee_else_ce/diffs/mixins/image_diff'; import imageDiffMixin from 'ee_else_ce/diffs/mixins/image_diff';
function calcPercent(pos, size, renderedSize) { function calcPercent(pos, renderedSize) {
return (((pos / size) * 100) / ((renderedSize / size) * 100)) * 100; return (100 * pos) / renderedSize;
} }
export default { export default {
...@@ -65,8 +65,8 @@ export default { ...@@ -65,8 +65,8 @@ export default {
...mapActions('diffs', ['openDiffFileCommentForm']), ...mapActions('diffs', ['openDiffFileCommentForm']),
getImageDimensions() { getImageDimensions() {
return { return {
width: this.$parent.width, width: Math.round(this.$parent.width),
height: this.$parent.height, height: Math.round(this.$parent.height),
}; };
}, },
getPositionForObject(meta) { getPositionForObject(meta) {
...@@ -87,15 +87,15 @@ export default { ...@@ -87,15 +87,15 @@ export default {
}, },
clickedImage(x, y) { clickedImage(x, y) {
const { width, height } = this.getImageDimensions(); const { width, height } = this.getImageDimensions();
const xPercent = calcPercent(x, width, this.renderedWidth); const xPercent = calcPercent(x, this.renderedWidth);
const yPercent = calcPercent(y, height, this.renderedHeight); const yPercent = calcPercent(y, this.renderedHeight);
this.openDiffFileCommentForm({ this.openDiffFileCommentForm({
fileHash: this.fileHash, fileHash: this.fileHash,
width, width,
height, height,
x: width * (xPercent / 100), x: Math.round(width * (xPercent / 100)),
y: height * (yPercent / 100), y: Math.round(height * (yPercent / 100)),
xPercent, xPercent,
yPercent, yPercent,
}); });
......
...@@ -6,8 +6,8 @@ import { imageDiffDiscussions } from '../mock_data/diff_discussions'; ...@@ -6,8 +6,8 @@ import { imageDiffDiscussions } from '../mock_data/diff_discussions';
describe('Diffs image diff overlay component', () => { describe('Diffs image diff overlay component', () => {
const dimensions = { const dimensions = {
width: 100, width: 99.9,
height: 200, height: 199.5,
}; };
let wrapper; let wrapper;
let dispatch; let dispatch;
...@@ -38,7 +38,6 @@ describe('Diffs image diff overlay component', () => { ...@@ -38,7 +38,6 @@ describe('Diffs image diff overlay component', () => {
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null;
}); });
it('renders comment badges', () => { it('renders comment badges', () => {
...@@ -81,17 +80,21 @@ describe('Diffs image diff overlay component', () => { ...@@ -81,17 +80,21 @@ describe('Diffs image diff overlay component', () => {
it('dispatches openDiffFileCommentForm when clicking overlay', () => { it('dispatches openDiffFileCommentForm when clicking overlay', () => {
createComponent({ canComment: true }); createComponent({ canComment: true });
wrapper.find('.js-add-image-diff-note-button').trigger('click', { offsetX: 0, offsetY: 0 }); wrapper.find('.js-add-image-diff-note-button').trigger('click', { offsetX: 1.2, offsetY: 3.8 });
expect(dispatch).toHaveBeenCalledWith('diffs/openDiffFileCommentForm', { expect(dispatch).toHaveBeenCalledWith('diffs/openDiffFileCommentForm', {
fileHash: 'ABC', fileHash: 'ABC',
x: 0, x: 1,
y: 0, y: 4,
width: 100, width: 100,
height: 200, height: 200,
xPercent: 0, xPercent: expect.any(Number),
yPercent: 0, yPercent: expect.any(Number),
}); });
const { xPercent, yPercent } = dispatch.mock.calls[0][1];
expect(xPercent).toBeCloseTo(0.6);
expect(yPercent).toBeCloseTo(1.9);
}); });
describe('toggle discussion', () => { describe('toggle discussion', () => {
......
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