2020-05-12 21:07:35 +02:00
|
|
|
import assert from 'assert'
|
|
|
|
import { renderHook } from '@testing-library/react-hooks'
|
|
|
|
import * as reactRedux from 'react-redux'
|
|
|
|
import sinon from 'sinon'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { useCurrencyDisplay } from '../useCurrencyDisplay'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getCurrentCurrency,
|
|
|
|
getNativeCurrency,
|
|
|
|
getConversionRate,
|
|
|
|
} from '../../selectors'
|
2020-05-12 21:07:35 +02:00
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x2386f26fc10000',
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
currency: 'usd',
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '$2.80',
|
|
|
|
suffix: 'USD',
|
|
|
|
displayValue: '$2.80 USD',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x2386f26fc10000',
|
|
|
|
currency: 'usd',
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '$2.80',
|
|
|
|
suffix: 'USD',
|
|
|
|
displayValue: '$2.80 USD',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x1193461d01595930',
|
|
|
|
currency: 'ETH',
|
|
|
|
numberOfDecimals: 3,
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '1.266',
|
|
|
|
suffix: 'ETH',
|
|
|
|
displayValue: '1.266 ETH',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x1193461d01595930',
|
|
|
|
currency: 'ETH',
|
|
|
|
numberOfDecimals: 3,
|
|
|
|
hideLabel: true,
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '1.266',
|
|
|
|
suffix: undefined,
|
|
|
|
displayValue: '1.266',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x3b9aca00',
|
|
|
|
currency: 'ETH',
|
|
|
|
denomination: 'GWEI',
|
|
|
|
hideLabel: true,
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '1',
|
|
|
|
suffix: undefined,
|
|
|
|
displayValue: '1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x3b9aca00',
|
|
|
|
currency: 'ETH',
|
|
|
|
denomination: 'WEI',
|
|
|
|
hideLabel: true,
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '1000000000',
|
|
|
|
suffix: undefined,
|
|
|
|
displayValue: '1000000000',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: {
|
|
|
|
value: '0x3b9aca00',
|
|
|
|
currency: 'ETH',
|
|
|
|
numberOfDecimals: 100,
|
|
|
|
hideLabel: true,
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
value: '0.000000001',
|
|
|
|
suffix: undefined,
|
|
|
|
displayValue: '0.000000001',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
describe('useCurrencyDisplay', function () {
|
|
|
|
tests.forEach(({ input: { value, ...restProps }, result }) => {
|
|
|
|
describe(`when input is { value: ${value}, decimals: ${restProps.numberOfDecimals}, denomation: ${restProps.denomination} }`, function () {
|
|
|
|
const stub = sinon.stub(reactRedux, 'useSelector')
|
2020-05-26 22:49:11 +02:00
|
|
|
stub.callsFake((selector) => {
|
|
|
|
if (selector === getCurrentCurrency) {
|
|
|
|
return 'usd'
|
|
|
|
} else if (selector === getNativeCurrency) {
|
|
|
|
return 'ETH'
|
|
|
|
} else if (selector === getConversionRate) {
|
|
|
|
return 280.45
|
|
|
|
}
|
2020-08-12 21:06:57 +02:00
|
|
|
return undefined
|
2020-05-26 22:49:11 +02:00
|
|
|
})
|
2020-05-12 21:07:35 +02:00
|
|
|
const hookReturn = renderHook(() => useCurrencyDisplay(value, restProps))
|
2020-08-19 18:27:05 +02:00
|
|
|
const [displayValue, parts] = hookReturn.result.current
|
2020-05-12 21:07:35 +02:00
|
|
|
stub.restore()
|
|
|
|
it(`should return ${result.displayValue} as displayValue`, function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(displayValue, result.displayValue)
|
2020-05-12 21:07:35 +02:00
|
|
|
})
|
|
|
|
it(`should return ${result.value} as value`, function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(parts.value, result.value)
|
2020-05-12 21:07:35 +02:00
|
|
|
})
|
|
|
|
it(`should return ${result.suffix} as suffix`, function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(parts.suffix, result.suffix)
|
2020-05-12 21:07:35 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|