From 8822ece52eab80411682b2fbe438b937faad7b42 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Thu, 21 Nov 2024 14:10:09 +0100 Subject: [PATCH] fix: Disable confirm button if `transactionMeta` is undefined (#12273) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** This PR adds disabled button condition when `transactionMeta` is undefined. ## **Related issues** Fixes: https://github.com/MetaMask/metamask-mobile/issues/11537 ## **Manual testing steps** N/A ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [X] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- .../confirmations/SendFlow/Confirm/index.js | 27 +++++++++++++++---- .../SendFlow/Confirm/index.test.tsx | 23 ++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/components/Views/confirmations/SendFlow/Confirm/index.js b/app/components/Views/confirmations/SendFlow/Confirm/index.js index 4aae1abb75f..a8b8d3641a2 100644 --- a/app/components/Views/confirmations/SendFlow/Confirm/index.js +++ b/app/components/Views/confirmations/SendFlow/Confirm/index.js @@ -12,6 +12,7 @@ import { connect } from 'react-redux'; import { getSendFlowTitle } from '../../../../UI/Navbar'; import PropTypes from 'prop-types'; import Eth from '@metamask/ethjs-query'; +import { isEmpty } from 'lodash'; import { renderFromWei, renderFromTokenMinimalUnit, @@ -486,11 +487,25 @@ class Confirm extends PureComponent { const { TransactionController } = Engine.context; const transactionParams = this.prepareTransactionToSend(); - const { result, transactionMeta } = - await TransactionController.addTransaction(transactionParams, { - deviceConfirmedOn: WalletDevice.MM_MOBILE, - origin: TransactionTypes.MMM, - }); + let result, transactionMeta; + try { + ({ result, transactionMeta } = await TransactionController.addTransaction( + transactionParams, + { + deviceConfirmedOn: WalletDevice.MM_MOBILE, + origin: TransactionTypes.MMM, + }, + )); + } catch (error) { + Logger.error(error, 'error while adding transaction (Confirm)'); + navigation?.dangerouslyGetParent()?.pop(); + Alert.alert( + strings('transactions.transaction_error'), + error && error.message, + [{ text: 'OK' }], + ); + return; + } setTransactionId(transactionMeta.id); @@ -1305,6 +1320,7 @@ class Confirm extends PureComponent { EIP1559GasObject, EIP1559GasTransaction, legacyGasObject, + transactionMeta, } = this.state; const colors = this.context.colors || mockTheme.colors; const styles = createStyles(colors); @@ -1477,6 +1493,7 @@ class Confirm extends PureComponent { { ); }); }); + + it('should show error if transaction is not added', async () => { + jest.spyOn(Alert, 'alert'); + + Engine.context.TransactionController.addTransaction = jest + .fn() + .mockRejectedValue(new Error('Transaction not added')); + + render(Confirm); + + await flushPromises(); + + await waitFor(() => { + expect(Alert.alert).toHaveBeenCalledWith( + 'Transaction error', + 'Transaction not added', + expect.any(Array), + ); + }); + }); });