@@ -31,7 +31,7 @@ import type {
3131 SmartTransactionsControllerEvents ,
3232} from './SmartTransactionsController' ;
3333import type { SmartTransaction , UnsignedTransaction , Hex } from './types' ;
34- import { SmartTransactionStatuses } from './types' ;
34+ import { SmartTransactionStatuses , ClientId } from './types' ;
3535import * as utils from './utils' ;
3636
3737jest . mock ( '@ethersproject/bytes' , ( ) => ( {
@@ -1214,6 +1214,170 @@ describe('SmartTransactionsController', () => {
12141214 } ,
12151215 ) ;
12161216 } ) ;
1217+
1218+ it ( 'calls updateTransaction when smart transaction is cancelled and returnTxHashAsap is true' , async ( ) => {
1219+ const mockUpdateTransaction = jest . fn ( ) ;
1220+ const defaultState = getDefaultSmartTransactionsControllerState ( ) ;
1221+ const pendingStx = createStateAfterPending ( ) ;
1222+ await withController (
1223+ {
1224+ options : {
1225+ updateTransaction : mockUpdateTransaction ,
1226+ getFeatureFlags : ( ) => ( {
1227+ smartTransactions : {
1228+ mobileReturnTxHashAsap : true ,
1229+ } ,
1230+ } ) ,
1231+ getTransactions : ( ) => [
1232+ {
1233+ id : 'test-tx-id' ,
1234+ status : TransactionStatus . submitted ,
1235+ chainId : '0x1' ,
1236+ time : 123 ,
1237+ txParams : {
1238+ from : '0x123' ,
1239+ } ,
1240+ } ,
1241+ ] ,
1242+ state : {
1243+ smartTransactionsState : {
1244+ ...defaultState . smartTransactionsState ,
1245+ smartTransactions : {
1246+ [ ChainId . mainnet ] : pendingStx as SmartTransaction [ ] ,
1247+ } ,
1248+ } ,
1249+ } ,
1250+ } ,
1251+ } ,
1252+ async ( { controller } ) => {
1253+ const smartTransaction = {
1254+ uuid : 'uuid1' ,
1255+ status : SmartTransactionStatuses . CANCELLED ,
1256+ transactionId : 'test-tx-id' ,
1257+ } ;
1258+
1259+ controller . updateSmartTransaction ( smartTransaction ) ;
1260+
1261+ expect ( mockUpdateTransaction ) . toHaveBeenCalledWith (
1262+ {
1263+ id : 'test-tx-id' ,
1264+ status : TransactionStatus . failed ,
1265+ chainId : '0x1' ,
1266+ time : 123 ,
1267+ txParams : {
1268+ from : '0x123' ,
1269+ } ,
1270+ } ,
1271+ 'Smart transaction cancelled' ,
1272+ ) ;
1273+ } ,
1274+ ) ;
1275+ } ) ;
1276+
1277+ it ( 'does not call updateTransaction when smart transaction is cancelled but returnTxHashAsap is false' , async ( ) => {
1278+ const mockUpdateTransaction = jest . fn ( ) ;
1279+ await withController (
1280+ {
1281+ options : {
1282+ updateTransaction : mockUpdateTransaction ,
1283+ getFeatureFlags : ( ) => ( {
1284+ smartTransactions : {
1285+ mobileReturnTxHashAsap : false ,
1286+ } ,
1287+ } ) ,
1288+ getTransactions : ( ) => [
1289+ {
1290+ id : 'test-tx-id' ,
1291+ status : TransactionStatus . submitted ,
1292+ chainId : '0x1' ,
1293+ time : 123 ,
1294+ txParams : {
1295+ from : '0x123' ,
1296+ } ,
1297+ } ,
1298+ ] ,
1299+ } ,
1300+ } ,
1301+ async ( { controller } ) => {
1302+ const smartTransaction = {
1303+ uuid : 'test-uuid' ,
1304+ status : SmartTransactionStatuses . CANCELLED ,
1305+ transactionId : 'test-tx-id' ,
1306+ } ;
1307+
1308+ controller . updateSmartTransaction ( smartTransaction ) ;
1309+
1310+ expect ( mockUpdateTransaction ) . not . toHaveBeenCalled ( ) ;
1311+ } ,
1312+ ) ;
1313+ } ) ;
1314+
1315+ it ( 'does not call updateTransaction when transaction is not found in regular transactions' , async ( ) => {
1316+ const mockUpdateTransaction = jest . fn ( ) ;
1317+
1318+ await withController (
1319+ {
1320+ options : {
1321+ updateTransaction : mockUpdateTransaction ,
1322+ getFeatureFlags : ( ) => ( {
1323+ smartTransactions : {
1324+ mobileReturnTxHashAsap : true ,
1325+ } ,
1326+ } ) ,
1327+ getTransactions : ( ) => [ ] ,
1328+ } ,
1329+ } ,
1330+ async ( { controller } ) => {
1331+ const smartTransaction = {
1332+ uuid : 'test-uuid' ,
1333+ status : SmartTransactionStatuses . CANCELLED ,
1334+ transactionId : 'test-tx-id' ,
1335+ } ;
1336+
1337+ controller . updateSmartTransaction ( smartTransaction ) ;
1338+
1339+ expect ( mockUpdateTransaction ) . not . toHaveBeenCalled ( ) ;
1340+ } ,
1341+ ) ;
1342+ } ) ;
1343+
1344+ it ( 'does not call updateTransaction for non-cancelled transactions' , async ( ) => {
1345+ const mockUpdateTransaction = jest . fn ( ) ;
1346+ await withController (
1347+ {
1348+ options : {
1349+ updateTransaction : mockUpdateTransaction ,
1350+ getFeatureFlags : ( ) => ( {
1351+ smartTransactions : {
1352+ mobileReturnTxHashAsap : true ,
1353+ } ,
1354+ } ) ,
1355+ getTransactions : ( ) => [
1356+ {
1357+ id : 'test-tx-id' ,
1358+ status : TransactionStatus . submitted ,
1359+ chainId : '0x1' ,
1360+ time : 123 ,
1361+ txParams : {
1362+ from : '0x123' ,
1363+ } ,
1364+ } ,
1365+ ] ,
1366+ } ,
1367+ } ,
1368+ async ( { controller } ) => {
1369+ const smartTransaction = {
1370+ uuid : 'test-uuid' ,
1371+ status : SmartTransactionStatuses . PENDING ,
1372+ transactionId : 'test-tx-id' ,
1373+ } ;
1374+
1375+ controller . updateSmartTransaction ( smartTransaction ) ;
1376+
1377+ expect ( mockUpdateTransaction ) . not . toHaveBeenCalled ( ) ;
1378+ } ,
1379+ ) ;
1380+ } ) ;
12171381 } ) ;
12181382
12191383 describe ( 'cancelSmartTransaction' , ( ) => {
@@ -1438,7 +1602,7 @@ describe('SmartTransactionsController', () => {
14381602 const fetchHeaders = {
14391603 headers : {
14401604 'Content-Type' : 'application/json' ,
1441- 'X-Client-Id' : 'default' ,
1605+ 'X-Client-Id' : ClientId . Mobile ,
14421606 } ,
14431607 } ;
14441608
@@ -1813,6 +1977,7 @@ async function withController<ReturnValue>(
18131977
18141978 const controller = new SmartTransactionsController ( {
18151979 messenger,
1980+ clientId : ClientId . Mobile ,
18161981 getNonceLock : jest . fn ( ) . mockResolvedValue ( {
18171982 nextNonce : 'nextNonce' ,
18181983 releaseLock : jest . fn ( ) ,
@@ -1827,6 +1992,8 @@ async function withController<ReturnValue>(
18271992 deviceModel : 'ledger' ,
18281993 } ) ;
18291994 } ) ,
1995+ getFeatureFlags : jest . fn ( ) ,
1996+ updateTransaction : jest . fn ( ) ,
18301997 ...options ,
18311998 } ) ;
18321999
0 commit comments