From b8cdcf81aea22c112fcf43107d34ecbc44b21455 Mon Sep 17 00:00:00 2001 From: AlaoPrado Date: Thu, 19 Nov 2020 13:45:53 -0300 Subject: [PATCH 1/3] feat(test): add toDatetimeLocal unit test --- test/formatters.test.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/formatters.test.js diff --git a/test/formatters.test.js b/test/formatters.test.js new file mode 100644 index 0000000..b6fdf62 --- /dev/null +++ b/test/formatters.test.js @@ -0,0 +1,36 @@ +import {toDatetimeLocal} from '../src/utils/formatters' + +test('toDatetimeLocal should format date object into YYYY-MM-DDThh:mm', () =>{ + // common dates + let year = 2020; + let monthList = [2, 10]; + let dayList = [1, 19]; // 1 to 12 + let hourList = [1, 12]; + let minutesList = [1, 30]; + let secondsList = [0, 30]; + let millisecondsList = [0, 100]; + for(let i = 0; i < 2; i++){ + let month = monthList[i] - 1; // 0 to 11 + let date = new Date(year, month, dayList[i], hourList[i], + minutesList[i], secondsList[i], millisecondsList[i]); + let yearFormat = year.toString(); + let monthFormat = monthList[i].toString().padStart(2, '0'); + let dayFormat = dayList[i].toString().padStart(2, '0'); + let hourFormat = hourList[i].toString().padStart(2, '0'); + let minutesFormat = minutesList[i].toString().padStart(2, '0'); + let dateFormat = toDatetimeLocal(date); + expect(dateFormat).toBe(`${yearFormat}-${monthFormat}-${dayFormat}T${hourFormat}:${minutesFormat}`); + } + + // common strings + let dateString = '2020-11-19T16:35:00.000Z' + let dateFormat = toDatetimeLocal(dateString); + expect(dateFormat).toBe('2020-11-19T13:35'); // GMT -03:00 because of the timezone + + // invalid parameter + expect(() => toDatetimeLocal(2020)).toThrow(Error) + expect(() => toDatetimeLocal('2020')).toThrow(Error) + expect(() => toDatetimeLocal('19/11/2020')).toThrow(Error) + expect(() => toDatetimeLocal(undefined)).toThrow(Error) + expect(() => toDatetimeLocal(-1)).toThrow(Error) +}) \ No newline at end of file From eddc8ad2ad47add35c2ac2075a42e05ffa59e830 Mon Sep 17 00:00:00 2001 From: AlaoPrado Date: Thu, 19 Nov 2020 14:08:59 -0300 Subject: [PATCH 2/3] fix(test) fix toDatetimeLocal timezone offset --- test/formatters.test.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/formatters.test.js b/test/formatters.test.js index b6fdf62..eeb1745 100644 --- a/test/formatters.test.js +++ b/test/formatters.test.js @@ -23,9 +23,23 @@ test('toDatetimeLocal should format date object into YYYY-MM-DDThh:mm', () =>{ } // common strings - let dateString = '2020-11-19T16:35:00.000Z' + let day = 15; + let hour = 12; + let dateString = '2020-06-'+ day.toString() + 'T' + + hour.toString().padStart(2, '0') + ':00:00.000Z'; + var offset = new Date().getTimezoneOffset() / 60; + hour += -offset; + if(hour >= 24){ + day++; + hour -= 24; + } + else if(hour < 0){ + day--; + hour += 24; + } let dateFormat = toDatetimeLocal(dateString); - expect(dateFormat).toBe('2020-11-19T13:35'); // GMT -03:00 because of the timezone + expect(dateFormat).toBe('2020-06-'+ day.toString() + 'T' + + hour.toString().padStart(2, '0') + ':00'); // invalid parameter expect(() => toDatetimeLocal(2020)).toThrow(Error) From cb5d47fa36fa93911d572ab7980fb63518abfd25 Mon Sep 17 00:00:00 2001 From: AlaoPrado Date: Thu, 19 Nov 2020 14:39:43 -0300 Subject: [PATCH 3/3] fix (test) add ; + /n in formatter teest --- test/formatters.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/formatters.test.js b/test/formatters.test.js index eeb1745..b6f07c4 100644 --- a/test/formatters.test.js +++ b/test/formatters.test.js @@ -42,9 +42,9 @@ test('toDatetimeLocal should format date object into YYYY-MM-DDThh:mm', () =>{ hour.toString().padStart(2, '0') + ':00'); // invalid parameter - expect(() => toDatetimeLocal(2020)).toThrow(Error) - expect(() => toDatetimeLocal('2020')).toThrow(Error) - expect(() => toDatetimeLocal('19/11/2020')).toThrow(Error) - expect(() => toDatetimeLocal(undefined)).toThrow(Error) - expect(() => toDatetimeLocal(-1)).toThrow(Error) -}) \ No newline at end of file + expect(() => toDatetimeLocal(2020)).toThrow(Error); + expect(() => toDatetimeLocal('2020')).toThrow(Error); + expect(() => toDatetimeLocal('19/11/2020')).toThrow(Error); + expect(() => toDatetimeLocal(undefined)).toThrow(Error); + expect(() => toDatetimeLocal(-1)).toThrow(Error); +})