Skip to content

Commit

Permalink
Adds tests for untils for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Oct 10, 2018
1 parent 02e7492 commit 58b4a6c
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 13 deletions.
8 changes: 7 additions & 1 deletion components/brave_rewards/extension/brave_rewards/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ export const formatConverted = (converted: string, currency: string = 'USD'): st
}

export const convertProbiToFixed = (probi: string, places: number = 1) => {
return new BigNumber(probi.toString()).dividedBy('1e18').toFixed(places, BigNumber.ROUND_DOWN)
const result = new BigNumber(probi).dividedBy('1e18').toFixed(places, BigNumber.ROUND_DOWN)

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

return result
}
8 changes: 7 additions & 1 deletion components/brave_rewards/ui/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,11 @@ export const getAddresses = (addresses?: Record<Rewards.AddressesType, Rewards.A
}

export const convertProbiToFixed = (probi: string, places: number = 1) => {
return new BigNumber(probi.toString()).dividedBy('1e18').toFixed(places, BigNumber.ROUND_DOWN)
const result = new BigNumber(probi).dividedBy('1e18').toFixed(places, BigNumber.ROUND_DOWN)

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

return result
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* global describe, it */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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, convertProbiToFixed } from '../../../../brave_rewards/extension/brave_rewards/utils'

describe('Rewards Panel extension - Utils', () => {
describe('convertBalance', () => {
it('token has letters', () => {
expect(convertBalance('test', { 'USD': 10 })).toBe('0.00')
})

it('rates are empty', () => {
expect(convertBalance('10', {})).toBe('0.00')
})

it('rate is missing', () => {
expect(convertBalance('10', { 'USD': 10 }, 'EUR')).toBe('0.00')
})

it('all good', () => {
expect(convertBalance('10', { 'USD': 10 })).toBe('100.00')
})

it('currency is provided', () => {
expect(convertBalance('10', { 'USD': 10, 'EUR': 4 }, 'EUR')).toBe('40.00')
})
})

describe('convertProbiToFixed', () => {
it('probi is not in correct format', () => {
expect(convertProbiToFixed('sdfs')).toBe('0.0')
})

it('probi is not 10^18', () => {
expect(convertProbiToFixed('9')).toBe('0.0')
})

it('we should always round down', () => {
expect(convertProbiToFixed('0999999999999999999')).toBe('0.9')
})

it('regular convert', () => {
expect(convertProbiToFixed('1559999999999999990')).toBe('1.5')
})

it('regular convert two places', () => {
expect(convertProbiToFixed('1559999999999999990', 2)).toBe('1.55')
})

it('big convert', () => {
expect(convertProbiToFixed('150000000000000000000000000')).toBe('150000000.0')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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 { types } from '../../../brave_rewards/ui/constants/rewards_types'
import * as actions from '../../../brave_rewards/ui/actions/rewards_actions'
import { types } from '../../../../brave_rewards/ui/constants/rewards_types'
import * as actions from '../../../../brave_rewards/ui/actions/rewards_actions'

describe('rewards_actions', () => {
it('createWallet', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import * as React from 'react'
import { shallow } from 'enzyme'
import { types } from '../../../brave_rewards/ui/constants/rewards_types'
import { rewardsInitialState } from '../../testData'
import { types } from '../../../../brave_rewards/ui/constants/rewards_types'
import { rewardsInitialState } from '../../../testData'
import {
App,
mapStateToProps,
mapDispatchToProps
} from '../../../brave_rewards/ui/components/app'
} from '../../../../brave_rewards/ui/components/app'

describe('rewardsPage component', () => {
describe('mapStateToProps', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,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 reducers from '../../../brave_rewards/ui/reducers/index'
import { rewardsInitialState } from '../../testData'
import reducers from '../../../../brave_rewards/ui/reducers/index'
import { rewardsInitialState } from '../../../testData'

describe('rewards reducers test', () => {
it('reducers are a combined reducer function', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* global chrome */

import reducers from '../../../brave_rewards/ui/reducers/index'
import * as actions from '../../../brave_rewards/ui/actions/rewards_actions'
import { types } from '../../../brave_rewards/ui/constants/rewards_types'
import { defaultState } from '../../../brave_rewards/ui/storage'
import reducers from '../../../../brave_rewards/ui/reducers/index'
import * as actions from '../../../../brave_rewards/ui/actions/rewards_actions'
import { types } from '../../../../brave_rewards/ui/constants/rewards_types'
import { defaultState } from '../../../../brave_rewards/ui/storage'

describe('wallet reducer', () => {
const constantDate = new Date('2018-01-01T12:00:00')
Expand Down
66 changes: 66 additions & 0 deletions components/test/brave_rewards/ui/utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* global describe, it */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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, formatConverted, convertProbiToFixed } from '../../../brave_rewards/ui/utils'

describe('Rewards Settings - Utils', () => {
describe('convertBalance', () => {
it('token has letters', () => {
expect(convertBalance('test', { 'USD': 10 })).toBe('0.00')
})

it('rates are empty', () => {
expect(convertBalance('10', {})).toBe('0.00')
})

it('rate is missing', () => {
expect(convertBalance('10', { 'USD': 10 }, 'EUR')).toBe('0.00')
})

it('all good', () => {
expect(convertBalance('10', { 'USD': 10 })).toBe('100.00')
})

it('currency is provided', () => {
expect(convertBalance('10', { 'USD': 10, 'EUR': 4 }, 'EUR')).toBe('40.00')
})
})

describe('formatConverted', () => {
it('currency is not provided', () => {
expect(formatConverted('10.00')).toBe('10.00 USD')
})

it('currency is provided', () => {
expect(formatConverted('10.00', 'EUR')).toBe('10.00 EUR')
})
})

describe('convertProbiToFixed', () => {
it('probi is not in correct format', () => {
expect(convertProbiToFixed('sdfs')).toBe('0.0')
})

it('probi is not 10^18', () => {
expect(convertProbiToFixed('9')).toBe('0.0')
})

it('we should always round down', () => {
expect(convertProbiToFixed('0999999999999999999')).toBe('0.9')
})

it('regular convert', () => {
expect(convertProbiToFixed('1559999999999999990')).toBe('1.5')
})

it('regular convert two places', () => {
expect(convertProbiToFixed('1559999999999999990', 2)).toBe('1.55')
})

it('big convert', () => {
expect(convertProbiToFixed('150000000000000000000000000')).toBe('150000000.0')
})
})
})

0 comments on commit 58b4a6c

Please sign in to comment.