2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-03-08 18:12:59 +05:30
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2021-03-11 19:13:27 +05:30
|
|
|
import imageDiffHelper from '~/image_diff/helpers/index';
|
2018-03-17 18:26:18 +05:30
|
|
|
import ImageDiff from '~/image_diff/image_diff';
|
2021-03-11 19:13:27 +05:30
|
|
|
import ReplacedImageDiff from '~/image_diff/replaced_image_diff';
|
2018-03-17 18:26:18 +05:30
|
|
|
import { viewTypes } from '~/image_diff/view_types';
|
|
|
|
|
|
|
|
describe('ReplacedImageDiff', () => {
|
|
|
|
let element;
|
|
|
|
let replacedImageDiff;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(`
|
2018-03-17 18:26:18 +05:30
|
|
|
<div id="element">
|
|
|
|
<div class="two-up">
|
|
|
|
<div class="js-image-frame">
|
2020-07-28 23:09:34 +05:30
|
|
|
<img src="${TEST_HOST}/image.png">
|
2018-03-17 18:26:18 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="swipe">
|
|
|
|
<div class="js-image-frame">
|
2020-07-28 23:09:34 +05:30
|
|
|
<img src="${TEST_HOST}/image.png">
|
2018-03-17 18:26:18 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="onion-skin">
|
|
|
|
<div class="js-image-frame">
|
2020-07-28 23:09:34 +05:30
|
|
|
<img src="${TEST_HOST}/image.png">
|
2018-03-17 18:26:18 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="view-modes-menu">
|
|
|
|
<div class="two-up">2-up</div>
|
|
|
|
<div class="swipe">Swipe</div>
|
|
|
|
<div class="onion-skin">Onion skin</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
element = document.getElementById('element');
|
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
function setupImageFrameEls() {
|
|
|
|
replacedImageDiff.imageFrameEls = [];
|
2018-12-13 13:39:08 +05:30
|
|
|
replacedImageDiff.imageFrameEls[viewTypes.TWO_UP] = element.querySelector(
|
|
|
|
'.two-up .js-image-frame',
|
|
|
|
);
|
|
|
|
replacedImageDiff.imageFrameEls[viewTypes.SWIPE] = element.querySelector(
|
|
|
|
'.swipe .js-image-frame',
|
|
|
|
);
|
|
|
|
replacedImageDiff.imageFrameEls[viewTypes.ONION_SKIN] = element.querySelector(
|
|
|
|
'.onion-skin .js-image-frame',
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function setupViewModesEls() {
|
|
|
|
replacedImageDiff.viewModesEls = [];
|
2018-12-13 13:39:08 +05:30
|
|
|
replacedImageDiff.viewModesEls[viewTypes.TWO_UP] = element.querySelector(
|
|
|
|
'.view-modes-menu .two-up',
|
|
|
|
);
|
|
|
|
replacedImageDiff.viewModesEls[viewTypes.SWIPE] = element.querySelector(
|
|
|
|
'.view-modes-menu .swipe',
|
|
|
|
);
|
|
|
|
replacedImageDiff.viewModesEls[viewTypes.ONION_SKIN] = element.querySelector(
|
|
|
|
'.view-modes-menu .onion-skin',
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function setupImageEls() {
|
|
|
|
replacedImageDiff.imageEls = [];
|
|
|
|
replacedImageDiff.imageEls[viewTypes.TWO_UP] = element.querySelector('.two-up img');
|
|
|
|
replacedImageDiff.imageEls[viewTypes.SWIPE] = element.querySelector('.swipe img');
|
|
|
|
replacedImageDiff.imageEls[viewTypes.ONION_SKIN] = element.querySelector('.onion-skin img');
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should extend ImageDiff', () => {
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(replacedImageDiff instanceof ImageDiff).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('init', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(ReplacedImageDiff.prototype, 'bindEvents').mockImplementation(() => {});
|
|
|
|
jest.spyOn(ReplacedImageDiff.prototype, 'generateImageEls').mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
|
|
|
replacedImageDiff.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set imageFrameEls', () => {
|
|
|
|
const { imageFrameEls } = replacedImageDiff;
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(imageFrameEls).toBeDefined();
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(imageFrameEls[viewTypes.TWO_UP]).toEqual(
|
|
|
|
element.querySelector('.two-up .js-image-frame'),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(imageFrameEls[viewTypes.SWIPE]).toEqual(
|
|
|
|
element.querySelector('.swipe .js-image-frame'),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(imageFrameEls[viewTypes.ONION_SKIN]).toEqual(
|
|
|
|
element.querySelector('.onion-skin .js-image-frame'),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should set viewModesEls', () => {
|
|
|
|
const { viewModesEls } = replacedImageDiff;
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(viewModesEls).toBeDefined();
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(viewModesEls[viewTypes.TWO_UP]).toEqual(
|
|
|
|
element.querySelector('.view-modes-menu .two-up'),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(viewModesEls[viewTypes.SWIPE]).toEqual(
|
|
|
|
element.querySelector('.view-modes-menu .swipe'),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(viewModesEls[viewTypes.ONION_SKIN]).toEqual(
|
|
|
|
element.querySelector('.view-modes-menu .onion-skin'),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should generateImageEls', () => {
|
|
|
|
expect(ReplacedImageDiff.prototype.generateImageEls).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should bindEvents', () => {
|
|
|
|
expect(ReplacedImageDiff.prototype.bindEvents).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('currentView', () => {
|
|
|
|
it('should set currentView', () => {
|
|
|
|
replacedImageDiff.init(viewTypes.ONION_SKIN);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(replacedImageDiff.currentView).toEqual(viewTypes.ONION_SKIN);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should default to viewTypes.TWO_UP', () => {
|
|
|
|
expect(replacedImageDiff.currentView).toEqual(viewTypes.TWO_UP);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('generateImageEls', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(ReplacedImageDiff.prototype, 'bindEvents').mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element, {
|
|
|
|
canCreateNote: false,
|
|
|
|
renderCommentBadge: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
setupImageFrameEls();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set imageEls', () => {
|
|
|
|
replacedImageDiff.generateImageEls();
|
|
|
|
const { imageEls } = replacedImageDiff;
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(imageEls).toBeDefined();
|
|
|
|
expect(imageEls[viewTypes.TWO_UP]).toEqual(element.querySelector('.two-up img'));
|
|
|
|
expect(imageEls[viewTypes.SWIPE]).toEqual(element.querySelector('.swipe img'));
|
|
|
|
expect(imageEls[viewTypes.ONION_SKIN]).toEqual(element.querySelector('.onion-skin img'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('bindEvents', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(ImageDiff.prototype, 'bindEvents').mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
|
|
|
|
|
|
|
setupViewModesEls();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call super.bindEvents', () => {
|
|
|
|
replacedImageDiff.bindEvents();
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(ImageDiff.prototype.bindEvents).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should register click eventlistener to 2-up view mode', () => {
|
|
|
|
const changeViewSpy = jest
|
|
|
|
.spyOn(ReplacedImageDiff.prototype, 'changeView')
|
|
|
|
.mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff.bindEvents();
|
|
|
|
replacedImageDiff.viewModesEls[viewTypes.TWO_UP].click();
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
expect(changeViewSpy).toHaveBeenCalledWith(viewTypes.TWO_UP, expect.any(Object));
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should register click eventlistener to swipe view mode', () => {
|
|
|
|
const changeViewSpy = jest
|
|
|
|
.spyOn(ReplacedImageDiff.prototype, 'changeView')
|
|
|
|
.mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff.bindEvents();
|
|
|
|
replacedImageDiff.viewModesEls[viewTypes.SWIPE].click();
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
expect(changeViewSpy).toHaveBeenCalledWith(viewTypes.SWIPE, expect.any(Object));
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should register click eventlistener to onion skin view mode', () => {
|
|
|
|
const changeViewSpy = jest
|
|
|
|
.spyOn(ReplacedImageDiff.prototype, 'changeView')
|
|
|
|
.mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff.bindEvents();
|
|
|
|
replacedImageDiff.viewModesEls[viewTypes.SWIPE].click();
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(changeViewSpy).toHaveBeenCalledWith(viewTypes.SWIPE, expect.any(Object));
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
|
|
|
describe('imageEl', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
|
|
|
replacedImageDiff.currentView = viewTypes.TWO_UP;
|
|
|
|
setupImageEls();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return imageEl based on currentView', () => {
|
|
|
|
expect(replacedImageDiff.imageEl).toEqual(element.querySelector('.two-up img'));
|
|
|
|
|
|
|
|
replacedImageDiff.currentView = viewTypes.SWIPE;
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(replacedImageDiff.imageEl).toEqual(element.querySelector('.swipe img'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('imageFrameEl', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
|
|
|
replacedImageDiff.currentView = viewTypes.TWO_UP;
|
|
|
|
setupImageFrameEls();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return imageFrameEl based on currentView', () => {
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(replacedImageDiff.imageFrameEl).toEqual(
|
|
|
|
element.querySelector('.two-up .js-image-frame'),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff.currentView = viewTypes.ONION_SKIN;
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
expect(replacedImageDiff.imageFrameEl).toEqual(
|
|
|
|
element.querySelector('.onion-skin .js-image-frame'),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('changeView', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(imageDiffHelper, 'removeCommentIndicator').mockReturnValue({
|
2018-03-17 18:26:18 +05:30
|
|
|
removed: false,
|
|
|
|
});
|
|
|
|
setupImageFrameEls();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('invalid viewType', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
replacedImageDiff.changeView('some-view-name');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call removeCommentIndicator', () => {
|
|
|
|
expect(imageDiffHelper.removeCommentIndicator).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('valid viewType', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(ReplacedImageDiff.prototype, 'renderNewView').mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
replacedImageDiff.changeView(viewTypes.ONION_SKIN);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.clearAllTimers();
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should call removeCommentIndicator', () => {
|
|
|
|
expect(imageDiffHelper.removeCommentIndicator).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update currentView to newView', () => {
|
|
|
|
expect(replacedImageDiff.currentView).toEqual(viewTypes.ONION_SKIN);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should clear imageBadges', () => {
|
|
|
|
expect(replacedImageDiff.imageBadges.length).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call renderNewView', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.advanceTimersByTime(251);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(replacedImageDiff.renderNewView).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('renderNewView', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
replacedImageDiff = new ReplacedImageDiff(element);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call renderBadges', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(ReplacedImageDiff.prototype, 'renderBadges').mockImplementation(() => {});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
replacedImageDiff.renderNewView({
|
|
|
|
removed: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(replacedImageDiff.renderBadges).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeIndicator', () => {
|
|
|
|
const indicator = {
|
|
|
|
removed: true,
|
|
|
|
x: 0,
|
|
|
|
y: 1,
|
|
|
|
image: {
|
|
|
|
width: 50,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setupImageEls();
|
|
|
|
setupImageFrameEls();
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should pass showCommentIndicator normalized indicator values', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(imageDiffHelper, 'showCommentIndicator').mockImplementation(() => {});
|
2022-06-21 17:19:12 +05:30
|
|
|
const resizeCoordinatesToImageElementSpy = jest
|
2020-05-24 23:13:21 +05:30
|
|
|
.spyOn(imageDiffHelper, 'resizeCoordinatesToImageElement')
|
2022-06-21 17:19:12 +05:30
|
|
|
.mockImplementation(() => {});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
replacedImageDiff.renderNewView(indicator);
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
expect(resizeCoordinatesToImageElementSpy).toHaveBeenCalledWith(undefined, {
|
|
|
|
x: indicator.x,
|
|
|
|
y: indicator.y,
|
|
|
|
width: indicator.image.width,
|
|
|
|
height: indicator.image.height,
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should call showCommentIndicator', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
const normalized = {
|
|
|
|
normalized: true,
|
|
|
|
};
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(imageDiffHelper, 'resizeCoordinatesToImageElement').mockReturnValue(normalized);
|
2022-06-21 17:19:12 +05:30
|
|
|
const showCommentIndicatorSpy = jest
|
2020-05-24 23:13:21 +05:30
|
|
|
.spyOn(imageDiffHelper, 'showCommentIndicator')
|
2022-06-21 17:19:12 +05:30
|
|
|
.mockImplementation(() => {});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
replacedImageDiff.renderNewView(indicator);
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
expect(showCommentIndicatorSpy).toHaveBeenCalledWith(undefined, normalized);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|