Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add unit test for UserInputEventController #432

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions packages/starknet-snap/src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
BIP44CoinTypeNode,
getBIP44AddressKeyDeriver,
} from '@metamask/key-tree';
import type { UserInputEvent } from '@metamask/snaps-sdk';
import { UserInputEventType } from '@metamask/snaps-sdk';
import { generateMnemonic } from 'bip39';
import { getRandomValues } from 'crypto';
import type { constants, EstimateFee } from 'starknet';
Expand All @@ -17,6 +19,7 @@ import {
} from 'starknet';
import { v4 as uuidv4 } from 'uuid';

import { FeeToken } from '../types/snapApi';
import type {
AccContract,
Transaction,
Expand Down Expand Up @@ -83,7 +86,7 @@ export async function generateBip44Entropy(
* @returns An array of StarknetAccount object.
*/
export async function generateAccounts(
network: constants.StarknetChainId,
network: constants.StarknetChainId | string,
cnt: number = 1,
cairoVersion = '1',
mnemonic?: string,
Expand Down Expand Up @@ -298,7 +301,7 @@ export function generateTransactionRequests({
contractAddresses = PRELOADED_TOKENS.map((token) => token.address),
cnt = 1,
}: {
chainId: constants.StarknetChainId;
chainId: constants.StarknetChainId | string;
address: string;
contractAddresses?: string[];
cnt?: number;
Expand Down Expand Up @@ -359,12 +362,13 @@ export function generateTransactionRequests({

return requests;
}

/**
* Method to generate a mock estimate fee response.
*
* @returns An array containing a mock EstimateFee object.
*/
export function getEstimateFees() {
export function generateEstimateFeesResponse() {
return [
{
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -393,3 +397,36 @@ export function getEstimateFees() {
} as unknown as EstimateFee,
];
}

/**
* Method to generate a mock input event.
*
* @param params - The parameter for generate the mock input event.
* @param params.transactionRequest - The transaction request object.
* @param [params.eventValue] - The value of the event.
* @param [params.eventType] - The type of the event.
* @param [params.eventName] - The name of the event.
* @returns An array containing a mock input event object.
*/
export function generateInputEvent({
transactionRequest,
eventValue = FeeToken.ETH,
eventType = UserInputEventType.InputChangeEvent,
eventName = 'feeTokenSelector',
}: {
transactionRequest: TransactionRequest;
eventValue?: string;
eventType?: UserInputEventType;
eventName?: string;
}) {
return {
event: {
name: eventName,
type: eventType,
value: eventValue,
} as unknown as UserInputEvent,
context: {
request: transactionRequest,
},
};
}
22 changes: 0 additions & 22 deletions packages/starknet-snap/src/rpcs/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,13 @@ import type { constants } from 'starknet';

import type { StarknetAccount } from '../../__tests__/helper';
import { generateAccounts, generateRandomValue } from '../../__tests__/helper';
import { TransactionRequestStateManager } from '../../state/request-state-manager';
import type { SnapState } from '../../types/snapState';
import * as snapUiUtils from '../../ui/utils';
import { getExplorerUrl, shortenAddress, toJson } from '../../utils';
import * as snapHelper from '../../utils/snap';
import * as snapUtils from '../../utils/snapUtils';
import * as starknetUtils from '../../utils/starknetUtils';

export const mockTransactionRequestStateManager = () => {
const upsertTransactionRequestSpy = jest.spyOn(
TransactionRequestStateManager.prototype,
'upsertTransactionRequest',
);
const getTransactionRequestSpy = jest.spyOn(
TransactionRequestStateManager.prototype,
'getTransactionRequest',
);
const removeTransactionRequestSpy = jest.spyOn(
TransactionRequestStateManager.prototype,
'removeTransactionRequest',
);

return {
upsertTransactionRequestSpy,
getTransactionRequestSpy,
removeTransactionRequestSpy,
};
};

/**
*
* @param chainId
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/src/rpcs/estimate-fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Invocations } from 'starknet';
import { constants, TransactionType } from 'starknet';
import type { Infer } from 'superstruct';

import { getEstimateFees } from '../__tests__/helper';
import { generateEstimateFeesResponse } from '../__tests__/helper';
import { FeeTokenUnit } from '../types/snapApi';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import { InvalidRequestParamsError } from '../utils/exceptions';
Expand Down Expand Up @@ -45,7 +45,7 @@ const prepareMockEstimateFee = ({
details: { version },
} as unknown as EstimateFeeParams;

const estimateResults = getEstimateFees();
const estimateResults = generateEstimateFeesResponse();

const estimateBulkFeeRespMock = {
suggestedMaxFee: BigInt(1000000000000000).toString(10),
Expand Down
6 changes: 3 additions & 3 deletions packages/starknet-snap/src/rpcs/execute-txn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { UniversalDetails, Call, InvokeFunctionResponse } from 'starknet';
import { constants } from 'starknet';

import callsExamples from '../__tests__/fixture/callsExamples.json'; // Assuming you have a similar fixture
import { getEstimateFees } from '../__tests__/helper';
import { generateEstimateFeesResponse } from '../__tests__/helper';
import { mockTransactionRequestStateManager } from '../state/__tests__/helper';
import type { FeeTokenUnit } from '../types/snapApi';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import {
Expand All @@ -14,7 +15,6 @@ import { executeTxn as executeTxnUtil } from '../utils/starknetUtils';
import {
generateRandomFee,
mockAccount,
mockTransactionRequestStateManager,
prepareConfirmDialogInteractiveUI,
prepareMockAccount,
} from './__tests__/helper';
Expand Down Expand Up @@ -53,7 +53,7 @@ const prepareMockExecuteTxn = async (
transaction_hash: transactionHash,
};

const estimateResults = getEstimateFees();
const estimateResults = generateEstimateFeesResponse();

const getEstimatedFeesRepsMock = {
suggestedMaxFee: generateRandomFee('1000000000000000', '2000000000000000'),
Expand Down
51 changes: 51 additions & 0 deletions packages/starknet-snap/src/state/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type {
Transaction,
TransactionRequest,
} from '../../types/snapState';
import {
ETHER_SEPOLIA_TESTNET,
STRK_SEPOLIA_TESTNET,
} from '../../utils/constants';
import * as snapHelper from '../../utils/snap';
import { NetworkStateManager } from '../network-state-manager';
import { TransactionRequestStateManager } from '../request-state-manager';
import { TokenStateManager } from '../token-state-manager';

jest.mock('../../utils/snap');
jest.mock('../../utils/logger');
Expand Down Expand Up @@ -51,3 +58,47 @@ export const mockState = async ({
state,
};
};

export const mockTokenStateManager = () => {
const getEthTokenSpy = jest.spyOn(TokenStateManager.prototype, 'getEthToken');
const getStrkTokenSpy = jest.spyOn(
TokenStateManager.prototype,
'getStrkToken',
);
getStrkTokenSpy.mockResolvedValue(STRK_SEPOLIA_TESTNET);
getEthTokenSpy.mockResolvedValue(ETHER_SEPOLIA_TESTNET);

return {
getEthTokenSpy,
getStrkTokenSpy,
};
};

export const mockTransactionRequestStateManager = () => {
const upsertTransactionRequestSpy = jest.spyOn(
TransactionRequestStateManager.prototype,
'upsertTransactionRequest',
);
const getTransactionRequestSpy = jest.spyOn(
TransactionRequestStateManager.prototype,
'getTransactionRequest',
);
const removeTransactionRequestSpy = jest.spyOn(
TransactionRequestStateManager.prototype,
'removeTransactionRequest',
);

return {
upsertTransactionRequestSpy,
getTransactionRequestSpy,
removeTransactionRequestSpy,
};
};

export const mockNetworkStateManager = (network: Network) => {
const getNetworkSpy = jest.spyOn(NetworkStateManager.prototype, 'getNetwork');
getNetworkSpy.mockResolvedValue(network);
return {
getNetworkSpy,
};
};
Loading
Loading