Skip to content

Commit 3412e16

Browse files
test: finish unit tests for onAccountsRefresh
1 parent dc92041 commit 3412e16

1 file changed

Lines changed: 130 additions & 189 deletions

File tree

packages/snap/src/core/handlers/onCronjob/backgroundEvents/onAccountsRefresh.test.ts

Lines changed: 130 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
MOCK_SOLANA_KEYRING_ACCOUNT_0,
1313
MOCK_SOLANA_KEYRING_ACCOUNT_1,
1414
} from '../../../test/mocks/solana-keyring-accounts';
15-
import type { ILogger } from '../../../utils/logger';
16-
import logger from '../../../utils/logger';
1715
import type { SolanaKeyring } from '../../onKeyringRequest/Keyring';
1816
import { onAccountsRefresh } from './onAccountsRefresh';
1917

@@ -43,7 +41,6 @@ describe('onAccountsRefresh', () => {
4341
snapContext.assetsService as jest.Mocked<AssetsService>;
4442
const mockTransactionsService =
4543
snapContext.transactionsService as jest.Mocked<TransactionsService>;
46-
const mockLogger = logger as jest.Mocked<ILogger>;
4744

4845
beforeEach(() => {
4946
jest.clearAllMocks();
@@ -125,16 +122,6 @@ describe('onAccountsRefresh', () => {
125122
expect(mockTransactionsService.refreshTransactions).toHaveBeenCalledWith([
126123
accounts[0],
127124
]);
128-
129-
expect(mockLogger.info).toHaveBeenCalledWith(
130-
'[onAccountsRefresh] Cronjob triggered',
131-
);
132-
expect(mockLogger.info).toHaveBeenCalledWith(
133-
'[onAccountsRefresh] Found 1 accounts with changes',
134-
);
135-
expect(mockLogger.info).toHaveBeenCalledWith(
136-
'[onAccountsRefresh] Successfully refreshed 1 accounts',
137-
);
138125
});
139126

140127
it('handles accounts with no existing signatures', async () => {
@@ -169,180 +156,134 @@ describe('onAccountsRefresh', () => {
169156
});
170157
});
171158

172-
// describe('when no accounts have changes', () => {
173-
// it('should skip refresh when all accounts have no new signatures', async () => {
174-
// // Arrange
175-
// const accounts = [
176-
// MOCK_SOLANA_KEYRING_ACCOUNT_0,
177-
// MOCK_SOLANA_KEYRING_ACCOUNT_1,
178-
// ];
179-
// const existingSignatures: Signature[] = [
180-
// 'signature1',
181-
// 'signature2',
182-
// ] as Signature[];
183-
184-
// mockKeyring.listAccounts.mockResolvedValue(accounts);
185-
// mockState.getKey.mockResolvedValue(existingSignatures);
186-
187-
// // Return existing signatures (no changes)
188-
// mockTransactionsService.fetchLatestSignatures.mockResolvedValue([
189-
// 'signature1',
190-
// ] as Signature[]);
191-
192-
// const request = {
193-
// id: '1',
194-
// jsonrpc: '2.0' as const,
195-
// method: 'onCronjob',
196-
// params: {},
197-
// };
198-
199-
// await onAccountsRefresh({ request });
200-
201-
// // Assert
202-
// expect(mockAssetsService.refreshAssets).not.toHaveBeenCalled();
203-
// expect(
204-
// mockTransactionsService.refreshTransactions,
205-
// ).not.toHaveBeenCalled();
206-
// expect(mockLogger.info).toHaveBeenCalledWith(
207-
// '[onAccountsRefresh] No accounts with changes, skipping refresh',
208-
// );
209-
// });
210-
211-
// it('should skip refresh when no latest signatures are found', async () => {
212-
// // Arrange
213-
// const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
214-
215-
// mockKeyring.listAccounts.mockResolvedValue(accounts);
216-
// mockState.getKey.mockResolvedValue([]);
217-
// mockTransactionsService.fetchLatestSignatures.mockResolvedValue([]); // No signatures found
218-
219-
// const request = {
220-
// id: '1',
221-
// jsonrpc: '2.0' as const,
222-
// method: 'onCronjob',
223-
// params: {},
224-
// };
225-
226-
// await onAccountsRefresh({ request });
227-
228-
// // Assert
229-
// expect(mockAssetsService.refreshAssets).not.toHaveBeenCalled();
230-
// expect(
231-
// mockTransactionsService.refreshTransactions,
232-
// ).not.toHaveBeenCalled();
233-
// expect(mockLogger.info).toHaveBeenCalledWith(
234-
// '[onAccountsRefresh] No accounts with changes, skipping refresh',
235-
// );
236-
// });
237-
// });
238-
239-
// describe('when no accounts exist', () => {
240-
// it('should handle empty accounts list gracefully', async () => {
241-
// // Arrange
242-
// mockKeyring.listAccounts.mockResolvedValue([]);
243-
244-
// // Act
245-
// await onAccountsRefresh();
246-
247-
// // Assert
248-
// expect(mockState.getKey).not.toHaveBeenCalled();
249-
// expect(
250-
// mockTransactionsService.fetchLatestSignatures,
251-
// ).not.toHaveBeenCalled();
252-
// expect(mockAssetsService.refreshAssets).not.toHaveBeenCalled();
253-
// expect(
254-
// mockTransactionsService.refreshTransactions,
255-
// ).not.toHaveBeenCalled();
256-
// expect(mockLogger.info).toHaveBeenCalledWith(
257-
// '[onAccountsRefresh] No accounts with changes, skipping refresh',
258-
// );
259-
// });
260-
// });
261-
262-
// describe('error handling', () => {
263-
// it('should handle errors during assets refresh gracefully', async () => {
264-
// // Arrange
265-
// const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
266-
// const assetsError = new Error('Assets service failed');
267-
268-
// mockKeyring.listAccounts.mockResolvedValue(accounts);
269-
// mockState.getKey.mockResolvedValue([]);
270-
// mockTransactionsService.fetchLatestSignatures.mockResolvedValue([
271-
// 'newSig',
272-
// ] as Signature[]);
273-
// mockAssetsService.refreshAssets.mockRejectedValue(assetsError);
274-
// mockTransactionsService.refreshTransactions.mockResolvedValue();
275-
276-
// // Act
277-
// await onAccountsRefresh();
278-
279-
// // Assert
280-
// expect(mockLogger.warn).toHaveBeenCalledWith(
281-
// '[onAccountsRefresh] Caught error while refreshing assets',
282-
// assetsError,
283-
// );
284-
// expect(mockTransactionsService.refreshTransactions).toHaveBeenCalled(); // Should still continue
285-
// });
286-
287-
// it('should handle errors during transactions refresh gracefully', async () => {
288-
// // Arrange
289-
// const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
290-
// const transactionsError = new Error('Transactions service failed');
291-
292-
// mockKeyring.listAccounts.mockResolvedValue(accounts);
293-
// mockState.getKey.mockResolvedValue([]);
294-
// mockTransactionsService.fetchLatestSignatures.mockResolvedValue([
295-
// 'newSig',
296-
// ] as Signature[]);
297-
// mockAssetsService.refreshAssets.mockResolvedValue();
298-
// mockTransactionsService.refreshTransactions.mockRejectedValue(
299-
// transactionsError,
300-
// );
301-
302-
// // Act
303-
// await onAccountsRefresh();
304-
305-
// // Assert
306-
// expect(mockLogger.warn).toHaveBeenCalledWith(
307-
// '[onAccountsRefresh] Caught error while refreshing transactions',
308-
// transactionsError,
309-
// );
310-
// });
311-
312-
// it('should handle critical errors and log them', async () => {
313-
// // Arrange
314-
// const criticalError = new Error('Critical failure');
315-
// mockKeyring.listAccounts.mockRejectedValue(criticalError);
316-
317-
// // Act
318-
// await onAccountsRefresh();
319-
320-
// // Assert
321-
// expect(mockLogger.error).toHaveBeenCalledWith(
322-
// '[onAccountsRefresh] Error',
323-
// criticalError,
324-
// );
325-
// });
326-
327-
// it('should handle errors during signature fetching', async () => {
328-
// // Arrange
329-
// const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
330-
// const signatureError = new Error('Signature fetch failed');
331-
332-
// mockKeyring.listAccounts.mockResolvedValue(accounts);
333-
// mockState.getKey.mockResolvedValue([]);
334-
// mockTransactionsService.fetchLatestSignatures.mockRejectedValue(
335-
// signatureError,
336-
// );
337-
338-
// // Act
339-
// await onAccountsRefresh();
340-
341-
// // Assert
342-
// expect(mockLogger.error).toHaveBeenCalledWith(
343-
// '[onAccountsRefresh] Error',
344-
// signatureError,
345-
// );
346-
// });
347-
// });
159+
describe('when no accounts have changes', () => {
160+
it('skips refresh when all accounts have no new signatures', async () => {
161+
const accounts = [
162+
MOCK_SOLANA_KEYRING_ACCOUNT_0,
163+
MOCK_SOLANA_KEYRING_ACCOUNT_1,
164+
];
165+
const existingSignatures: Signature[] = [
166+
'signature1',
167+
'signature2',
168+
] as Signature[];
169+
170+
mockKeyring.listAccounts.mockResolvedValue(accounts);
171+
mockState.getKey.mockResolvedValue(existingSignatures);
172+
173+
// Return existing signatures (no changes)
174+
mockTransactionsService.fetchLatestSignatures.mockResolvedValue([
175+
'signature1',
176+
] as Signature[]);
177+
178+
const request = {
179+
id: '1',
180+
jsonrpc: '2.0' as const,
181+
method: 'onCronjob',
182+
params: {},
183+
};
184+
185+
await onAccountsRefresh({ request });
186+
187+
expect(mockAssetsService.refreshAssets).not.toHaveBeenCalled();
188+
expect(
189+
mockTransactionsService.refreshTransactions,
190+
).not.toHaveBeenCalled();
191+
});
192+
193+
it('skips refresh when no latest signatures are found', async () => {
194+
const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
195+
196+
mockKeyring.listAccounts.mockResolvedValue(accounts);
197+
mockState.getKey.mockResolvedValue([]);
198+
mockTransactionsService.fetchLatestSignatures.mockResolvedValue([]); // No signatures found
199+
200+
const request = {
201+
id: '1',
202+
jsonrpc: '2.0' as const,
203+
method: 'onCronjob',
204+
params: {},
205+
};
206+
207+
await onAccountsRefresh({ request });
208+
209+
expect(mockAssetsService.refreshAssets).not.toHaveBeenCalled();
210+
expect(
211+
mockTransactionsService.refreshTransactions,
212+
).not.toHaveBeenCalled();
213+
});
214+
});
215+
216+
describe('when no accounts exist', () => {
217+
it('handles empty accounts list gracefully', async () => {
218+
mockKeyring.listAccounts.mockResolvedValue([]);
219+
220+
const request = {
221+
id: '1',
222+
jsonrpc: '2.0' as const,
223+
method: 'onCronjob',
224+
params: {},
225+
};
226+
227+
await onAccountsRefresh({ request });
228+
229+
expect(mockState.getKey).not.toHaveBeenCalled();
230+
expect(
231+
mockTransactionsService.fetchLatestSignatures,
232+
).not.toHaveBeenCalled();
233+
expect(mockAssetsService.refreshAssets).not.toHaveBeenCalled();
234+
expect(
235+
mockTransactionsService.refreshTransactions,
236+
).not.toHaveBeenCalled();
237+
});
238+
});
239+
240+
describe('error handling', () => {
241+
it('handles errors during assets refresh gracefully', async () => {
242+
const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
243+
const assetsError = new Error('Assets service failed');
244+
mockKeyring.listAccounts.mockResolvedValue(accounts);
245+
mockState.getKey.mockResolvedValue([]);
246+
mockTransactionsService.fetchLatestSignatures.mockResolvedValue([
247+
'newSig',
248+
] as Signature[]);
249+
mockAssetsService.refreshAssets.mockRejectedValue(assetsError);
250+
mockTransactionsService.refreshTransactions.mockResolvedValue();
251+
252+
const request = {
253+
id: '1',
254+
jsonrpc: '2.0' as const,
255+
method: 'onCronjob',
256+
params: {},
257+
};
258+
259+
await onAccountsRefresh({ request });
260+
261+
expect(mockTransactionsService.refreshTransactions).toHaveBeenCalled(); // Should still continue
262+
});
263+
264+
it('handles errors during transactions refresh gracefully', async () => {
265+
const accounts = [MOCK_SOLANA_KEYRING_ACCOUNT_0];
266+
const transactionsError = new Error('Transactions service failed');
267+
mockKeyring.listAccounts.mockResolvedValue(accounts);
268+
mockState.getKey.mockResolvedValue([]);
269+
mockTransactionsService.fetchLatestSignatures.mockResolvedValue([
270+
'newSig',
271+
] as Signature[]);
272+
mockAssetsService.refreshAssets.mockResolvedValue();
273+
mockTransactionsService.refreshTransactions.mockRejectedValue(
274+
transactionsError,
275+
);
276+
277+
const request = {
278+
id: '1',
279+
jsonrpc: '2.0' as const,
280+
method: 'onCronjob',
281+
params: {},
282+
};
283+
284+
await onAccountsRefresh({ request });
285+
286+
expect(mockAssetsService.refreshAssets).toHaveBeenCalled(); // Should still continue
287+
});
288+
});
348289
});

0 commit comments

Comments
 (0)