Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: percentages undefined #5703

Merged
merged 3 commits into from Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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({})
})
})
})