debian-mirror-gitlab/spec/frontend/vue_shared/components/gl_countdown_spec.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import Vue, { nextTick } from 'vue';
2021-03-11 19:13:27 +05:30
import mountComponent from 'helpers/vue_mount_component_helper';
2018-12-13 13:39:08 +05:30
import GlCountdown from '~/vue_shared/components/gl_countdown.vue';
describe('GlCountdown', () => {
const Component = Vue.extend(GlCountdown);
let vm;
let now = '2000-01-01T00:00:00Z';
beforeEach(() => {
2020-05-24 23:13:21 +05:30
jest.spyOn(Date, 'now').mockImplementation(() => new Date(now).getTime());
2018-12-13 13:39:08 +05:30
});
afterEach(() => {
vm.$destroy();
2020-05-24 23:13:21 +05:30
jest.clearAllTimers();
2018-12-13 13:39:08 +05:30
});
describe('when there is time remaining', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2018-12-13 13:39:08 +05:30
vm = mountComponent(Component, {
endDateString: '2000-01-01T01:02:03Z',
});
2022-04-04 11:22:00 +05:30
await nextTick();
2018-12-13 13:39:08 +05:30
});
it('displays remaining time', () => {
2020-05-24 23:13:21 +05:30
expect(vm.$el.textContent).toContain('01:02:03');
2018-12-13 13:39:08 +05:30
});
2022-04-04 11:22:00 +05:30
it('updates remaining time', async () => {
2018-12-13 13:39:08 +05:30
now = '2000-01-01T00:00:01Z';
2020-05-24 23:13:21 +05:30
jest.advanceTimersByTime(1000);
2018-12-13 13:39:08 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(vm.$el.textContent).toContain('01:02:02');
2018-12-13 13:39:08 +05:30
});
});
describe('when there is no time remaining', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2018-12-13 13:39:08 +05:30
vm = mountComponent(Component, {
endDateString: '1900-01-01T00:00:00Z',
});
2022-04-04 11:22:00 +05:30
await nextTick();
2018-12-13 13:39:08 +05:30
});
it('displays 00:00:00', () => {
2020-05-24 23:13:21 +05:30
expect(vm.$el.textContent).toContain('00:00:00');
2018-12-13 13:39:08 +05:30
});
});
describe('when an invalid date is passed', () => {
2020-05-24 23:13:21 +05:30
beforeEach(() => {
Vue.config.warnHandler = jest.fn();
});
afterEach(() => {
Vue.config.warnHandler = null;
});
2018-12-13 13:39:08 +05:30
it('throws a validation error', () => {
vm = mountComponent(Component, {
endDateString: 'this is invalid',
});
expect(Vue.config.warnHandler).toHaveBeenCalledTimes(1);
2020-05-24 23:13:21 +05:30
const [errorMessage] = Vue.config.warnHandler.mock.calls[0];
2018-12-13 13:39:08 +05:30
expect(errorMessage).toMatch(/^Invalid prop: .* "endDateString"/);
});
});
});