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

Fixes notification amount #4300

Merged
merged 1 commit into from
Jan 6, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>= 19 would be easier to read as probi are always 19 characters

Copy link
Contributor Author

@NejcZdovc NejcZdovc Jan 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>= 19 and > 18 both tell you that we are looking for number 19. Personally I like to avoid >= as it's really easy to miss when reading through stuff

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easier to read if the magic number is replaced

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', () => {
tmancey marked this conversation as resolved.
Show resolved Hide resolved
expect(handleContributionAmount('10.454545')).toBe('10.5')
})
it('amount is int', () => {
expect(handleContributionAmount('10')).toBe('10.0')
})
})
})