2022-04-04 11:22:00 +05:30
|
|
|
import Vue, { nextTick } from 'vue';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
2018-12-13 13:39:08 +05:30
|
|
|
import GlCountdown from '~/vue_shared/components/gl_countdown.vue';
|
|
|
|
|
|
|
|
describe('GlCountdown', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
let wrapper;
|
2018-12-13 13:39:08 +05:30
|
|
|
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(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.destroy();
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('when there is time remaining', () => {
|
2022-04-04 11:22:00 +05:30
|
|
|
beforeEach(async () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper = mount(GlCountdown, {
|
|
|
|
propsData: {
|
|
|
|
endDateString: '2000-01-01T01:02:03Z',
|
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays remaining time', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.text()).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();
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.text()).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 () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper = mount(GlCountdown, {
|
|
|
|
propsData: {
|
|
|
|
endDateString: '1900-01-01T00:00:00Z',
|
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays 00:00:00', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.text()).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', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper = mount(GlCountdown, {
|
|
|
|
propsData: {
|
|
|
|
endDateString: 'this is invalid',
|
|
|
|
},
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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"/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|