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.
149 lines
3.3 KiB
149 lines
3.3 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 { getPreferences, getShouldShowFiat } from '../selectors';
|
||
|
import { useUserPreferencedCurrency } from './useUserPreferencedCurrency';
|
||
5 years ago
|
|
||
|
const tests = [
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: true,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: true,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'PRIMARY',
|
||
|
},
|
||
|
result: {
|
||
|
currency: 'ETH',
|
||
|
numberOfDecimals: 6,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: false,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: true,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'PRIMARY',
|
||
|
},
|
||
|
result: {
|
||
|
currency: undefined,
|
||
|
numberOfDecimals: 2,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: true,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: true,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'SECONDARY',
|
||
|
fiatNumberOfDecimals: 4,
|
||
|
fiatPrefix: '-',
|
||
|
},
|
||
|
result: {
|
||
|
currency: undefined,
|
||
|
numberOfDecimals: 4,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: false,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: true,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'SECONDARY',
|
||
|
fiatNumberOfDecimals: 4,
|
||
|
numberOfDecimals: 3,
|
||
|
fiatPrefix: 'a',
|
||
|
},
|
||
|
result: {
|
||
|
currency: 'ETH',
|
||
|
numberOfDecimals: 3,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: false,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: false,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'PRIMARY',
|
||
|
},
|
||
|
result: {
|
||
|
currency: 'ETH',
|
||
|
numberOfDecimals: 6,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: false,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: true,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'PRIMARY',
|
||
|
},
|
||
|
result: {
|
||
|
currency: undefined,
|
||
|
numberOfDecimals: 2,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
state: {
|
||
|
useNativeCurrencyAsPrimaryCurrency: false,
|
||
|
nativeCurrency: 'ETH',
|
||
|
showFiat: true,
|
||
|
},
|
||
|
params: {
|
||
|
type: 'PRIMARY',
|
||
|
},
|
||
|
result: {
|
||
|
currency: undefined,
|
||
|
numberOfDecimals: 2,
|
||
|
},
|
||
|
},
|
||
4 years ago
|
];
|
||
5 years ago
|
|
||
4 years ago
|
function getFakeUseSelector(state) {
|
||
5 years ago
|
return (selector) => {
|
||
5 years ago
|
if (selector === getPreferences) {
|
||
4 years ago
|
return state;
|
||
5 years ago
|
} else if (selector === getShouldShowFiat) {
|
||
4 years ago
|
return state.showFiat;
|
||
5 years ago
|
}
|
||
4 years ago
|
return state.nativeCurrency;
|
||
|
};
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
describe('useUserPreferencedCurrency', () => {
|
||
5 years ago
|
tests.forEach(({ params: { type, ...otherParams }, state, result }) => {
|
||
4 years ago
|
describe(`when showFiat is ${state.showFiat}, useNativeCurrencyAsPrimary is ${state.useNativeCurrencyAsPrimaryCurrency} and type is ${type}`, () => {
|
||
4 years ago
|
const stub = sinon.stub(reactRedux, 'useSelector');
|
||
|
stub.callsFake(getFakeUseSelector(state));
|
||
5 years ago
|
|
||
4 years ago
|
const { result: hookResult } = renderHook(() =>
|
||
|
useUserPreferencedCurrency(type, otherParams),
|
||
4 years ago
|
);
|
||
|
stub.restore();
|
||
4 years ago
|
it(`should return currency as ${
|
||
|
result.currency || 'not modified by user preferences'
|
||
4 years ago
|
}`, () => {
|
||
|
expect(hookResult.current.currency).toStrictEqual(result.currency);
|
||
4 years ago
|
});
|
||
4 years ago
|
it(`should return decimals as ${
|
||
|
result.numberOfDecimals || 'not modified by user preferences'
|
||
4 years ago
|
}`, () => {
|
||
|
expect(hookResult.current.numberOfDecimals).toStrictEqual(
|
||
4 years ago
|
result.numberOfDecimals,
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|