1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/hooks/useUserPreferencedCurrency.test.js

156 lines
3.5 KiB
JavaScript
Raw Normal View History

import { renderHook } from '@testing-library/react-hooks';
import * as reactRedux from 'react-redux';
import sinon from 'sinon';
import {
getCurrentCurrency,
getPreferences,
getShouldShowFiat,
} from '../selectors';
import { useUserPreferencedCurrency } from './useUserPreferencedCurrency';
const tests = [
{
state: {
useNativeCurrencyAsPrimaryCurrency: true,
nativeCurrency: 'ETH',
showFiat: true,
},
params: {
type: 'PRIMARY',
},
result: {
currency: 'ETH',
numberOfDecimals: 8,
},
},
{
state: {
useNativeCurrencyAsPrimaryCurrency: false,
nativeCurrency: 'ETH',
showFiat: true,
currentCurrency: 'usd',
},
params: {
type: 'PRIMARY',
},
result: {
currency: 'usd',
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: 8,
},
},
{
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,
},
},
];
2020-11-03 00:41:28 +01:00
function getFakeUseSelector(state) {
return (selector) => {
if (selector === getPreferences) {
return state;
} else if (selector === getShouldShowFiat) {
return state.showFiat;
} else if (selector === getCurrentCurrency) {
return state.currentCurrency;
}
return state.nativeCurrency;
};
}
describe('useUserPreferencedCurrency', () => {
tests.forEach(({ params: { type, ...otherParams }, state, result }) => {
describe(`when showFiat is ${state.showFiat}, useNativeCurrencyAsPrimary is ${state.useNativeCurrencyAsPrimaryCurrency} and type is ${type}`, () => {
const stub = sinon.stub(reactRedux, 'useSelector');
stub.callsFake(getFakeUseSelector(state));
2020-11-03 00:41:28 +01:00
const { result: hookResult } = renderHook(() =>
useUserPreferencedCurrency(type, otherParams),
);
stub.restore();
2020-11-03 00:41:28 +01:00
it(`should return currency as ${
result.currency || 'not modified by user preferences'
}`, () => {
expect(hookResult.current.currency).toStrictEqual(result.currency);
});
2020-11-03 00:41:28 +01:00
it(`should return decimals as ${
result.numberOfDecimals || 'not modified by user preferences'
}`, () => {
expect(hookResult.current.numberOfDecimals).toStrictEqual(
2020-11-03 00:41:28 +01:00
result.numberOfDecimals,
);
});
});
});
});