Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Update tests

Cleanup
  • Loading branch information
dan437 committed Nov 26, 2024
1 parent 17f09d3 commit af2d5e6
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 5 deletions.
169 changes: 167 additions & 2 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
} from './SmartTransactionsController';
import { advanceTime, flushPromises, getFakeProvider } from './test-helpers';
import type { SmartTransaction, UnsignedTransaction, Hex } from './types';
import { SmartTransactionStatuses } from './types';
import { SmartTransactionStatuses, ClientId } from './types';
import * as utils from './utils';
import packageJson from '../package.json';

Expand Down Expand Up @@ -1216,6 +1216,170 @@ describe('SmartTransactionsController', () => {
},
);
});

it('calls updateTransaction when smart transaction is cancelled and returnTxHashAsap is true', async () => {
const mockUpdateTransaction = jest.fn();
const defaultState = getDefaultSmartTransactionsControllerState();
const pendingStx = createStateAfterPending();
await withController(
{
options: {
updateTransaction: mockUpdateTransaction,
getFeatureFlags: () => ({
smartTransactions: {
mobileReturnTxHashAsap: true,
},
}),
getTransactions: () => [
{
id: 'test-tx-id',
status: TransactionStatus.submitted,
chainId: '0x1',
time: 123,
txParams: {
from: '0x123',
},
},
],
state: {
smartTransactionsState: {
...defaultState.smartTransactionsState,
smartTransactions: {
[ChainId.mainnet]: pendingStx as SmartTransaction[],
},
},
},
},
},
async ({ controller }) => {
const smartTransaction = {
uuid: 'uuid1',
status: SmartTransactionStatuses.CANCELLED,
transactionId: 'test-tx-id',
};

controller.updateSmartTransaction(smartTransaction);

expect(mockUpdateTransaction).toHaveBeenCalledWith(
{
id: 'test-tx-id',
status: TransactionStatus.failed,
chainId: '0x1',
time: 123,
txParams: {
from: '0x123',
},
},
'Smart transaction cancelled',
);
},
);
});

it('does not call updateTransaction when smart transaction is cancelled but returnTxHashAsap is false', async () => {
const mockUpdateTransaction = jest.fn();
await withController(
{
options: {
updateTransaction: mockUpdateTransaction,
getFeatureFlags: () => ({
smartTransactions: {
mobileReturnTxHashAsap: false,
},
}),
getTransactions: () => [
{
id: 'test-tx-id',
status: TransactionStatus.submitted,
chainId: '0x1',
time: 123,
txParams: {
from: '0x123',
},
},
],
},
},
async ({ controller }) => {
const smartTransaction = {
uuid: 'test-uuid',
status: SmartTransactionStatuses.CANCELLED,
transactionId: 'test-tx-id',
};

controller.updateSmartTransaction(smartTransaction);

expect(mockUpdateTransaction).not.toHaveBeenCalled();
},
);
});

it('does not call updateTransaction when transaction is not found in regular transactions', async () => {
const mockUpdateTransaction = jest.fn();

await withController(
{
options: {
updateTransaction: mockUpdateTransaction,
getFeatureFlags: () => ({
smartTransactions: {
mobileReturnTxHashAsap: true,
},
}),
getTransactions: () => [],
},
},
async ({ controller }) => {
const smartTransaction = {
uuid: 'test-uuid',
status: SmartTransactionStatuses.CANCELLED,
transactionId: 'test-tx-id',
};

controller.updateSmartTransaction(smartTransaction);

expect(mockUpdateTransaction).not.toHaveBeenCalled();
},
);
});

it('does not call updateTransaction for non-cancelled transactions', async () => {
const mockUpdateTransaction = jest.fn();
await withController(
{
options: {
updateTransaction: mockUpdateTransaction,
getFeatureFlags: () => ({
smartTransactions: {
mobileReturnTxHashAsap: true,
},
}),
getTransactions: () => [
{
id: 'test-tx-id',
status: TransactionStatus.submitted,
chainId: '0x1',
time: 123,
txParams: {
from: '0x123',
},
},
],
},
},
async ({ controller }) => {
const smartTransaction = {
uuid: 'test-uuid',
status: SmartTransactionStatuses.PENDING,
transactionId: 'test-tx-id',
};

controller.updateSmartTransaction(smartTransaction);

expect(mockUpdateTransaction).not.toHaveBeenCalled();
},
);
});
});

describe('cancelSmartTransaction', () => {
Expand Down Expand Up @@ -1440,7 +1604,7 @@ describe('SmartTransactionsController', () => {
const fetchHeaders = {
headers: {
'Content-Type': 'application/json',
'X-Client-Id': 'default',
'X-Client-Id': ClientId.Mobile,
},
};

Expand Down Expand Up @@ -1815,6 +1979,7 @@ async function withController<ReturnValue>(

const controller = new SmartTransactionsController({
messenger,
clientId: ClientId.Mobile,
getNonceLock: jest.fn().mockResolvedValue({
nextNonce: 'nextNonce',
releaseLock: jest.fn(),
Expand Down
33 changes: 33 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
APIType,
SmartTransactionStatuses,
SmartTransactionCancellationReason,
ClientId,
} from './types';
import * as utils from './utils';
import packageJson from '../package.json';
Expand Down Expand Up @@ -50,6 +51,20 @@ describe('src/utils.js', () => {
);
});

it('returns correct URL for ESTIMATE_GAS', () => {
const chainId = '0x1'; // Mainnet in hex
const expectedUrl = `${API_BASE_URL}/networks/1/estimateGas`;
const result = utils.getAPIRequestURL(APIType.ESTIMATE_GAS, chainId);
expect(result).toBe(expectedUrl);
});

it('converts hex chainId to decimal for ESTIMATE_GAS', () => {
const chainId = '0x89'; // Polygon in hex (137 in decimal)
const expectedUrl = `${API_BASE_URL}/networks/137/estimateGas`;
const result = utils.getAPIRequestURL(APIType.ESTIMATE_GAS, chainId);
expect(result).toBe(expectedUrl);
});

it('returns a URL for submitting transactions', () => {
expect(
utils.getAPIRequestURL(APIType.SUBMIT_TRANSACTIONS, ChainId.mainnet),
Expand Down Expand Up @@ -296,4 +311,22 @@ describe('src/utils.js', () => {
}).toThrow('kzg instance required to instantiate blob tx');
});
});

describe('getReturnTxHashAsap', () => {
it('returns extensionReturnTxHashAsap value for Extension client', () => {
const result = utils.getReturnTxHashAsap(ClientId.Extension, {
extensionReturnTxHashAsap: true,
mobileReturnTxHashAsap: false,
});
expect(result).toBe(true);
});

it('returns mobileReturnTxHashAsap value for Mobile client', () => {
const result = utils.getReturnTxHashAsap(ClientId.Mobile, {
extensionReturnTxHashAsap: false,
mobileReturnTxHashAsap: true,
});
expect(result).toBe(true);
});
});
});
3 changes: 0 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,6 @@ export const getReturnTxHashAsap = (
clientId: ClientId,
smartTransactionsFeatureFlags: FeatureFlags['smartTransactions'],
) => {
if (!clientId) {
return false;
}
return clientId === ClientId.Extension
? smartTransactionsFeatureFlags?.extensionReturnTxHashAsap
: smartTransactionsFeatureFlags?.mobileReturnTxHashAsap;
Expand Down

0 comments on commit af2d5e6

Please sign in to comment.