Commit 71ac09ac authored by Thomas Randolph's avatar Thomas Randolph

Move test file to Jest to make CI pass

This also switches the `initMouseEvent` stuff to not
use the deprecated API. In the process, I stopped
explicitly defining values that default to the same thing.
parent 55bd77be
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';
...@@ -14,50 +16,45 @@ describe('ImageDiffViewer', () => { ...@@ -14,50 +16,45 @@ describe('ImageDiffViewer', () => {
oldSize: 2048, oldSize: 2048,
newSize: 1024, 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,
0,
clientX, clientX,
0, });
false,
false, // JSDOM does not implement experimental APIs
false, event.pageX = clientX;
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({ ...allProps }); createComponent({ ...allProps });
setTimeout(() => { vm.$nextTick(() => {
const metaInfoElements = vm.$el.querySelectorAll('.image-info'); const metaInfoElements = vm.$el.querySelectorAll('.image-info');
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);
...@@ -84,7 +81,7 @@ describe('ImageDiffViewer', () => { ...@@ -84,7 +81,7 @@ describe('ImageDiffViewer', () => {
it('renders image diff for new', done => { it('renders image diff for new', done => {
createComponent({ ...allProps, diffMode: 'new', oldPath: '' }); createComponent({ ...allProps, diffMode: 'new', oldPath: '' });
setTimeout(() => { setImmediate(() => {
const metaInfoElement = vm.$el.querySelector('.image-info'); const metaInfoElement = vm.$el.querySelector('.image-info');
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);
...@@ -97,7 +94,7 @@ describe('ImageDiffViewer', () => { ...@@ -97,7 +94,7 @@ describe('ImageDiffViewer', () => {
it('renders image diff for deleted', done => { it('renders image diff for deleted', done => {
createComponent({ ...allProps, diffMode: 'deleted', newPath: '' }); createComponent({ ...allProps, diffMode: 'deleted', newPath: '' });
setTimeout(() => { setImmediate(() => {
const metaInfoElement = vm.$el.querySelector('.image-info'); const metaInfoElement = vm.$el.querySelector('.image-info');
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);
...@@ -116,7 +113,7 @@ describe('ImageDiffViewer', () => { ...@@ -116,7 +113,7 @@ describe('ImageDiffViewer', () => {
...allProps, ...allProps,
diffMode: 'renamed', diffMode: 'renamed',
}, },
template: ` ...compileToFunctions(`
<image-diff-viewer <image-diff-viewer
:diff-mode="diffMode" :diff-mode="diffMode"
:new-path="newPath" :new-path="newPath"
...@@ -126,10 +123,10 @@ describe('ImageDiffViewer', () => { ...@@ -126,10 +123,10 @@ describe('ImageDiffViewer', () => {
> >
<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'); 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);
...@@ -145,7 +142,7 @@ describe('ImageDiffViewer', () => { ...@@ -145,7 +142,7 @@ describe('ImageDiffViewer', () => {
beforeEach(done => { beforeEach(done => {
createComponent({ ...requiredProps }); createComponent({ ...requiredProps });
setTimeout(() => { setImmediate(() => {
done(); done();
}); });
}); });
...@@ -164,7 +161,7 @@ describe('ImageDiffViewer', () => { ...@@ -164,7 +161,7 @@ describe('ImageDiffViewer', () => {
beforeEach(done => { beforeEach(done => {
createComponent({ ...requiredProps }); createComponent({ ...requiredProps });
setTimeout(() => { setImmediate(() => {
done(); done();
}); });
}); });
...@@ -184,7 +181,7 @@ describe('ImageDiffViewer', () => { ...@@ -184,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