Commit 6f565753 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'himkp-jest-vue-shared-3' into 'master'

Migrate 3 specs in vue_shared to Jest

See merge request gitlab-org/gitlab!34001
parents 1d7e69e3 e61f2740
import $ from 'jquery';
import Vue from 'vue'; import Vue from 'vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'helpers/vue_mount_component_helper';
import DeprecatedModal2 from '~/vue_shared/components/deprecated_modal_2.vue'; import DeprecatedModal2 from '~/vue_shared/components/deprecated_modal_2.vue';
const modalComponent = Vue.extend(DeprecatedModal2); const modalComponent = Vue.extend(DeprecatedModal2);
...@@ -86,7 +85,7 @@ describe('DeprecatedModal2', () => { ...@@ -86,7 +85,7 @@ describe('DeprecatedModal2', () => {
}); });
}); });
it('works with data-toggle="modal"', done => { it('works with data-toggle="modal"', () => {
setFixtures(` setFixtures(`
<button id="modal-button" data-toggle="modal" data-target="#my-modal"></button> <button id="modal-button" data-toggle="modal" data-target="#my-modal"></button>
<div id="modal-container"></div> <div id="modal-container"></div>
...@@ -101,9 +100,16 @@ describe('DeprecatedModal2', () => { ...@@ -101,9 +100,16 @@ describe('DeprecatedModal2', () => {
}, },
modalContainer, modalContainer,
); );
$(vm.$el).on('shown.bs.modal', () => done()); const modalElement = document.getElementById('my-modal');
modalButton.click(); modalButton.click();
expect(modalElement).not.toHaveClass('show');
// let the modal fade in
jest.runOnlyPendingTimers();
expect(modalElement).toHaveClass('show');
}); });
describe('methods', () => { describe('methods', () => {
...@@ -111,7 +117,7 @@ describe('DeprecatedModal2', () => { ...@@ -111,7 +117,7 @@ describe('DeprecatedModal2', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(modalComponent, {}); vm = mountComponent(modalComponent, {});
spyOn(vm, '$emit'); jest.spyOn(vm, '$emit').mockImplementation(() => {});
}); });
describe('emitCancel', () => { describe('emitCancel', () => {
...@@ -149,23 +155,14 @@ describe('DeprecatedModal2', () => { ...@@ -149,23 +155,14 @@ describe('DeprecatedModal2', () => {
describe('slots', () => { describe('slots', () => {
const slotContent = 'this should go into the slot'; const slotContent = 'this should go into the slot';
const modalWithSlot = slotName => {
let template;
if (slotName) {
template = `
<deprecated-modal-2>
<template slot="${slotName}">${slotContent}</template>
</deprecated-modal-2>
`;
} else {
template = `<deprecated-modal-2>${slotContent}</deprecated-modal-2>`;
}
const modalWithSlot = slot => {
return Vue.extend({ return Vue.extend({
components: { components: {
DeprecatedModal2, DeprecatedModal2,
}, },
template, render: h =>
h('deprecated-modal-2', [slot ? h('template', { slot }, slotContent) : slotContent]),
}); });
}; };
......
import $ from 'jquery';
import Vue from 'vue'; import Vue from 'vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'helpers/vue_mount_component_helper';
import DeprecatedModal from '~/vue_shared/components/deprecated_modal.vue'; import DeprecatedModal from '~/vue_shared/components/deprecated_modal.vue';
const modalComponent = Vue.extend(DeprecatedModal); const modalComponent = Vue.extend(DeprecatedModal);
...@@ -47,7 +46,7 @@ describe('DeprecatedModal', () => { ...@@ -47,7 +46,7 @@ describe('DeprecatedModal', () => {
}); });
}); });
it('works with data-toggle="modal"', done => { it('works with data-toggle="modal"', () => {
setFixtures(` setFixtures(`
<button id="modal-button" data-toggle="modal" data-target="#my-modal"></button> <button id="modal-button" data-toggle="modal" data-target="#my-modal"></button>
<div id="modal-container"></div> <div id="modal-container"></div>
...@@ -63,9 +62,12 @@ describe('DeprecatedModal', () => { ...@@ -63,9 +62,12 @@ describe('DeprecatedModal', () => {
modalContainer, modalContainer,
); );
const modalElement = vm.$el.querySelector('#my-modal'); const modalElement = vm.$el.querySelector('#my-modal');
$(modalElement).on('shown.bs.modal', () => done());
expect(modalElement).not.toHaveClass('show');
modalButton.click(); modalButton.click();
expect(modalElement).toHaveClass('show');
}); });
}); });
}); });
import $ from 'jquery'; import $ from 'jquery';
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import tooltip from '~/vue_shared/directives/tooltip'; import tooltip from '~/vue_shared/directives/tooltip';
describe('Tooltip directive', () => { describe('Tooltip directive', () => {
...@@ -13,19 +13,22 @@ describe('Tooltip directive', () => { ...@@ -13,19 +13,22 @@ describe('Tooltip directive', () => {
describe('with a single tooltip', () => { describe('with a single tooltip', () => {
beforeEach(() => { beforeEach(() => {
setFixtures('<div id="dummy-element"></div>'); const wrapper = mount(
vm = new Vue({ {
el: '#dummy-element', directives: {
directives: { tooltip,
tooltip, },
data() {
return {
tooltip: 'some text',
};
},
template: '<div v-tooltip :title="tooltip"></div>',
}, },
data() { { attachToDocument: true },
return { );
tooltip: 'some text',
}; vm = wrapper.vm;
},
template: '<div v-tooltip :title="tooltip"></div>',
});
}); });
it('should have tooltip plugin applied', () => { it('should have tooltip plugin applied', () => {
...@@ -34,48 +37,54 @@ describe('Tooltip directive', () => { ...@@ -34,48 +37,54 @@ describe('Tooltip directive', () => {
it('displays the title as tooltip', () => { it('displays the title as tooltip', () => {
$(vm.$el).tooltip('show'); $(vm.$el).tooltip('show');
jest.runOnlyPendingTimers();
const tooltipElement = document.querySelector('.tooltip-inner'); const tooltipElement = document.querySelector('.tooltip-inner');
expect(tooltipElement.innerText).toContain('some text'); expect(tooltipElement.textContent).toContain('some text');
}); });
it('updates a visible tooltip', done => { it('updates a visible tooltip', () => {
$(vm.$el).tooltip('show'); $(vm.$el).tooltip('show');
jest.runOnlyPendingTimers();
const tooltipElement = document.querySelector('.tooltip-inner'); const tooltipElement = document.querySelector('.tooltip-inner');
vm.tooltip = 'other text'; vm.tooltip = 'other text';
Vue.nextTick() jest.runOnlyPendingTimers();
.then(() => {
expect(tooltipElement).toContainText('other text'); return vm.$nextTick().then(() => {
done(); expect(tooltipElement.textContent).toContain('other text');
}) });
.catch(done.fail);
}); });
}); });
describe('with multiple tooltips', () => { describe('with multiple tooltips', () => {
beforeEach(() => { beforeEach(() => {
const SomeComponent = Vue.extend({ const wrapper = mount(
directives: { {
tooltip, directives: {
}, tooltip,
template: ` },
<div> template: `
<div <div>
v-tooltip <div
class="js-look-for-tooltip" v-tooltip
title="foo"> class="js-look-for-tooltip"
</div> title="foo">
<div </div>
v-tooltip <div
title="bar"> v-tooltip
title="bar">
</div>
</div> </div>
</div> `,
`, },
}); { attachToDocument: true },
);
vm = new SomeComponent().$mount(); vm = wrapper.vm;
}); });
it('should have tooltip plugin applied to all instances', () => { it('should have tooltip plugin applied to all instances', () => {
......
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