-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: refactor calculate votes percentages and create tests (#5691)
* refactor: extract calculate percentages function to a separate file * creates tests
- Loading branch information
1 parent
109f83a
commit 6300d85
Showing
5 changed files
with
67 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/shared/lib/contexts/governance/utils/getPercentagesFromAnswerStatuses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { AnswerStatus } from '@iota/wallet' | ||
|
||
export function getPercentagesFromAnswerStatuses(answerStatuses: AnswerStatus[]): { [key: number]: string } { | ||
const totalVotes = answerStatuses.reduce((acc, answerStatus) => acc + answerStatus.accumulated, 0) | ||
if (totalVotes === 0 || Number.isNaN(totalVotes)) { | ||
return | ||
} | ||
|
||
let percentages: { [key: number]: string } = {} | ||
answerStatuses.forEach((answerStatus) => { | ||
if (answerStatus.value !== undefined) { | ||
const divisionResult = (answerStatus.accumulated ?? 0) / totalVotes | ||
percentages = { | ||
...percentages, | ||
[answerStatus.value]: Number.isNaN(divisionResult) ? '0%' : `${Math.round(divisionResult * 100)}%`, | ||
} | ||
} | ||
}) | ||
|
||
return percentages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/shared/lib/contexts/governance/utils/tests/getPercentagesFromAnswerStatuses.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import '@mocks/i18n.mock' | ||
import type { AnswerStatus } from '@iota/wallet' | ||
|
||
import { getPercentagesFromAnswerStatuses } from '../getPercentagesFromAnswerStatuses' | ||
|
||
describe('File: getPercentagesFromAnswerStatuses.ts', () => { | ||
describe('Function: getPercentagesFromAnswerStatuses', () => { | ||
const ANSWER_STATUSES: AnswerStatus[] = [ | ||
{ | ||
value: 0, | ||
current: 0, | ||
accumulated: 1000, | ||
}, | ||
{ | ||
value: 1, | ||
current: 0, | ||
accumulated: 2000, | ||
}, | ||
{ | ||
value: 2, | ||
current: 0, | ||
accumulated: 3000, | ||
}, | ||
] | ||
|
||
it('should return percentages from valid arguments', () => { | ||
expect(getPercentagesFromAnswerStatuses(ANSWER_STATUSES)).toEqual({ 0: '17%', 1: '33%', 2: '50%' }) | ||
}) | ||
it('should return undefined from invalid arguments', () => { | ||
expect(getPercentagesFromAnswerStatuses([{} as AnswerStatus])).toBeUndefined() | ||
}) | ||
it('should return undefined from empty arguments', () => { | ||
expect(getPercentagesFromAnswerStatuses([])).toBeUndefined() | ||
}) | ||
}) | ||
}) |