Skip to content

Commit

Permalink
Merge pull request #3138 from goratt12/3135-fix-sort-by-comment
Browse files Browse the repository at this point in the history
fix(research): fix sort by comments
  • Loading branch information
benfurber authored Jan 3, 2024
2 parents 157684f + c541a67 commit 7e57be2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('src/utils/helpers', () => {
describe('calculateTotalComments Function', () => {
it('should return 0 when item has no updates', () => {
const item = { item: {} } as any
expect(calculateTotalComments(item)).toBe('0')
expect(calculateTotalComments(item)).toBe(0)
})

it('should return 0 when updates have no comments', () => {
Expand All @@ -221,7 +221,7 @@ describe('src/utils/helpers', () => {
}),
),
} as IResearch.ItemDB | IItem
expect(calculateTotalComments(item)).toBe('0')
expect(calculateTotalComments(item)).toBe(0)
})

it('should return the correct amount of comments', () => {
Expand All @@ -234,7 +234,7 @@ describe('src/utils/helpers', () => {
}),
),
} as IResearch.ItemDB | IItem
expect(calculateTotalComments(item)).toBe('9')
expect(calculateTotalComments(item)).toBe(9)
})

it('should ignore deleted and draft updates', () => {
Expand All @@ -260,7 +260,7 @@ describe('src/utils/helpers', () => {
}),
]),
} as IResearch.ItemDB | IItem
expect(calculateTotalComments(item)).toBe('4')
expect(calculateTotalComments(item)).toBe(4)
})
})

Expand Down
8 changes: 5 additions & 3 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ export const isAllowedToPin = (pin: IMapPin, user?: IUser) => {
}
}

export const calculateTotalComments = (item: IResearch.ItemDB | IItem) => {
export const calculateTotalComments = (
item: IResearch.ItemDB | IItem,
): number => {
if (item.updates) {
const commentOnUpdates = item.updates.reduce((totalComments, update) => {
const updateCommentsLength =
Expand All @@ -177,9 +179,9 @@ export const calculateTotalComments = (item: IResearch.ItemDB | IItem) => {
return totalComments + updateCommentsLength
}, 0)

return commentOnUpdates ? String(commentOnUpdates) : '0'
return commentOnUpdates ? commentOnUpdates : 0
} else {
return '0'
return 0
}
}

Expand Down

0 comments on commit 7e57be2

Please sign in to comment.