Commit 6ebc082e authored by Jose Vargas's avatar Jose Vargas

Refactor delayed_job_mixin_spec to use shallowMount

parent 0dbe4c2d
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'helpers/vue_mount_component_helper';
import delayedJobMixin from '~/jobs/mixins/delayed_job_mixin'; import delayedJobMixin from '~/jobs/mixins/delayed_job_mixin';
describe('DelayedJobMixin', () => { describe('DelayedJobMixin', () => {
let wrapper;
const delayedJobFixture = getJSONFixture('jobs/delayed.json'); const delayedJobFixture = getJSONFixture('jobs/delayed.json');
const dummyComponent = Vue.extend({ const dummyComponent = {
mixins: [delayedJobMixin],
props: { props: {
job: { job: {
type: Object, type: Object,
required: true, required: true,
}, },
}, },
render(createElement) { mixins: [delayedJobMixin],
return createElement('div', this.remainingTime); template: '<div>{{remainingTime}}</div>',
}, };
});
let vm;
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
jest.clearAllTimers(); wrapper = null;
}); });
describe('if job is empty object', () => { describe('if job is empty object', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(dummyComponent, { wrapper = shallowMount(dummyComponent, {
job: {}, propsData: {
job: {},
},
}); });
}); });
it('sets remaining time to 00:00:00', () => { it('sets remaining time to 00:00:00', () => {
expect(vm.$el.innerText).toBe('00:00:00'); expect(wrapper.text()).toBe('00:00:00');
}); });
describe('after mounting', () => { it('does not update remaining time after mounting', async () => {
beforeEach(() => vm.$nextTick()); await wrapper.vm.$nextTick();
it('does not update remaining time', () => { expect(wrapper.text()).toBe('00:00:00');
expect(vm.$el.innerText).toBe('00:00:00');
});
}); });
}); });
...@@ -48,33 +44,32 @@ describe('DelayedJobMixin', () => { ...@@ -48,33 +44,32 @@ describe('DelayedJobMixin', () => {
describe('if job is delayed job', () => { describe('if job is delayed job', () => {
let remainingTimeInMilliseconds = 42000; let remainingTimeInMilliseconds = 42000;
beforeEach(() => { beforeEach(async () => {
jest jest
.spyOn(Date, 'now') .spyOn(Date, 'now')
.mockImplementation( .mockImplementation(
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingTimeInMilliseconds, () => new Date(delayedJobFixture.scheduled_at).getTime() - remainingTimeInMilliseconds,
); );
vm = mountComponent(dummyComponent, { wrapper = shallowMount(dummyComponent, {
job: delayedJobFixture, propsData: {
job: delayedJobFixture,
},
}); });
});
describe('after mounting', () => { await wrapper.vm.$nextTick();
beforeEach(() => vm.$nextTick()); });
it('sets remaining time', () => { it('sets remaining time', () => {
expect(vm.$el.innerText).toBe('00:00:42'); expect(wrapper.text()).toBe('00:00:42');
}); });
it('updates remaining time', () => { it('updates remaining time', async () => {
remainingTimeInMilliseconds = 41000; remainingTimeInMilliseconds = 41000;
jest.advanceTimersByTime(1000); jest.advanceTimersByTime(1000);
return vm.$nextTick().then(() => { await wrapper.vm.$nextTick();
expect(vm.$el.innerText).toBe('00:00:41'); expect(wrapper.text()).toBe('00:00:41');
});
});
}); });
}); });
}); });
...@@ -96,33 +91,32 @@ describe('DelayedJobMixin', () => { ...@@ -96,33 +91,32 @@ describe('DelayedJobMixin', () => {
describe('if job is delayed job', () => { describe('if job is delayed job', () => {
let remainingTimeInMilliseconds = 42000; let remainingTimeInMilliseconds = 42000;
beforeEach(() => { beforeEach(async () => {
jest jest
.spyOn(Date, 'now') .spyOn(Date, 'now')
.mockImplementation( .mockImplementation(
() => mockGraphQlJob.scheduledAt.getTime() - remainingTimeInMilliseconds, () => mockGraphQlJob.scheduledAt.getTime() - remainingTimeInMilliseconds,
); );
vm = mountComponent(dummyComponent, { wrapper = shallowMount(dummyComponent, {
job: mockGraphQlJob, propsData: {
job: mockGraphQlJob,
},
}); });
});
describe('after mounting', () => { await wrapper.vm.$nextTick();
beforeEach(() => vm.$nextTick()); });
it('sets remaining time', () => { it('sets remaining time', () => {
expect(vm.$el.innerText).toBe('00:00:42'); expect(wrapper.text()).toBe('00:00:42');
}); });
it('updates remaining time', () => { it('updates remaining time', async () => {
remainingTimeInMilliseconds = 41000; remainingTimeInMilliseconds = 41000;
jest.advanceTimersByTime(1000); jest.advanceTimersByTime(1000);
return vm.$nextTick().then(() => { await wrapper.vm.$nextTick();
expect(vm.$el.innerText).toBe('00:00:41'); expect(wrapper.text()).toBe('00:00:41');
});
});
}); });
}); });
}); });
......
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