|
| 1 | +import { SolMethod } from '@metamask/keyring-api'; |
| 2 | + |
| 3 | +import { transactionScanService, state } from '../../../../snapContext'; |
| 4 | +import { Network } from '../../../constants/solana'; |
| 5 | +import { serialize } from '../../../serialization/serialize'; |
| 6 | +import { trackError } from '../../../utils/errors'; |
| 7 | +import { getInterfaceContext, updateInterface } from '../../../utils/interface'; |
| 8 | +import { refreshConfirmationEstimation } from './refreshConfirmationEstimation'; |
| 9 | + |
| 10 | +jest.mock('../../../utils/errors', () => ({ |
| 11 | + trackError: jest.fn().mockResolvedValue('tracked-error-id'), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('../../../serialization/serialize', () => ({ |
| 15 | + serialize: jest.fn((value) => value), |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('../../../utils/interface', () => ({ |
| 19 | + CONFIRM_SIGN_AND_SEND_TRANSACTION_INTERFACE_NAME: 'confirmation-interface', |
| 20 | + getInterfaceContext: jest.fn(), |
| 21 | + updateInterface: jest.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +jest.mock( |
| 25 | + '../../../../features/confirmation/views/ConfirmTransactionRequest/ConfirmTransactionRequest', |
| 26 | + () => ({ |
| 27 | + ConfirmTransactionRequest: () => null, |
| 28 | + }), |
| 29 | +); |
| 30 | + |
| 31 | +jest.mock('../../../../snapContext', () => ({ |
| 32 | + state: { |
| 33 | + getKey: jest.fn(), |
| 34 | + }, |
| 35 | + transactionScanService: { |
| 36 | + scanTransaction: jest.fn(), |
| 37 | + }, |
| 38 | +})); |
| 39 | + |
| 40 | +const setupTest = () => { |
| 41 | + const interfaceContext = { |
| 42 | + account: { |
| 43 | + address: 'BLw3RweJmfbTapJRgnPRvd962YDjFYAnVGd1p5hmZ5tP', |
| 44 | + }, |
| 45 | + transaction: 'mock-transaction', |
| 46 | + scope: Network.Mainnet, |
| 47 | + method: SolMethod.SignAndSendTransaction, |
| 48 | + origin: 'https://metamask.io', |
| 49 | + preferences: { |
| 50 | + simulateOnChainActions: true, |
| 51 | + }, |
| 52 | + scanFetchStatus: 'fetched', |
| 53 | + }; |
| 54 | + |
| 55 | + (state.getKey as jest.Mock).mockResolvedValue({ |
| 56 | + 'confirmation-interface': 'interface-id', |
| 57 | + }); |
| 58 | + (getInterfaceContext as jest.Mock).mockResolvedValue(interfaceContext); |
| 59 | + (updateInterface as jest.Mock).mockResolvedValue(undefined); |
| 60 | + (serialize as jest.Mock).mockImplementation((value) => value); |
| 61 | +}; |
| 62 | + |
| 63 | +describe('refreshConfirmationEstimation', () => { |
| 64 | + it('tracks refresh failures and restores the fetched state', async () => { |
| 65 | + setupTest(); |
| 66 | + |
| 67 | + const error = new Error('Scan failed'); |
| 68 | + |
| 69 | + (transactionScanService.scanTransaction as jest.Mock).mockRejectedValue( |
| 70 | + error, |
| 71 | + ); |
| 72 | + |
| 73 | + await refreshConfirmationEstimation({ request: {} as any }); |
| 74 | + |
| 75 | + expect(trackError).toHaveBeenCalledWith(error); |
| 76 | + expect(updateInterface).toHaveBeenLastCalledWith( |
| 77 | + 'interface-id', |
| 78 | + null, |
| 79 | + expect.objectContaining({ |
| 80 | + scanFetchStatus: 'fetched', |
| 81 | + }), |
| 82 | + ); |
| 83 | + }); |
| 84 | +}); |
0 commit comments