Skip to content

Commit 308115d

Browse files
cursoragentn3ps
andcommitted
fix: mock account api activity in e2e
Co-authored-by: Francis Nepomuceno <n3ps@users.noreply.github.com>
1 parent 1d5f911 commit 308115d

6 files changed

Lines changed: 72 additions & 17 deletions

File tree

app/components/Views/UnifiedTransactionsView/UnifiedTransactionsView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ const UnifiedTransactionsView = ({
188188
return false;
189189
}
190190

191-
const { chainId: _chainId, txParams, hash } = tx;
191+
const { chainId: _chainId, txParams } = tx;
192+
const hash = 'hash' in tx ? tx.hash : undefined;
192193
const { from, nonce, actionId } = txParams || {};
193194
// Some txs don't have nonce, like intent based swaps
194195
const hasNonce = nonce !== undefined && nonce !== null;

app/components/Views/UnifiedTransactionsView/helpers/mappers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import { NATIVE_TOKEN_ADDRESS } from '../../confirmations/constants/tokens';
1212
import type { ConfirmedEvmTransaction } from './types';
1313

1414
export interface ActivityHistoryQueryParams {
15-
accountAddresses: string[];
16-
networks: CaipChainId[];
15+
accountAddresses: readonly string[];
16+
networks: readonly CaipChainId[];
1717
}
1818

1919
const EXCLUDED_TRANSACTION_TYPES = ['SPAM_TOKEN_TRANSFER'];
2020

2121
// The API returns CAIP-10 account IDs, but the filtering logic only needs the
2222
// plain wallet address when deciding whether a tx belongs to the active account.
23-
const getPlainAddresses = (accountAddresses: string[]) =>
23+
const getPlainAddresses = (accountAddresses: readonly string[]) =>
2424
[
2525
...new Set(
2626
accountAddresses
@@ -206,7 +206,7 @@ export const normalizeTransactions = (
206206
// rules first, then adapts the surviving rows into TransactionMeta-like items
207207
// for the existing mobile Activity renderers.
208208
export const selectConfirmedTransactions =
209-
({ accountAddresses }: { accountAddresses: string[] }) =>
209+
({ accountAddresses }: { accountAddresses: readonly string[] }) =>
210210
(data: InfiniteData<V4MultiAccountTransactionsResponse>) => {
211211
const plainAddresses = new Set(getPlainAddresses(accountAddresses));
212212

app/components/Views/UnifiedTransactionsView/hooks/useTransactionsQuery.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
2+
InfiniteData,
23
QueryFunctionContext,
4+
QueryFunction,
35
QueryKey,
46
useInfiniteQuery,
57
} from '@tanstack/react-query';
@@ -11,43 +13,69 @@ import { selectAccountGroupEvmAccountAddresses } from '../../../../selectors/mul
1113
import { selectEvmEnabledCaipNetworks } from '../../../../selectors/networkEnablementController';
1214
import { selectConfirmedTransactions } from '../helpers/mappers';
1315
import { MINUTE } from '../../../../constants/time';
16+
import type { ConfirmedEvmTransaction } from '../helpers/types';
17+
18+
type ConfirmedEvmTransactionsPage = Omit<
19+
V4MultiAccountTransactionsResponse,
20+
'data'
21+
> & {
22+
data: ConfirmedEvmTransaction[];
23+
};
1424

1525
export const useTransactionsQuery = () => {
1626
const accountAddresses = useSelector(selectAccountGroupEvmAccountAddresses);
1727
const networks = useSelector(selectEvmEnabledCaipNetworks);
1828
const selectFn = useMemo(
1929
() =>
2030
selectConfirmedTransactions({
21-
accountAddresses,
31+
accountAddresses: [...accountAddresses],
2232
}),
2333
[accountAddresses],
2434
);
2535

26-
const queryOptions = useMemo(
27-
() =>
36+
const queryOptions = useMemo(() => {
37+
const options =
2838
getApiClient().accounts.getV4MultiAccountTransactionsInfiniteQueryOptions(
2939
{
3040
accountAddresses: [...accountAddresses],
3141
networks: [...networks],
3242
includeTxMetadata: true,
3343
},
34-
),
35-
[accountAddresses, networks],
36-
);
44+
);
45+
46+
return {
47+
queryKey: options.queryKey as QueryKey,
48+
queryFn: options.queryFn as QueryFunction<
49+
V4MultiAccountTransactionsResponse,
50+
QueryKey,
51+
string | undefined
52+
>,
53+
};
54+
}, [accountAddresses, networks]);
3755

3856
return useInfiniteQuery<
3957
V4MultiAccountTransactionsResponse,
4058
Error,
41-
ReturnType<ReturnType<typeof selectConfirmedTransactions>>,
59+
ConfirmedEvmTransactionsPage,
4260
QueryKey
4361
>({
44-
queryKey: queryOptions.queryKey as QueryKey,
45-
queryFn: ({
62+
queryKey: queryOptions.queryKey,
63+
queryFn: (({
4664
pageParam,
4765
signal,
4866
}: QueryFunctionContext<QueryKey, string | undefined>) =>
49-
queryOptions.queryFn({ pageParam, signal }),
50-
getNextPageParam: queryOptions.getNextPageParam,
67+
queryOptions.queryFn({
68+
pageParam,
69+
signal,
70+
queryKey: queryOptions.queryKey,
71+
meta: undefined,
72+
})) as QueryFunction<
73+
V4MultiAccountTransactionsResponse,
74+
QueryKey,
75+
string | undefined
76+
>,
77+
getNextPageParam: ({ pageInfo }) =>
78+
pageInfo.hasNextPage ? pageInfo.endCursor : undefined,
5179
select: selectFn,
5280
enabled: accountAddresses.length > 0 && networks.length > 0,
5381
staleTime: 5 * MINUTE,

app/selectors/multichainAccounts/accountTreeController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ export const selectAccountGroupEvmInternalAccounts = createSelector(
429429
export const selectAccountGroupEvmAccountAddresses = createSelector(
430430
[selectSelectedInternalAccount, selectAccountGroupEvmInternalAccounts],
431431
(selectedInternalAccount, groupEvmAccounts) => {
432-
const selectedEvmAccount = isEvmAccountType(selectedInternalAccount?.type)
432+
const selectedEvmAccount =
433+
selectedInternalAccount &&
434+
isEvmAccountType(selectedInternalAccount.type)
433435
? selectedInternalAccount
434436
: (groupEvmAccounts.find(
435437
(account) =>

tests/api-mocking/MockServerE2E.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
findMatchingPostEvent,
1515
processPostRequestBody,
1616
setupAccountsV2SupportedNetworksMock,
17+
setupAccountsV4TransactionsMock,
1718
} from './helpers/mockHelpers.ts';
1819
import { getLocalHost } from '../framework/fixtures/FixtureUtils.ts';
1920
import PortManager, { ResourceType } from '../framework/PortManager.ts';
@@ -298,6 +299,7 @@ export default class MockServerE2E implements Resource {
298299
}
299300

300301
await setupAccountsV2SupportedNetworksMock(this._server);
302+
await setupAccountsV4TransactionsMock(this._server);
301303

302304
await this._server
303305
.forAnyRequest()

tests/api-mocking/helpers/mockHelpers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,28 @@ export async function setupAccountsV2SupportedNetworksMock(
463463
});
464464
}
465465

466+
/**
467+
* Registers a default empty history response for Accounts API v4 transactions.
468+
* Tests that need activity rows can override this with a test-specific mock.
469+
*/
470+
export async function setupAccountsV4TransactionsMock(
471+
server: Mockttp,
472+
): Promise<void> {
473+
await setupMockRequest(server, {
474+
requestMethod: 'GET',
475+
url: /^https:\/\/accounts\.api\.cx\.metamask\.io\/v4\/multiaccount\/transactions(\?.*)?$/,
476+
response: {
477+
data: [],
478+
unprocessedNetworks: [],
479+
pageInfo: {
480+
count: 0,
481+
hasNextPage: false,
482+
},
483+
},
484+
responseCode: 200,
485+
});
486+
}
487+
466488
export interface SeenProxiedRequest {
467489
method: string;
468490
proxiedUrl: string;

0 commit comments

Comments
 (0)