Commit 24ed1470 authored by Clement Ho's avatar Clement Ho

Merge branch 'fix-image-spec-recursive' into 'master'

Fix recursive dependency in image_diff

See merge request gitlab-org/gitlab!16689
parents 2b5d5814 e3e3329e
...@@ -5,7 +5,7 @@ import { __ } from '~/locale'; ...@@ -5,7 +5,7 @@ import { __ } from '~/locale';
import { getLocationHash } from './lib/utils/url_utility'; import { getLocationHash } from './lib/utils/url_utility';
import FilesCommentButton from './files_comment_button'; import FilesCommentButton from './files_comment_button';
import SingleFileDiff from './single_file_diff'; import SingleFileDiff from './single_file_diff';
import imageDiffHelper from './image_diff/helpers/index'; import initImageDiffHelper from './image_diff/helpers/init_image_diff';
const UNFOLD_COUNT = 20; const UNFOLD_COUNT = 20;
let isBound = false; let isBound = false;
...@@ -28,7 +28,7 @@ export default class Diff { ...@@ -28,7 +28,7 @@ export default class Diff {
.first() .first()
.get(0); .get(0);
const canCreateNote = firstFile && firstFile.hasAttribute('data-can-create-note'); const canCreateNote = firstFile && firstFile.hasAttribute('data-can-create-note');
$diffFile.each((index, file) => imageDiffHelper.initImageDiff(file, canCreateNote)); $diffFile.each((index, file) => initImageDiffHelper.initImageDiff(file, canCreateNote));
if (!isBound) { if (!isBound) {
$(document) $(document)
......
...@@ -21,5 +21,4 @@ export default { ...@@ -21,5 +21,4 @@ export default {
resizeCoordinatesToImageElement: utilsHelper.resizeCoordinatesToImageElement, resizeCoordinatesToImageElement: utilsHelper.resizeCoordinatesToImageElement,
generateBadgeFromDiscussionDOM: utilsHelper.generateBadgeFromDiscussionDOM, generateBadgeFromDiscussionDOM: utilsHelper.generateBadgeFromDiscussionDOM,
getTargetSelection: utilsHelper.getTargetSelection, getTargetSelection: utilsHelper.getTargetSelection,
initImageDiff: utilsHelper.initImageDiff,
}; };
import ImageDiff from '../image_diff';
import ReplacedImageDiff from '../replaced_image_diff';
import ImageFile from '../../commit/image_file';
function initImageDiff(fileEl, canCreateNote, renderCommentBadge) {
const options = {
canCreateNote,
renderCommentBadge,
};
let diff;
// ImageFile needs to be invoked before initImageDiff so that badges
// can mount to the correct location
new ImageFile(fileEl); // eslint-disable-line no-new
if (fileEl.querySelector('.diff-file .js-single-image')) {
diff = new ImageDiff(fileEl, options);
diff.init();
} else if (fileEl.querySelector('.diff-file .js-replaced-image')) {
diff = new ReplacedImageDiff(fileEl, options);
diff.init();
}
return diff;
}
export default { initImageDiff };
import ImageBadge from '../image_badge'; import ImageBadge from '../image_badge';
import ImageDiff from '../image_diff';
import ReplacedImageDiff from '../replaced_image_diff';
import ImageFile from '../../commit/image_file';
export function resizeCoordinatesToImageElement(imageEl, meta) { export function resizeCoordinatesToImageElement(imageEl, meta) {
const { x, y, width, height } = meta; const { x, y, width, height } = meta;
...@@ -70,25 +67,3 @@ export function getTargetSelection(event) { ...@@ -70,25 +67,3 @@ export function getTargetSelection(event) {
}, },
}; };
} }
export function initImageDiff(fileEl, canCreateNote, renderCommentBadge) {
const options = {
canCreateNote,
renderCommentBadge,
};
let diff;
// ImageFile needs to be invoked before initImageDiff so that badges
// can mount to the correct location
new ImageFile(fileEl); // eslint-disable-line no-new
if (fileEl.querySelector('.diff-file .js-single-image')) {
diff = new ImageDiff(fileEl, options);
diff.init();
} else if (fileEl.querySelector('.diff-file .js-replaced-image')) {
diff = new ReplacedImageDiff(fileEl, options);
diff.init();
}
return diff;
}
import imageDiffHelper from './helpers/index'; import initImageDiffHelper from './helpers/init_image_diff';
export default () => { export default () => {
// Always pass can-create-note as false because a user // Always pass can-create-note as false because a user
...@@ -8,6 +8,6 @@ export default () => { ...@@ -8,6 +8,6 @@ export default () => {
const diffFileEls = document.querySelectorAll('.timeline-content .diff-file.js-image-file'); const diffFileEls = document.querySelectorAll('.timeline-content .diff-file.js-image-file');
[...diffFileEls].forEach(diffFileEl => [...diffFileEls].forEach(diffFileEl =>
imageDiffHelper.initImageDiff(diffFileEl, canCreateNote, renderCommentBadge), initImageDiffHelper.initImageDiff(diffFileEl, canCreateNote, renderCommentBadge),
); );
}; };
...@@ -5,7 +5,7 @@ import { __ } from './locale'; ...@@ -5,7 +5,7 @@ import { __ } from './locale';
import axios from './lib/utils/axios_utils'; import axios from './lib/utils/axios_utils';
import createFlash from './flash'; import createFlash from './flash';
import FilesCommentButton from './files_comment_button'; import FilesCommentButton from './files_comment_button';
import imageDiffHelper from './image_diff/helpers/index'; import initImageDiffHelper from './image_diff/helpers/init_image_diff';
import syntaxHighlight from './syntax_highlight'; import syntaxHighlight from './syntax_highlight';
const WRAPPER = '<div class="diff-content"></div>'; const WRAPPER = '<div class="diff-content"></div>';
...@@ -101,7 +101,7 @@ export default class SingleFileDiff { ...@@ -101,7 +101,7 @@ export default class SingleFileDiff {
FilesCommentButton.init($file); FilesCommentButton.init($file);
const canCreateNote = $file.closest('.files').is('[data-can-create-note]'); const canCreateNote = $file.closest('.files').is('[data-can-create-note]');
imageDiffHelper.initImageDiff($file[0], canCreateNote); initImageDiffHelper.initImageDiff($file[0], canCreateNote);
if (cb) cb(); if (cb) cb();
}) })
......
import initImageDiffHelper from '~/image_diff/helpers/init_image_diff';
import ImageDiff from '~/image_diff/image_diff';
import ReplacedImageDiff from '~/image_diff/replaced_image_diff';
describe('initImageDiff', () => {
let glCache;
let fileEl;
beforeEach(() => {
window.gl = window.gl || (window.gl = {});
glCache = window.gl;
fileEl = document.createElement('div');
fileEl.innerHTML = `
<div class="diff-file"></div>
`;
spyOn(ReplacedImageDiff.prototype, 'init').and.callFake(() => {});
spyOn(ImageDiff.prototype, 'init').and.callFake(() => {});
});
afterEach(() => {
window.gl = glCache;
});
it('should initialize ImageDiff if js-single-image', () => {
const diffFileEl = fileEl.querySelector('.diff-file');
diffFileEl.innerHTML = `
<div class="js-single-image">
</div>
`;
const imageDiff = initImageDiffHelper.initImageDiff(fileEl, true, false);
expect(ImageDiff.prototype.init).toHaveBeenCalled();
expect(imageDiff.canCreateNote).toEqual(true);
expect(imageDiff.renderCommentBadge).toEqual(false);
});
it('should initialize ReplacedImageDiff if js-replaced-image', () => {
const diffFileEl = fileEl.querySelector('.diff-file');
diffFileEl.innerHTML = `
<div class="js-replaced-image">
</div>
`;
const replacedImageDiff = initImageDiffHelper.initImageDiff(fileEl, false, true);
expect(ReplacedImageDiff.prototype.init).toHaveBeenCalled();
expect(replacedImageDiff.canCreateNote).toEqual(false);
expect(replacedImageDiff.renderCommentBadge).toEqual(true);
});
});
import * as utilsHelper from '~/image_diff/helpers/utils_helper'; import * as utilsHelper from '~/image_diff/helpers/utils_helper';
import ImageDiff from '~/image_diff/image_diff';
import ReplacedImageDiff from '~/image_diff/replaced_image_diff';
import ImageBadge from '~/image_diff/image_badge'; import ImageBadge from '~/image_diff/image_badge';
import * as mockData from '../mock_data'; import * as mockData from '../mock_data';
...@@ -151,53 +149,4 @@ describe('utilsHelper', () => { ...@@ -151,53 +149,4 @@ describe('utilsHelper', () => {
}); });
}); });
}); });
describe('initImageDiff', () => {
let glCache;
let fileEl;
beforeEach(() => {
window.gl = window.gl || (window.gl = {});
glCache = window.gl;
fileEl = document.createElement('div');
fileEl.innerHTML = `
<div class="diff-file"></div>
`;
spyOn(ReplacedImageDiff.prototype, 'init').and.callFake(() => {});
spyOn(ImageDiff.prototype, 'init').and.callFake(() => {});
});
afterEach(() => {
window.gl = glCache;
});
it('should initialize ImageDiff if js-single-image', () => {
const diffFileEl = fileEl.querySelector('.diff-file');
diffFileEl.innerHTML = `
<div class="js-single-image">
</div>
`;
const imageDiff = utilsHelper.initImageDiff(fileEl, true, false);
expect(ImageDiff.prototype.init).toHaveBeenCalled();
expect(imageDiff.canCreateNote).toEqual(true);
expect(imageDiff.renderCommentBadge).toEqual(false);
});
it('should initialize ReplacedImageDiff if js-replaced-image', () => {
const diffFileEl = fileEl.querySelector('.diff-file');
diffFileEl.innerHTML = `
<div class="js-replaced-image">
</div>
`;
const replacedImageDiff = utilsHelper.initImageDiff(fileEl, false, true);
expect(ReplacedImageDiff.prototype.init).toHaveBeenCalled();
expect(replacedImageDiff.canCreateNote).toEqual(false);
expect(replacedImageDiff.renderCommentBadge).toEqual(true);
});
});
}); });
import initDiscussionTab from '~/image_diff/init_discussion_tab'; import initDiscussionTab from '~/image_diff/init_discussion_tab';
import imageDiffHelper from '~/image_diff/helpers/index'; import initImageDiffHelper from '~/image_diff/helpers/init_image_diff';
describe('initDiscussionTab', () => { describe('initDiscussionTab', () => {
beforeEach(() => { beforeEach(() => {
...@@ -12,7 +12,7 @@ describe('initDiscussionTab', () => { ...@@ -12,7 +12,7 @@ describe('initDiscussionTab', () => {
}); });
it('should pass canCreateNote as false to initImageDiff', done => { it('should pass canCreateNote as false to initImageDiff', done => {
spyOn(imageDiffHelper, 'initImageDiff').and.callFake((diffFileEl, canCreateNote) => { spyOn(initImageDiffHelper, 'initImageDiff').and.callFake((diffFileEl, canCreateNote) => {
expect(canCreateNote).toEqual(false); expect(canCreateNote).toEqual(false);
done(); done();
}); });
...@@ -21,7 +21,7 @@ describe('initDiscussionTab', () => { ...@@ -21,7 +21,7 @@ describe('initDiscussionTab', () => {
}); });
it('should pass renderCommentBadge as true to initImageDiff', done => { it('should pass renderCommentBadge as true to initImageDiff', done => {
spyOn(imageDiffHelper, 'initImageDiff').and.callFake( spyOn(initImageDiffHelper, 'initImageDiff').and.callFake(
(diffFileEl, canCreateNote, renderCommentBadge) => { (diffFileEl, canCreateNote, renderCommentBadge) => {
expect(renderCommentBadge).toEqual(true); expect(renderCommentBadge).toEqual(true);
done(); done();
...@@ -32,9 +32,9 @@ describe('initDiscussionTab', () => { ...@@ -32,9 +32,9 @@ describe('initDiscussionTab', () => {
}); });
it('should call initImageDiff for each diffFileEls', () => { it('should call initImageDiff for each diffFileEls', () => {
spyOn(imageDiffHelper, 'initImageDiff').and.callFake(() => {}); spyOn(initImageDiffHelper, 'initImageDiff').and.callFake(() => {});
initDiscussionTab(); initDiscussionTab();
expect(imageDiffHelper.initImageDiff.calls.count()).toEqual(2); expect(initImageDiffHelper.initImageDiff.calls.count()).toEqual(2);
}); });
}); });
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