Skip to content

Commit

Permalink
fix(strak-count): Handling when completedDays is null
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica committed Nov 8, 2024
1 parent 440dadf commit 84ad619
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/hooks/useGoalsUpdater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ describe('useGoalsUpdater', () => {
],
})

// should be empty Array
expect(user.streak).toEqual([])

await goalsUpdater.toggleTaskCompletion(
goalIndex,
microGoalIndex,
Expand All @@ -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 () => {
Expand Down
8 changes: 6 additions & 2 deletions src/utils/streakUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 84ad619

Please sign in to comment.