Commit 135727a7 authored by Himanshu Kapoor's avatar Himanshu Kapoor

Migrate javascripts/jobs specs to Jest

Migrate spec/javascripts/jobs specs from Karma to Jest
parent 91dc8ac6
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 JobContainerItem from '~/jobs/components/job_container_item.vue'; import JobContainerItem from '~/jobs/components/job_container_item.vue';
import job from '../mock_data'; import job from '../mock_data';
...@@ -18,7 +18,7 @@ describe('JobContainerItem', () => { ...@@ -18,7 +18,7 @@ describe('JobContainerItem', () => {
}); });
it('displays the job name', () => { it('displays the job name', () => {
expect(vm.$el).toContainText(job.name); expect(vm.$el.innerText).toContain(job.name);
}); });
it('displays a link to the job', () => { it('displays a link to the job', () => {
...@@ -75,9 +75,11 @@ describe('JobContainerItem', () => { ...@@ -75,9 +75,11 @@ describe('JobContainerItem', () => {
describe('for delayed job', () => { describe('for delayed job', () => {
beforeEach(() => { beforeEach(() => {
const remainingMilliseconds = 1337000; const remainingMilliseconds = 1337000;
spyOn(Date, 'now').and.callFake( jest
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingMilliseconds, .spyOn(Date, 'now')
); .mockImplementation(
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingMilliseconds,
);
}); });
it('displays remaining time in tooltip', done => { it('displays remaining time in tooltip', done => {
......
import Vue from 'vue'; import Vue from 'vue';
import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
import component from '~/jobs/components/job_log.vue'; import component from '~/jobs/components/job_log.vue';
import createStore from '~/jobs/store'; import createStore from '~/jobs/store';
import { resetStore } from '../store/helpers'; import { resetStore } from '../store/helpers';
......
import Vue from 'vue'; import Vue from 'vue';
import { trimText } from 'spec/helpers/text_helper'; import { trimText } from 'helpers/text_helper';
import component from '~/jobs/components/stages_dropdown.vue'; import component from '~/jobs/components/stages_dropdown.vue';
import mountComponent from '../../helpers/vue_mount_component_helper'; import mountComponent from '../../helpers/vue_mount_component_helper';
......
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 delayedJobMixin from '~/jobs/mixins/delayed_job_mixin'; import delayedJobMixin from '~/jobs/mixins/delayed_job_mixin';
describe('DelayedJobMixin', () => { describe('DelayedJobMixin', () => {
...@@ -12,18 +12,16 @@ describe('DelayedJobMixin', () => { ...@@ -12,18 +12,16 @@ describe('DelayedJobMixin', () => {
required: true, required: true,
}, },
}, },
template: '<div>{{ remainingTime }}</div>', render(createElement) {
return createElement('div', this.remainingTime);
},
}); });
let vm; let vm;
beforeEach(() => {
jasmine.clock().install();
});
afterEach(() => { afterEach(() => {
vm.$destroy(); vm.$destroy();
jasmine.clock().uninstall(); jest.clearAllTimers();
}); });
describe('if job is empty object', () => { describe('if job is empty object', () => {
...@@ -38,13 +36,9 @@ describe('DelayedJobMixin', () => { ...@@ -38,13 +36,9 @@ describe('DelayedJobMixin', () => {
}); });
describe('after mounting', () => { describe('after mounting', () => {
beforeEach(done => { beforeEach(() => vm.$nextTick());
Vue.nextTick()
.then(done)
.catch(done.fail);
});
it('doe not update remaining time', () => { it('does not update remaining time', () => {
expect(vm.$el.innerText).toBe('00:00:00'); expect(vm.$el.innerText).toBe('00:00:00');
}); });
}); });
...@@ -54,39 +48,31 @@ describe('DelayedJobMixin', () => { ...@@ -54,39 +48,31 @@ describe('DelayedJobMixin', () => {
let remainingTimeInMilliseconds = 42000; let remainingTimeInMilliseconds = 42000;
beforeEach(() => { beforeEach(() => {
spyOn(Date, 'now').and.callFake( jest
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingTimeInMilliseconds, .spyOn(Date, 'now')
); .mockImplementation(
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingTimeInMilliseconds,
);
vm = mountComponent(dummyComponent, { vm = mountComponent(dummyComponent, {
job: delayedJobFixture, job: delayedJobFixture,
}); });
}); });
it('sets remaining time to 00:00:00', () => {
expect(vm.$el.innerText).toBe('00:00:00');
});
describe('after mounting', () => { describe('after mounting', () => {
beforeEach(done => { beforeEach(() => vm.$nextTick());
Vue.nextTick()
.then(done)
.catch(done.fail);
});
it('sets remaining time', () => { it('sets remaining time', () => {
expect(vm.$el.innerText).toBe('00:00:42'); expect(vm.$el.innerText).toBe('00:00:42');
}); });
it('updates remaining time', done => { it('updates remaining time', () => {
remainingTimeInMilliseconds = 41000; remainingTimeInMilliseconds = 41000;
jasmine.clock().tick(1000); jest.advanceTimersByTime(1000);
Vue.nextTick() return vm.$nextTick().then(() => {
.then(() => { expect(vm.$el.innerText).toBe('00:00:41');
expect(vm.$el.innerText).toBe('00:00:41'); });
})
.then(done)
.catch(done.fail);
}); });
}); });
}); });
......
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import testAction from 'spec/helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import { TEST_HOST } from 'spec/test_constants'; import { TEST_HOST } from '../../helpers/test_constants';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { import {
setJobEndpoint, setJobEndpoint,
...@@ -315,26 +315,26 @@ describe('Job State actions', () => { ...@@ -315,26 +315,26 @@ describe('Job State actions', () => {
let commit; let commit;
beforeEach(() => { beforeEach(() => {
jasmine.clock().install(); dispatch = jest.fn();
commit = jest.fn();
dispatch = jasmine.createSpy();
commit = jasmine.createSpy();
startPollingTrace({ dispatch, commit }); startPollingTrace({ dispatch, commit });
}); });
afterEach(() => { afterEach(() => {
jasmine.clock().uninstall(); jest.clearAllTimers();
}); });
it('should save the timeout id but not call fetchTrace', () => { it('should save the timeout id but not call fetchTrace', () => {
expect(commit).toHaveBeenCalledWith(types.SET_TRACE_TIMEOUT, 1); expect(commit).toHaveBeenCalledWith(types.SET_TRACE_TIMEOUT, expect.any(Number));
expect(commit.mock.calls[0][1]).toBeGreaterThan(0);
expect(dispatch).not.toHaveBeenCalledWith('fetchTrace'); expect(dispatch).not.toHaveBeenCalledWith('fetchTrace');
}); });
describe('after timeout has passed', () => { describe('after timeout has passed', () => {
beforeEach(() => { beforeEach(() => {
jasmine.clock().tick(4000); jest.advanceTimersByTime(4000);
}); });
it('should clear the timeout id and fetchTrace', () => { it('should clear the timeout id and fetchTrace', () => {
...@@ -351,7 +351,7 @@ describe('Job State actions', () => { ...@@ -351,7 +351,7 @@ describe('Job State actions', () => {
// Can't use spyOn(window, 'clearTimeout') because this caused unrelated specs to timeout // Can't use spyOn(window, 'clearTimeout') because this caused unrelated specs to timeout
// https://gitlab.com/gitlab-org/gitlab/-/merge_requests/23838#note_280277727 // https://gitlab.com/gitlab-org/gitlab/-/merge_requests/23838#note_280277727
origTimeout = window.clearTimeout; origTimeout = window.clearTimeout;
window.clearTimeout = jasmine.createSpy(); window.clearTimeout = jest.fn();
}); });
afterEach(() => { afterEach(() => {
......
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