Commit 6bf2cb91 authored by Winnie Hellmann's avatar Winnie Hellmann

Add validation for date strings passed to GlCountdown

parent 6e680647
...@@ -6,9 +6,12 @@ import { calculateRemainingMilliseconds, formatTime } from '~/lib/utils/datetime ...@@ -6,9 +6,12 @@ import { calculateRemainingMilliseconds, formatTime } from '~/lib/utils/datetime
*/ */
export default { export default {
props: { props: {
endDate: { endDateString: {
type: String, type: String,
required: true, required: true,
validator(value) {
return !Number.isNaN(new Date(value).getTime());
},
}, },
}, },
...@@ -21,7 +24,7 @@ export default { ...@@ -21,7 +24,7 @@ export default {
mounted() { mounted() {
const updateRemainingTime = () => { const updateRemainingTime = () => {
const remainingMilliseconds = calculateRemainingMilliseconds(this.endDate); const remainingMilliseconds = calculateRemainingMilliseconds(this.endDateString);
this.remainingTime = formatTime(remainingMilliseconds); this.remainingTime = formatTime(remainingMilliseconds);
}; };
...@@ -38,8 +41,8 @@ export default { ...@@ -38,8 +41,8 @@ export default {
<template> <template>
<time <time
v-gl-tooltip v-gl-tooltip
:datetime="endDate" :datetime="endDateString"
:title="endDate" :title="endDateString"
> >
{{ remainingTime }} {{ remainingTime }}
</time> </time>
......
...@@ -20,7 +20,7 @@ describe('GlCountdown', () => { ...@@ -20,7 +20,7 @@ describe('GlCountdown', () => {
describe('when there is time remaining', () => { describe('when there is time remaining', () => {
beforeEach(done => { beforeEach(done => {
vm = mountComponent(Component, { vm = mountComponent(Component, {
endDate: '2000-01-01T01:02:03Z', endDateString: '2000-01-01T01:02:03Z',
}); });
Vue.nextTick() Vue.nextTick()
...@@ -48,7 +48,7 @@ describe('GlCountdown', () => { ...@@ -48,7 +48,7 @@ describe('GlCountdown', () => {
describe('when there is no time remaining', () => { describe('when there is no time remaining', () => {
beforeEach(done => { beforeEach(done => {
vm = mountComponent(Component, { vm = mountComponent(Component, {
endDate: '1900-01-01T00:00:00Z', endDateString: '1900-01-01T00:00:00Z',
}); });
Vue.nextTick() Vue.nextTick()
...@@ -60,4 +60,18 @@ describe('GlCountdown', () => { ...@@ -60,4 +60,18 @@ describe('GlCountdown', () => {
expect(vm.$el).toContainText('00:00:00'); expect(vm.$el).toContainText('00:00:00');
}); });
}); });
describe('when an invalid date is passed', () => {
it('throws a validation error', () => {
spyOn(Vue.config, 'warnHandler').and.stub();
vm = mountComponent(Component, {
endDateString: 'this is invalid',
});
expect(Vue.config.warnHandler).toHaveBeenCalledTimes(1);
const [errorMessage] = Vue.config.warnHandler.calls.argsFor(0);
expect(errorMessage).toMatch(/^Invalid prop: .* "endDateString"/);
});
});
}); });
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