Skip to content

Commit

Permalink
fix: top voters sort in back (#1246)
Browse files Browse the repository at this point in the history
* fix: sort top voters in the back

* chore: add test for no votes case
  • Loading branch information
1emu authored Sep 7, 2023
1 parent b354724 commit 6396428
Show file tree
Hide file tree
Showing 3 changed files with 2,305 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/back/services/vote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { def, get } from 'bdd-lazy-var/getter'

import { SnapshotService } from '../../services/SnapshotService'
import Time from '../../utils/date/Time'
import { getPreviousMonthStartAndEnd } from '../../utils/date/getPreviousMonthStartAndEnd'
import { SNAPSHOT_VOTES_AUGUST_2023 } from '../../utils/votes/utils.testData'

import { VoteService } from './vote'

import clearAllMocks = jest.clearAllMocks

describe('getTopVoters', () => {
const firstOfAugust = Time.utc('2023-08-1T00:00:000Z').toDate()
const { start, end } = getPreviousMonthStartAndEnd(firstOfAugust)

beforeAll(() => {
jest.spyOn(SnapshotService, 'getAllVotesBetweenDates').mockResolvedValue(SNAPSHOT_VOTES_AUGUST_2023)
})

describe('when fetching the top 3', () => {
def('topVoters', async () => await VoteService.getTopVoters(start, end, 3))

it('should return the top 3 voters sorted by votes in descending order', async () => {
expect(await get.topVoters).toEqual([
{
address: '0xa2f8cd45cd7ea14bce6e87f177cf9df928a089a5',
votes: 79,
},
{
address: '0x2684a202a374d87bb321a744482b89bf6deaf8bd',
votes: 75,
},
{
address: '0x8218a2445679e38f358e42f88fe2125c98440d59',
votes: 73,
},
])
})
})

describe('when called with the default params', () => {
def('topVoters', async () => await VoteService.getTopVoters(start, end))

it('should return the top 5 voters sorted by votes in descending order', async () => {
expect(await get.topVoters).toEqual([
{
address: '0xa2f8cd45cd7ea14bce6e87f177cf9df928a089a5',
votes: 79,
},
{
address: '0x2684a202a374d87bb321a744482b89bf6deaf8bd',
votes: 75,
},
{
address: '0x8218a2445679e38f358e42f88fe2125c98440d59',
votes: 73,
},
{
address: '0xa77294828d42b538890fa6e97adffe9305536171',
votes: 53,
},
{
address: '0x4534c46ea854c9a302d3dc95b2d3253ae6a28abc',
votes: 21,
},
])
})

describe('when there are no votes on the selected time period', () => {
beforeEach(() => {
clearAllMocks()
jest.spyOn(SnapshotService, 'getAllVotesBetweenDates').mockResolvedValue([])
})

it('should return an empty array', async () => {
expect(await get.topVoters).toEqual([])
})
})
})
})
4 changes: 3 additions & 1 deletion src/back/services/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export class VoteService {

static async getTopVoters(start: Date, end: Date, limit = DEFAULT_TOP_VOTERS_LIMIT) {
const votes = await SnapshotService.getAllVotesBetweenDates(new Date(start), new Date(end))
return this.countVotesByUser(votes).slice(0, limit)
return this.countVotesByUser(votes)
.sort((a, b) => b['votes'] - a['votes'])
.slice(0, limit)
}

private static countVotesByUser(votes: SnapshotVote[]) {
Expand Down
Loading

0 comments on commit 6396428

Please sign in to comment.