2022-01-06 03:47:26 +01:00
import React from 'react' ;
import { Provider } from 'react-redux' ;
import { renderHook } from '@testing-library/react-hooks' ;
import {
CUSTOM _GAS _ESTIMATE ,
EDIT _GAS _MODES ,
GAS _RECOMMENDATIONS ,
} from '../../../shared/constants/gas' ;
import mockState from '../../../test/data/mock-state.json' ;
import * as Actions from '../../store/actions' ;
import configureStore from '../../store/store' ;
import { useGasFeeEstimates } from '../useGasFeeEstimates' ;
import { FEE _MARKET _ESTIMATE _RETURN _VALUE } from './test-utils' ;
import { useTransactionFunctions } from './useTransactionFunctions' ;
jest . mock ( '../useGasFeeEstimates' , ( ) => ( {
useGasFeeEstimates : jest . fn ( ) ,
} ) ) ;
useGasFeeEstimates . mockImplementation ( ( ) => FEE _MARKET _ESTIMATE _RETURN _VALUE ) ;
jest . mock ( '../../selectors' , ( ) => ( {
checkNetworkAndAccountSupports1559 : ( ) => true ,
} ) ) ;
const wrapper = ( { children } ) => (
< Provider store = { configureStore ( mockState ) } > { children } < / P r o v i d e r >
) ;
const renderUseTransactionFunctions = ( props ) => {
return renderHook (
( ) =>
useTransactionFunctions ( {
defaultEstimateToUse : GAS _RECOMMENDATIONS . MEDIUM ,
editGasMode : EDIT _GAS _MODES . MODIFY _IN _PLACE ,
estimatedBaseFee : '0x59682f10' ,
gasFeeEstimates : FEE _MARKET _ESTIMATE _RETURN _VALUE . gasFeeEstimates ,
gasLimit : '21000' ,
maxPriorityFeePerGas : '0x59682f10' ,
transaction : {
userFeeLevel : CUSTOM _GAS _ESTIMATE ,
txParams : { maxFeePerGas : '0x5028' , maxPriorityFeePerGas : '0x5028' } ,
} ,
... props ,
} ) ,
{ wrapper } ,
) ;
} ;
describe ( 'useMaxPriorityFeePerGasInput' , ( ) => {
beforeEach ( ( ) => {
jest . clearAllMocks ( ) ;
} ) ;
it ( 'should invoke action createCancelTransaction when cancelTransaction callback is invoked' , ( ) => {
const mock = jest
. spyOn ( Actions , 'createCancelTransaction' )
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
const { result } = renderUseTransactionFunctions ( ) ;
result . current . cancelTransaction ( ) ;
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
it ( 'should invoke action createSpeedUpTransaction when speedUpTransaction callback is invoked' , ( ) => {
const mock = jest
. spyOn ( Actions , 'createSpeedUpTransaction' )
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
const { result } = renderUseTransactionFunctions ( ) ;
result . current . speedUpTransaction ( ) ;
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
2022-01-06 23:40:31 +01:00
it ( 'should invoke action updateTransaction with 10% increased fee when updateTransactionToTenPercentIncreasedGasFee callback is invoked' , ( ) => {
2022-01-06 03:47:26 +01:00
const mock = jest
. spyOn ( Actions , 'updateTransaction' )
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
const { result } = renderUseTransactionFunctions ( ) ;
2022-01-06 23:40:31 +01:00
result . current . updateTransactionToTenPercentIncreasedGasFee ( ) ;
2022-01-06 03:47:26 +01:00
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( mock ) . toHaveBeenCalledWith ( {
txParams : {
estimateSuggested : 'medium' ,
2022-01-06 23:40:31 +01:00
estimateUsed : 'tenPercentIncreased' ,
2022-01-06 03:47:26 +01:00
gas : '5208' ,
gasLimit : '5208' ,
maxFeePerGas : '0x582c' ,
maxPriorityFeePerGas : '0x582c' ,
} ,
2022-01-06 23:40:31 +01:00
userFeeLevel : 'tenPercentIncreased' ,
2022-01-06 03:47:26 +01:00
} ) ;
} ) ;
it ( 'should invoke action updateTransaction with estimate gas values fee when updateTransactionUsingEstimate callback is invoked' , ( ) => {
const mock = jest
. spyOn ( Actions , 'updateTransaction' )
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
const { result } = renderUseTransactionFunctions ( ) ;
result . current . updateTransactionUsingEstimate ( GAS _RECOMMENDATIONS . LOW ) ;
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( mock ) . toHaveBeenCalledWith ( {
txParams : {
estimateSuggested : 'medium' ,
estimateUsed : 'low' ,
gas : '5208' ,
gasLimit : '5208' ,
maxFeePerGas : 'c570bd200' ,
maxPriorityFeePerGas : 'b2d05e00' ,
} ,
userFeeLevel : 'low' ,
} ) ;
} ) ;
it ( 'should invoke action updateTransaction with dappSuggestedValues values fee when updateTransactionUsingDAPPSuggestedValues callback is invoked' , ( ) => {
const mock = jest
. spyOn ( Actions , 'updateTransaction' )
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
const { result } = renderUseTransactionFunctions ( {
transaction : {
userFeeLevel : CUSTOM _GAS _ESTIMATE ,
dappSuggestedGasFees : {
maxFeePerGas : '0x5028' ,
maxPriorityFeePerGas : '0x5028' ,
} ,
} ,
} ) ;
result . current . updateTransactionUsingDAPPSuggestedValues ( ) ;
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( mock ) . toHaveBeenCalledWith ( {
dappSuggestedGasFees : {
maxFeePerGas : '0x5028' ,
maxPriorityFeePerGas : '0x5028' ,
} ,
txParams : {
estimateSuggested : 'medium' ,
estimateUsed : 'dappSuggested' ,
gas : '5208' ,
gasLimit : '5208' ,
maxFeePerGas : '0x5028' ,
maxPriorityFeePerGas : '0x5028' ,
} ,
userFeeLevel : 'dappSuggested' ,
} ) ;
} ) ;
} ) ;