2021-02-04 19:15:23 +01:00
import assert from 'assert' ;
import { renderHook } from '@testing-library/react-hooks' ;
import * as reactRedux from 'react-redux' ;
import sinon from 'sinon' ;
2021-03-16 22:00:08 +01:00
import { getPreferences , getShouldShowFiat } from '../selectors' ;
import { useUserPreferencedCurrency } from './useUserPreferencedCurrency' ;
2020-05-12 21:07:35 +02:00
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 ,
} ,
} ,
2021-02-04 19:15:23 +01:00
] ;
2020-05-12 21:07:35 +02:00
2020-11-03 00:41:28 +01:00
function getFakeUseSelector ( state ) {
2020-05-12 21:07:35 +02:00
return ( selector ) => {
2020-05-26 08:11:58 +02:00
if ( selector === getPreferences ) {
2021-02-04 19:15:23 +01:00
return state ;
2020-05-12 21:07:35 +02:00
} else if ( selector === getShouldShowFiat ) {
2021-02-04 19:15:23 +01:00
return state . showFiat ;
2020-05-12 21:07:35 +02:00
}
2021-02-04 19:15:23 +01:00
return state . nativeCurrency ;
} ;
2020-05-12 21:07:35 +02:00
}
describe ( 'useUserPreferencedCurrency' , function ( ) {
tests . forEach ( ( { params : { type , ... otherParams } , state , result } ) => {
describe ( ` when showFiat is ${ state . showFiat } , useNativeCurrencyAsPrimary is ${ state . useNativeCurrencyAsPrimaryCurrency } and type is ${ type } ` , function ( ) {
2021-02-04 19:15:23 +01:00
const stub = sinon . stub ( reactRedux , 'useSelector' ) ;
stub . callsFake ( getFakeUseSelector ( state ) ) ;
2020-05-12 21:07:35 +02:00
2020-11-03 00:41:28 +01:00
const { result : hookResult } = renderHook ( ( ) =>
useUserPreferencedCurrency ( type , otherParams ) ,
2021-02-04 19:15:23 +01:00
) ;
stub . restore ( ) ;
2020-11-03 00:41:28 +01:00
it ( ` should return currency as ${
result . currency || 'not modified by user preferences'
} ` , function () {
2021-02-04 19:15:23 +01:00
assert . strictEqual ( hookResult . current . currency , result . currency ) ;
} ) ;
2020-11-03 00:41:28 +01:00
it ( ` should return decimals as ${
result . numberOfDecimals || 'not modified by user preferences'
} ` , function () {
2020-12-03 16:46:22 +01:00
assert . strictEqual (
2020-11-03 00:41:28 +01:00
hookResult . current . numberOfDecimals ,
result . numberOfDecimals ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
} ) ;
} ) ;