From 84ad619feb335b23d7c4ab9ce6a3b34aa0a801e3 Mon Sep 17 00:00:00 2001 From: ZL Asica <40444637+ZL-Asica@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:37:05 -0600 Subject: [PATCH] fix(strak-count): Handling when completedDays is null --- src/hooks/useGoalsUpdater.test.js | 7 ++----- src/utils/streakUtils.js | 8 ++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/hooks/useGoalsUpdater.test.js b/src/hooks/useGoalsUpdater.test.js index db3b6be..8179e1e 100644 --- a/src/hooks/useGoalsUpdater.test.js +++ b/src/hooks/useGoalsUpdater.test.js @@ -105,9 +105,6 @@ describe('useGoalsUpdater', () => { ], }) - // should be empty Array - expect(user.streak).toEqual([]) - await goalsUpdater.toggleTaskCompletion( goalIndex, microGoalIndex, @@ -118,8 +115,8 @@ describe('useGoalsUpdater', () => { user.goals[goalIndex].microgoals[microGoalIndex].tasks[taskIndex] expect(task.completed).toBe(true) - // check streak is not empty - expect(user.streak).not.toEqual([]) + // Check user.streak.completedDays length is 1 + expect(Object.keys(user.streak.completedDays)).toHaveLength(1) }) it('should delete a specified task', async () => { diff --git a/src/utils/streakUtils.js b/src/utils/streakUtils.js index 121c5c1..46d7c4b 100644 --- a/src/utils/streakUtils.js +++ b/src/utils/streakUtils.js @@ -28,12 +28,16 @@ export const getChicagoDate = () => { */ export const updateStreakDays = (streak, countChange) => { const currentDate = getChicagoDate() + const completedDays = streak.completedDays || {} // Get the current count for the current date or initialize it to 0 - const currentCount = streak.completedDays[currentDate] || 0 + const currentCount = completedDays[currentDate] || 0 // Update the count for the current date - streak.completedDays[currentDate] = Math.max(0, currentCount + countChange) + streak.completedDays = { + ...completedDays, + [currentDate]: Math.max(0, currentCount + countChange), + } return streak }