You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.0 KiB
130 lines
3.0 KiB
4 years ago
|
import { renderHook } from '@testing-library/react-hooks';
|
||
|
import * as reactRedux from 'react-redux';
|
||
|
import sinon from 'sinon';
|
||
4 years ago
|
import {
|
||
|
getCurrentCurrency,
|
||
|
getNativeCurrency,
|
||
|
getConversionRate,
|
||
4 years ago
|
} from '../selectors';
|
||
|
import { useCurrencyDisplay } from './useCurrencyDisplay';
|
||
5 years ago
|
|
||
|
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',
|
||
|
},
|
||
|
},
|
||
4 years ago
|
];
|
||
5 years ago
|
|
||
4 years ago
|
describe('useCurrencyDisplay', () => {
|
||
5 years ago
|
tests.forEach(({ input: { value, ...restProps }, result }) => {
|
||
4 years ago
|
describe(`when input is { value: ${value}, decimals: ${restProps.numberOfDecimals}, denomation: ${restProps.denomination} }`, () => {
|
||
4 years ago
|
const stub = sinon.stub(reactRedux, 'useSelector');
|
||
5 years ago
|
stub.callsFake((selector) => {
|
||
|
if (selector === getCurrentCurrency) {
|
||
4 years ago
|
return 'usd';
|
||
5 years ago
|
} else if (selector === getNativeCurrency) {
|
||
4 years ago
|
return 'ETH';
|
||
5 years ago
|
} else if (selector === getConversionRate) {
|
||
4 years ago
|
return 280.45;
|
||
5 years ago
|
}
|
||
4 years ago
|
return undefined;
|
||
|
});
|
||
|
const hookReturn = renderHook(() => useCurrencyDisplay(value, restProps));
|
||
|
const [displayValue, parts] = hookReturn.result.current;
|
||
|
stub.restore();
|
||
4 years ago
|
it(`should return ${result.displayValue} as displayValue`, () => {
|
||
|
expect(displayValue).toStrictEqual(result.displayValue);
|
||
4 years ago
|
});
|
||
4 years ago
|
it(`should return ${result.value} as value`, () => {
|
||
|
expect(parts.value).toStrictEqual(result.value);
|
||
4 years ago
|
});
|
||
4 years ago
|
it(`should return ${result.suffix} as suffix`, () => {
|
||
|
expect(parts.suffix).toStrictEqual(result.suffix);
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|