diff options
Diffstat (limited to 'spec/frontend/vue_shared/components/date_time_picker/date_time_picker_lib_spec.js')
-rw-r--r-- | spec/frontend/vue_shared/components/date_time_picker/date_time_picker_lib_spec.js | 178 |
1 files changed, 100 insertions, 78 deletions
diff --git a/spec/frontend/vue_shared/components/date_time_picker/date_time_picker_lib_spec.js b/spec/frontend/vue_shared/components/date_time_picker/date_time_picker_lib_spec.js index 98962918b49..e46c63a1a32 100644 --- a/spec/frontend/vue_shared/components/date_time_picker/date_time_picker_lib_spec.js +++ b/spec/frontend/vue_shared/components/date_time_picker/date_time_picker_lib_spec.js @@ -1,7 +1,13 @@ -import * as dateTimePickerLib from '~/vue_shared/components/date_time_picker/date_time_picker_lib'; +import timezoneMock from 'timezone-mock'; + +import { + isValidInputString, + inputStringToIsoDate, + isoDateToInputString, +} from '~/vue_shared/components/date_time_picker/date_time_picker_lib'; describe('date time picker lib', () => { - describe('isValidDate', () => { + describe('isValidInputString', () => { [ { input: '2019-09-09T00:00:00.000Z', @@ -48,121 +54,137 @@ describe('date time picker lib', () => { output: false, }, ].forEach(({ input, output }) => { - it(`isValidDate return ${output} for ${input}`, () => { - expect(dateTimePickerLib.isValidDate(input)).toBe(output); + it(`isValidInputString return ${output} for ${input}`, () => { + expect(isValidInputString(input)).toBe(output); }); }); }); - describe('stringToISODate', () => { - ['', 'null', undefined, 'abc'].forEach(input => { + describe('inputStringToIsoDate', () => { + [ + '', + 'null', + undefined, + 'abc', + 'xxxx-xx-xx', + '9999-99-19', + '2019-19-23', + '2019-09-23 x', + '2019-09-29 24:24:24', + ].forEach(input => { it(`throws error for invalid input like ${input}`, () => { - expect(() => dateTimePickerLib.stringToISODate(input)).toThrow(); + expect(() => inputStringToIsoDate(input)).toThrow(); }); }); + [ { - input: '2019-09-09 01:01:01', - output: '2019-09-09T01:01:01Z', + input: '2019-09-08 01:01:01', + output: '2019-09-08T01:01:01Z', }, { - input: '2019-09-09 00:00:00', - output: '2019-09-09T00:00:00Z', + input: '2019-09-08 00:00:00', + output: '2019-09-08T00:00:00Z', }, { - input: '2019-09-09 23:59:59', - output: '2019-09-09T23:59:59Z', + input: '2019-09-08 23:59:59', + output: '2019-09-08T23:59:59Z', }, { - input: '2019-09-09', - output: '2019-09-09T00:00:00Z', + input: '2019-09-08', + output: '2019-09-08T00:00:00Z', }, - ].forEach(({ input, output }) => { - it(`returns ${output} from ${input}`, () => { - expect(dateTimePickerLib.stringToISODate(input)).toBe(output); - }); - }); - }); - - describe('truncateZerosInDateTime', () => { - [ { - input: '', - output: '', + input: '2019-09-08', + output: '2019-09-08T00:00:00Z', }, { - input: '2019-10-10', - output: '2019-10-10', + input: '2019-09-08 00:00:00', + output: '2019-09-08T00:00:00Z', }, { - input: '2019-10-10 00:00:01', - output: '2019-10-10 00:00:01', + input: '2019-09-08 23:24:24', + output: '2019-09-08T23:24:24Z', }, { - input: '2019-10-10 00:00:00', - output: '2019-10-10', + input: '2019-09-08 0:0:0', + output: '2019-09-08T00:00:00Z', }, ].forEach(({ input, output }) => { - it(`truncateZerosInDateTime return ${output} for ${input}`, () => { - expect(dateTimePickerLib.truncateZerosInDateTime(input)).toBe(output); + it(`returns ${output} from ${input}`, () => { + expect(inputStringToIsoDate(input)).toBe(output); }); }); + + describe('timezone formatting', () => { + const value = '2019-09-08 01:01:01'; + const utcResult = '2019-09-08T01:01:01Z'; + const localResult = '2019-09-08T08:01:01Z'; + + test.each` + val | locatTimezone | utc | result + ${value} | ${'UTC'} | ${undefined} | ${utcResult} + ${value} | ${'UTC'} | ${false} | ${utcResult} + ${value} | ${'UTC'} | ${true} | ${utcResult} + ${value} | ${'US/Pacific'} | ${undefined} | ${localResult} + ${value} | ${'US/Pacific'} | ${false} | ${localResult} + ${value} | ${'US/Pacific'} | ${true} | ${utcResult} + `( + 'when timezone is $locatTimezone, formats $result for utc = $utc', + ({ val, locatTimezone, utc, result }) => { + timezoneMock.register(locatTimezone); + + expect(inputStringToIsoDate(val, utc)).toBe(result); + + timezoneMock.unregister(); + }, + ); + }); }); - describe('isDateTimePickerInputValid', () => { + describe('isoDateToInputString', () => { [ { - input: null, - output: false, - }, - { - input: '', - output: false, + input: '2019-09-08T01:01:01Z', + output: '2019-09-08 01:01:01', }, { - input: 'xxxx-xx-xx', - output: false, + input: '2019-09-08T01:01:01.999Z', + output: '2019-09-08 01:01:01', }, { - input: '9999-99-19', - output: false, - }, - { - input: '2019-19-23', - output: false, - }, - { - input: '2019-09-23', - output: true, - }, - { - input: '2019-09-23 x', - output: false, - }, - { - input: '2019-09-29 0:0:0', - output: false, - }, - { - input: '2019-09-29 00:00:00', - output: true, - }, - { - input: '2019-09-29 24:24:24', - output: false, - }, - { - input: '2019-09-29 23:24:24', - output: true, - }, - { - input: '2019-09-29 23:24:24 ', - output: false, + input: '2019-09-08T00:00:00Z', + output: '2019-09-08 00:00:00', }, ].forEach(({ input, output }) => { it(`returns ${output} for ${input}`, () => { - expect(dateTimePickerLib.isDateTimePickerInputValid(input)).toBe(output); + expect(isoDateToInputString(input)).toBe(output); }); }); + + describe('timezone formatting', () => { + const value = '2019-09-08T08:01:01Z'; + const utcResult = '2019-09-08 08:01:01'; + const localResult = '2019-09-08 01:01:01'; + + test.each` + val | locatTimezone | utc | result + ${value} | ${'UTC'} | ${undefined} | ${utcResult} + ${value} | ${'UTC'} | ${false} | ${utcResult} + ${value} | ${'UTC'} | ${true} | ${utcResult} + ${value} | ${'US/Pacific'} | ${undefined} | ${localResult} + ${value} | ${'US/Pacific'} | ${false} | ${localResult} + ${value} | ${'US/Pacific'} | ${true} | ${utcResult} + `( + 'when timezone is $locatTimezone, formats $result for utc = $utc', + ({ val, locatTimezone, utc, result }) => { + timezoneMock.register(locatTimezone); + + expect(isoDateToInputString(val, utc)).toBe(result); + + timezoneMock.unregister(); + }, + ); + }); }); }); |