Skip to content

Commit 17a39f6

Browse files
committed
test
1 parent d498edb commit 17a39f6

2 files changed

Lines changed: 78 additions & 87 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type {
2+
V1TransactionByHashResponse,
3+
V4MultiAccountTransactionsResponse,
4+
} from '@metamask/core-backend';
5+
import type { InfiniteData } from '@tanstack/react-query';
6+
import { selectTransactions } from './transformations';
7+
8+
describe('selectTransactions', () => {
9+
const address = '0x0000000000000000000000000000000000000001';
10+
const otherAddress = '0x0000000000000000000000000000000000000002';
11+
12+
const buildTransaction = (
13+
overrides: Partial<V1TransactionByHashResponse> = {},
14+
): V1TransactionByHashResponse =>
15+
({
16+
hash: '0xhash',
17+
timestamp: '2024-01-01T00:00:00Z',
18+
chainId: 1,
19+
blockNumber: 100,
20+
blockHash: '0xblock',
21+
gas: 21000,
22+
gasUsed: 21000,
23+
gasPrice: '1000000000',
24+
effectiveGasPrice: '1000000000',
25+
nonce: 0,
26+
cumulativeGasUsed: 21000,
27+
value: '1000',
28+
to: otherAddress,
29+
from: address,
30+
...overrides,
31+
} as unknown as V1TransactionByHashResponse);
32+
33+
const buildData = (
34+
transactions: V1TransactionByHashResponse[],
35+
): InfiniteData<V4MultiAccountTransactionsResponse> =>
36+
({
37+
pages: [{ data: transactions } as V4MultiAccountTransactionsResponse],
38+
pageParams: [undefined],
39+
} as InfiniteData<V4MultiAccountTransactionsResponse>);
40+
41+
it('transforms transactions into view models with id and transactionMeta', () => {
42+
const tx = buildTransaction();
43+
const result = selectTransactions({ address })(buildData([tx]));
44+
45+
expect(result.pages).toHaveLength(1);
46+
expect(result.pages[0].data).toHaveLength(1);
47+
const [viewModel] = result.pages[0].data;
48+
expect(viewModel.id).toBe('0xhash-1');
49+
expect(viewModel.hexChainId).toBe('0x1');
50+
expect(viewModel.transactionMeta).toBeDefined();
51+
expect(viewModel.hash).toBe('0xhash');
52+
});
53+
54+
it('filters out spam token transfers', () => {
55+
const spam = buildTransaction({
56+
hash: '0xspam',
57+
transactionType: 'SPAM_TOKEN_TRANSFER',
58+
} as Partial<V1TransactionByHashResponse>);
59+
const normal = buildTransaction({ hash: '0xnormal' });
60+
61+
const result = selectTransactions({ address })(buildData([spam, normal]));
62+
63+
expect(result.pages[0].data).toHaveLength(1);
64+
expect(result.pages[0].data[0].hash).toBe('0xnormal');
65+
});
66+
67+
it('filters out transactions unrelated to the address', () => {
68+
const unrelated = buildTransaction({
69+
hash: '0xunrelated',
70+
from: '0x0000000000000000000000000000000000000003',
71+
to: '0x0000000000000000000000000000000000000004',
72+
});
73+
74+
const result = selectTransactions({ address })(buildData([unrelated]));
75+
76+
expect(result.pages[0].data).toHaveLength(0);
77+
});
78+
});

tests/smoke/wallet/incoming-transactions.spec.ts

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -100,79 +100,6 @@ const RESPONSE_OUTGOING_TRANSACTION_MOCK = {
100100
from: DEFAULT_FIXTURE_ACCOUNT,
101101
};
102102

103-
const createInfuraRpcResponses = (
104-
rpcUrl = '',
105-
): Record<string, unknown> => {
106-
const chainId = rpcUrl.includes('linea-sepolia')
107-
? '0xe705'
108-
: rpcUrl.includes('sepolia')
109-
? '0xaa36a7'
110-
: '0x1';
111-
112-
return {
113-
eth_chainId: chainId,
114-
eth_getBalance: '0x0',
115-
eth_call: '0x',
116-
eth_estimateGas: '0x5208',
117-
eth_gasPrice: '0x3B9ACA00',
118-
eth_getTransactionCount: '0x0',
119-
eth_blockNumber: '0x1234567',
120-
eth_getBlockByNumber: {
121-
number: '0x1234567',
122-
hash: '0xabc123',
123-
timestamp: '0x' + Math.floor(Date.now() / 1000).toString(16),
124-
gasLimit: '0x1c9c380',
125-
gasUsed: '0x5208',
126-
baseFeePerGas: '0x3B9ACA00',
127-
transactions: [],
128-
},
129-
eth_maxPriorityFeePerGas: '0x3B9ACA00',
130-
eth_feeHistory: {
131-
baseFeePerGas: ['0x3B9ACA00', '0x3B9ACA00'],
132-
gasUsedRatio: [0.5],
133-
oldestBlock: '0x1234566',
134-
reward: [['0x3B9ACA00']],
135-
},
136-
net_version: parseInt(chainId, 16).toString(10),
137-
};
138-
};
139-
140-
const createMainnetRpcCallback =
141-
() =>
142-
async (request: {
143-
url: string;
144-
body: { getText: () => Promise<string | undefined> };
145-
}) => {
146-
const proxiedUrl = new URL(request.url, 'http://localhost').searchParams.get(
147-
'url',
148-
);
149-
const rpcResponses = createInfuraRpcResponses(proxiedUrl ?? request.url);
150-
const bodyText = await request.body.getText();
151-
const body = bodyText ? JSON.parse(bodyText) : undefined;
152-
153-
if (Array.isArray(body)) {
154-
return {
155-
statusCode: 200,
156-
body: JSON.stringify(
157-
body.map((req: { id?: number; method?: string }) => ({
158-
id: req.id ?? 1,
159-
jsonrpc: '2.0',
160-
result: rpcResponses[req.method ?? ''] ?? '0x',
161-
})),
162-
),
163-
};
164-
}
165-
166-
return {
167-
statusCode: 200,
168-
body: JSON.stringify({
169-
id: body?.id ?? 1,
170-
jsonrpc: '2.0',
171-
result: rpcResponses[body?.method ?? ''] ?? '0x',
172-
}),
173-
};
174-
};
175-
176103
function mockAccountsApi(
177104
transactions: Record<string, unknown>[] = [],
178105
): MockApiEndpoint {
@@ -197,20 +124,6 @@ function createAccountsTestSpecificMock(
197124
transactions: Record<string, unknown>[] = [],
198125
): TestSpecificMock {
199126
return async (mockServer: Mockttp) => {
200-
await mockServer
201-
.forPost(/^https:\/\/.*infura\.io\/v3\/.*$/)
202-
.asPriority(1000)
203-
.thenCallback(createMainnetRpcCallback());
204-
205-
await mockServer
206-
.forPost('/proxy')
207-
.matching((request) => {
208-
const url = new URL(request.url).searchParams.get('url');
209-
return Boolean(url?.includes('infura.io'));
210-
})
211-
.asPriority(1000)
212-
.thenCallback(createMainnetRpcCallback());
213-
214127
await setupRemoteFeatureFlagsMock(mockServer, {
215128
homepageRedesignV1: { enabled: false, minimumVersion: '0.0.0' },
216129
homepageSectionsV1: { enabled: false, minimumVersion: '0.0.0' },

0 commit comments

Comments
 (0)