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-03-25 18:11:04 +01:00
it ( 'should invoke action updateTransaction with 10% increased fee when updateTransactionToTenPercentIncreasedGasFee callback is invoked' , async ( ) => {
const mockUpdateGasFees = jest
. spyOn ( Actions , 'updateTransactionGasFees' )
2022-01-06 03:47:26 +01:00
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
2022-03-25 18:11:04 +01:00
2022-01-06 03:47:26 +01:00
const { result } = renderUseTransactionFunctions ( ) ;
2022-03-25 18:11:04 +01:00
await result . current . updateTransactionToTenPercentIncreasedGasFee ( ) ;
expect ( mockUpdateGasFees ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( mockUpdateGasFees ) . toHaveBeenCalledWith ( undefined , {
estimateSuggested : 'tenPercentIncreased' ,
estimateUsed : 'tenPercentIncreased' ,
gas : '5208' ,
gasLimit : '5208' ,
maxFeePerGas : '0x582c' ,
maxPriorityFeePerGas : '0x582c' ,
userEditedGasLimit : undefined ,
2022-01-06 23:40:31 +01:00
userFeeLevel : 'tenPercentIncreased' ,
2022-01-06 03:47:26 +01:00
} ) ;
} ) ;
2022-03-25 18:11:04 +01:00
it ( 'should invoke action updateTransaction with estimate gas values fee when updateTransactionUsingEstimate callback is invoked' , async ( ) => {
const mockUpdateGasFees = jest
. spyOn ( Actions , 'updateTransactionGasFees' )
2022-01-06 03:47:26 +01:00
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
2022-03-25 18:11:04 +01:00
2022-01-06 03:47:26 +01:00
const { result } = renderUseTransactionFunctions ( ) ;
2022-03-25 18:11:04 +01:00
await result . current . updateTransactionUsingEstimate (
GAS _RECOMMENDATIONS . LOW ,
) ;
expect ( mockUpdateGasFees ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( mockUpdateGasFees ) . toHaveBeenCalledWith ( undefined , {
estimateSuggested : 'medium' ,
estimateUsed : 'low' ,
gas : '5208' ,
gasLimit : '5208' ,
maxFeePerGas : 'c570bd200' ,
maxPriorityFeePerGas : 'b2d05e00' ,
userEditedGasLimit : undefined ,
2022-01-06 03:47:26 +01:00
userFeeLevel : 'low' ,
} ) ;
} ) ;
2022-03-25 18:11:04 +01:00
it ( 'should invoke action updateTransaction with dappSuggestedValues values fee when updateTransactionUsingDAPPSuggestedValues callback is invoked' , async ( ) => {
const mockUpdateGasFees = jest
. spyOn ( Actions , 'updateTransactionGasFees' )
2022-01-06 03:47:26 +01:00
. mockImplementation ( ( ) => ( { type : '' } ) ) ;
2022-03-25 18:11:04 +01:00
2022-01-06 03:47:26 +01:00
const { result } = renderUseTransactionFunctions ( {
transaction : {
userFeeLevel : CUSTOM _GAS _ESTIMATE ,
dappSuggestedGasFees : {
maxFeePerGas : '0x5028' ,
maxPriorityFeePerGas : '0x5028' ,
} ,
} ,
} ) ;
2022-03-25 18:11:04 +01:00
await result . current . updateTransactionUsingDAPPSuggestedValues ( ) ;
expect ( mockUpdateGasFees ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( mockUpdateGasFees ) . toHaveBeenCalledWith ( undefined , {
estimateSuggested : 'medium' ,
estimateUsed : 'dappSuggested' ,
gas : '5208' ,
gasLimit : '5208' ,
maxFeePerGas : '0x5028' ,
maxPriorityFeePerGas : '0x5028' ,
userEditedGasLimit : undefined ,
2022-01-06 03:47:26 +01:00
userFeeLevel : 'dappSuggested' ,
} ) ;
} ) ;
} ) ;