From dcefa04db4d6f07486780c7dd0052565f19e3e1a Mon Sep 17 00:00:00 2001 From: Mohamed Shadab Date: Wed, 13 Mar 2019 00:32:18 +0530 Subject: [PATCH] Issue #32 - Fixes bug in code for putting bugs into buckets (#33) Problem: The Date() API creates a date object with the current UTC time, where as when the same object is passed into moment its converted to the local timezone, so when ever the local time zone has moved to the next day and UTC is behind(or vice-versa), one of the tests fail. Fix: Create date objects using moment instead of the Date() API. --- src/utils/toDayOfWeek.js | 7 +++++-- test/utils/toDayOfWeek.test.js | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/utils/toDayOfWeek.js b/src/utils/toDayOfWeek.js index 06cdd0e..e01f6b6 100644 --- a/src/utils/toDayOfWeek.js +++ b/src/utils/toDayOfWeek.js @@ -2,14 +2,17 @@ import moment from 'moment'; // By default it changes the day to Friday // We're interested to know the state of bucket by the end of the week -const toDayOfWeek = (dt = new Date(), dayOfWeek = 5) => { +const toDayOfWeek = (dt = moment().utc(), dayOfWeek = 5) => { let increment = 0; // isoWeekDay represents Sunday as 7 instead of 0 const day = moment(dt).isoWeekday(); if (day > dayOfWeek) { increment = 7; } - return moment(dt).add(increment, 'days').isoWeekday(dayOfWeek).format('YYYY-MM-DD'); + return moment(dt) + .add(increment, 'days') + .isoWeekday(dayOfWeek) + .format('YYYY-MM-DD'); }; export default toDayOfWeek; diff --git a/test/utils/toDayOfWeek.test.js b/test/utils/toDayOfWeek.test.js index f75e633..1023053 100644 --- a/test/utils/toDayOfWeek.test.js +++ b/test/utils/toDayOfWeek.test.js @@ -1,3 +1,4 @@ +import moment from 'moment'; import toDayOfWeek from '../../src/utils/toDayOfWeek'; it('Monday to Friday', () => { @@ -22,8 +23,8 @@ it('Sunday to Friday', () => { it('Current - To Friday of current week', () => { const newDate = toDayOfWeek(); - const myTempDate = new Date(); - const distance = 5 - myTempDate.getDay(); // 5 represents Friday - myTempDate.setDate(myTempDate.getDate() + distance); - expect(newDate).toBe((myTempDate).toISOString().split('T')[0]); + const myTempDate = moment().utc(); + const distance = 5 - myTempDate.day(); // 5 represents Friday + myTempDate.date(myTempDate.date() + distance); + expect(moment(myTempDate.format('YYYY-MM-DD')).isSame(newDate)).toBe(true); });