Skip to content

Commit

Permalink
fix: percentages undefined (#5703)
Browse files Browse the repository at this point in the history
* fix: percentages undefined

* refactor: adds type for percentages

* fix: adds correct return type to getPercentages function
  • Loading branch information
jeeanribeiro committed Jan 30, 2023
1 parent 4044b0d commit e9f6d72
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/shared/components/ProposalQuestion.svelte
Expand Up @@ -7,7 +7,7 @@
import { selectedProposal } from '@contexts/governance/stores'
import { Icon as IconEnum } from '@auxiliary/icon'
import { Position } from './enums'
import { getPercentagesFromAnswerStatuses } from '@contexts/governance'
import { getPercentagesFromAnswerStatuses, IProposalAnswerPercentages } from '@contexts/governance'
const dispatch = createEventDispatcher()
Expand All @@ -18,7 +18,7 @@
export let selectedAnswerValue: number = undefined
export let votedAnswerValue: number = undefined
let percentages: { [key: number]: string } = {}
let percentages: IProposalAnswerPercentages = {}
let winnerAnswerIndex: number
$: answers = [...question?.answers, { value: 0, text: 'Abstain', additionalInfo: '' }]
Expand Down
@@ -1,4 +1,5 @@
export * from './organization.interface'
export * from './proposal-answer-percentages.interface'
export * from './proposal-filter.interface'
export * from './proposal-state.interface'
export * from './proposal.interface'
Expand Down
@@ -0,0 +1,3 @@
export interface IProposalAnswerPercentages {
[answerIndex: number]: string
}
@@ -1,12 +1,13 @@
import type { AnswerStatus } from '@iota/wallet'
import type { AnswerStatus } from '@iota/wallet/out/types'
import { IProposalAnswerPercentages } from '../interfaces'

export function getPercentagesFromAnswerStatuses(answerStatuses: AnswerStatus[]): { [key: number]: string } {
export function getPercentagesFromAnswerStatuses(answerStatuses: AnswerStatus[]): IProposalAnswerPercentages {
const totalVotes = answerStatuses.reduce((acc, answerStatus) => acc + answerStatus.accumulated, 0)
if (totalVotes === 0 || Number.isNaN(totalVotes)) {
return
return {}
}

let percentages: { [key: number]: string } = {}
let percentages: IProposalAnswerPercentages = {}
answerStatuses.forEach((answerStatus) => {
if (answerStatus.value !== undefined) {
const divisionResult = (answerStatus.accumulated ?? 0) / totalVotes
Expand Down
Expand Up @@ -26,11 +26,11 @@ describe('File: getPercentagesFromAnswerStatuses.ts', () => {
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 empty object from invalid arguments', () => {
expect(getPercentagesFromAnswerStatuses([{} as AnswerStatus])).toEqual({})
})
it('should return undefined from empty arguments', () => {
expect(getPercentagesFromAnswerStatuses([])).toBeUndefined()
it('should return empty object from empty arguments', () => {
expect(getPercentagesFromAnswerStatuses([])).toEqual({})
})
})
})

0 comments on commit e9f6d72

Please sign in to comment.