@@ -11,6 +11,7 @@ import { updateSelectedGasFeeToken } from '../../../store/controller-actions/tra
1111import { Alert } from '../../../ducks/confirm-alerts/confirm-alerts' ;
1212import { forceUpdateMetamaskState } from '../../../store/actions' ;
1313import { GAS_FEE_TOKEN_MOCK } from '../../../../test/data/confirmations/gas' ;
14+ import * as actionConstants from '../../../store/actionConstants' ;
1415import { useInsufficientBalanceAlerts } from './alerts/transactions/useInsufficientBalanceAlerts' ;
1516import { useAutomaticGasFeeTokenSelect } from './useAutomaticGasFeeTokenSelect' ;
1617import { useIsGaslessSupported } from './gas/useIsGaslessSupported' ;
@@ -53,6 +54,35 @@ async function flushAsyncUpdates() {
5354 } ) ;
5455}
5556
57+ type ConfirmHookRenderResult = ReturnType <
58+ typeof renderHookWithConfirmContextProvider
59+ > ;
60+
61+ function updateTransactionInStore (
62+ store : ConfirmHookRenderResult [ 'store' ] ,
63+ transactionId : string ,
64+ updates : Partial < TransactionMeta > ,
65+ ) {
66+ if ( ! store ) {
67+ throw new Error ( 'Expected store to be defined' ) ;
68+ }
69+
70+ act ( ( ) => {
71+ const currentTransactions = store
72+ . getState ( )
73+ . metamask . transactions . map ( ( transaction : TransactionMeta ) =>
74+ transaction . id === transactionId
75+ ? { ...transaction , ...updates }
76+ : transaction ,
77+ ) ;
78+
79+ store . dispatch ( {
80+ type : actionConstants . UPDATE_METAMASK_STATE ,
81+ value : { transactions : currentTransactions } ,
82+ } ) ;
83+ } ) ;
84+ }
85+
5686describe ( 'useAutomaticGasFeeTokenSelect' , ( ) => {
5787 const updateSelectedGasFeeTokenMock = jest . mocked ( updateSelectedGasFeeToken ) ;
5888 const forceUpdateMetamaskStateMock = jest . mocked ( forceUpdateMetamaskState ) ;
@@ -111,7 +141,7 @@ describe('useAutomaticGasFeeTokenSelect', () => {
111141 } ) ;
112142
113143 it ( 'selects first gas fee token on rerender when selection becomes eligible' , async ( ) => {
114- const { rerender, state , store } = runHook ( {
144+ const { rerender, store } = runHook ( {
115145 selectedGasFeeToken : GAS_FEE_TOKEN_MOCK . tokenAddress ,
116146 } ) ;
117147
@@ -124,11 +154,11 @@ describe('useAutomaticGasFeeTokenSelect', () => {
124154 expect ( updateSelectedGasFeeTokenMock ) . toHaveBeenCalledTimes ( 0 ) ;
125155 expect ( forceUpdateMetamaskStateMock ) . toHaveBeenCalledTimes ( 0 ) ;
126156
127- const transactionMeta = state . metamask
128- . transactions [ 0 ] as unknown as TransactionMeta ;
157+ const transactionMeta = store . getState ( ) . metamask
158+ . transactions [ 0 ] as TransactionMeta ;
129159
130- act ( ( ) => {
131- transactionMeta . selectedGasFeeToken = undefined ;
160+ updateTransactionInStore ( store , transactionMeta . id , {
161+ selectedGasFeeToken : undefined ,
132162 } ) ;
133163
134164 rerender ( ) ;
@@ -202,7 +232,7 @@ describe('useAutomaticGasFeeTokenSelect', () => {
202232 } ) ;
203233
204234 it ( 'does not select first gas fee token after firstCheck is set to false' , async ( ) => {
205- const { rerender, state , store } = runHook ( ) ;
235+ const { rerender, store } = runHook ( ) ;
206236
207237 if ( ! store ) {
208238 throw new Error ( 'Expected store to be defined' ) ;
@@ -211,10 +241,10 @@ describe('useAutomaticGasFeeTokenSelect', () => {
211241 await flushAsyncUpdates ( ) ;
212242
213243 // Simulate a rerender with new state that would otherwise trigger selection
214- act ( ( ) => {
215- (
216- state . metamask . transactions [ 0 ] as unknown as TransactionMeta
217- ) . selectedGasFeeToken = undefined ;
244+ const transactionMeta = store . getState ( ) . metamask
245+ . transactions [ 0 ] as TransactionMeta ;
246+ updateTransactionInStore ( store , transactionMeta . id , {
247+ selectedGasFeeToken : undefined ,
218248 } ) ;
219249
220250 rerender ( ) ;
0 commit comments