Skip to content

Commit

Permalink
Fixes notification amount
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jan 6, 2020
1 parent b7271fb commit 8dbeee6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class Panel extends React.Component<Props, State> {

if (result === '0') {
const currency = onlyAnonWallet ? getMessage('bap') : getMessage('bat')
const contributionAmount = parseFloat(notification.args[3]).toFixed(1)
const contributionAmount = utils.handleContributionAmount(notification.args[3])
text = getMessage('contributeNotificationSuccess', [contributionAmount, currency])
} else if (result === '15') {
text = getMessage('contributeNotificationNotEnoughFunds')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import BigNumber from 'bignumber.js'

import { getMessage } from './background/api/locale_api'
import { WalletState } from '../../ui/components/walletWrapper'

Expand All @@ -23,6 +25,24 @@ export const formatConverted = (converted: string, currency: string = 'USD'): st
return `${converted} ${currency}`
}

export const handleContributionAmount = (amount: string) => {
let result = '0.0'
const amountSplit = amount.split('.')
if (amountSplit && amountSplit[0].length > 18) {
const result = new BigNumber(amount).dividedBy('1e18').toFixed(1, BigNumber.ROUND_UP)

return result
} else {
result = parseFloat(amount).toFixed(1)
}

if (result === 'NaN') {
return '0.0'
}

return result
}

export const generatePromotions = (promotions?: RewardsExtension.Promotion[]) => {
if (!promotions) {
return []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import { convertBalance } from '../../../../brave_rewards/resources/extension/brave_rewards/utils'
import { convertBalance, handleContributionAmount } from '../../../../brave_rewards/resources/extension/brave_rewards/utils'

describe('Rewards Panel extension - Utils', () => {
describe('convertBalance', () => {
Expand All @@ -27,4 +27,25 @@ describe('Rewards Panel extension - Utils', () => {
expect(convertBalance('10', { 'USD': 10, 'EUR': 4 }, 'EUR')).toBe('40.00')
})
})

describe('handleContributionAmount', () => {
it('amount is 0', () => {
expect(handleContributionAmount('0')).toBe('0.0')
})
it('amount is in wrong format', () => {
expect(handleContributionAmount('dasdfasdfasdf')).toBe('0.0')
})
it('amount is probi', () => {
expect(handleContributionAmount('1000000000000000000')).toBe('1.0')
})
it('amount is probi', () => {
expect(handleContributionAmount('10454000000000000000')).toBe('10.5')
})
it('amount is double', () => {
expect(handleContributionAmount('10.454545')).toBe('10.5')
})
it('amount is int', () => {
expect(handleContributionAmount('10')).toBe('10.0')
})
})
})

0 comments on commit 8dbeee6

Please sign in to comment.