From c1921e035822c47e5953daf1d310c12b87864c82 Mon Sep 17 00:00:00 2001 From: gabrieledm Date: Mon, 24 Aug 2026 17:22:32 +0200 Subject: [PATCH 1/2] test: added shape of setup function and its arguments --- .../src/caching/useCache.test.ts | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/packages/tron-wallet-snap/src/caching/useCache.test.ts b/packages/tron-wallet-snap/src/caching/useCache.test.ts index 456f8a2b9..9a9b2ee9c 100644 --- a/packages/tron-wallet-snap/src/caching/useCache.test.ts +++ b/packages/tron-wallet-snap/src/caching/useCache.test.ts @@ -3,6 +3,72 @@ import type { ICache } from './ICache'; import { useCache } from './useCache'; import type { CacheOptions } from './useCache'; +type WithUseCacheCallback = (payload: { + cachedTestFunction: () => Promise; + cachedTestFunctionWithArgs: (arg1: string, arg2: number) => Promise; + cachedTestFunctionWithComplexArgs: (obj: { + name: string; + age: number; + }) => Promise; +}) => void | Promise; + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +async function withUseCache(testFn: WithUseCacheCallback): Promise { + // Reset mocks for each test + const actualExecutionSpy = jest + .fn, Serializable[]>() + .mockResolvedValue('test'); + + // Create a mock cache + const cache = { + get: jest.fn().mockResolvedValue(undefined), + set: jest.fn().mockResolvedValue(undefined), + } as unknown as ICache; + + // Define common cache options + const cacheOptions = { + ttlMilliseconds: 1000, + functionName: 'testFunction', + }; + + // Define original functions + const testFunction = async (): Promise => actualExecutionSpy(); + const testFunctionWithArgs = async ( + arg1: string, + arg2: number, + ): Promise => actualExecutionSpy(arg1, arg2); + const testFunctionWithComplexArgs = async (obj: { + name: string; + age: number; + }): Promise => actualExecutionSpy(obj); + + // Create cached versions + const cachedTestFunction = useCache(testFunction, cache, { + ...cacheOptions, + functionName: 'testFunction', + }); + + const cachedTestFunctionWithArgs = useCache(testFunctionWithArgs, cache, { + ...cacheOptions, + functionName: 'testFunctionWithArgs', + }); + + const cachedTestFunctionWithComplexArgs = useCache( + testFunctionWithComplexArgs, + cache, + { + ...cacheOptions, + functionName: 'testFunctionWithComplexArgs', + }, + ); + + await testFn({ + cachedTestFunction, + cachedTestFunctionWithArgs, + cachedTestFunctionWithComplexArgs, + }); +} + describe('useCache', () => { // Spy to check if the original function was executed or not let actualExecutionSpy: jest.Mock; From 75760e9e9b5c19494dee5b3d085b6fff086ca3e5 Mon Sep 17 00:00:00 2001 From: gabrieledm Date: Wed, 26 Aug 2026 14:41:04 +0200 Subject: [PATCH 2/2] doc: added JSDoc for withUseCache setup function --- packages/tron-wallet-snap/src/caching/useCache.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/tron-wallet-snap/src/caching/useCache.test.ts b/packages/tron-wallet-snap/src/caching/useCache.test.ts index 9a9b2ee9c..d026e6b58 100644 --- a/packages/tron-wallet-snap/src/caching/useCache.test.ts +++ b/packages/tron-wallet-snap/src/caching/useCache.test.ts @@ -12,6 +12,13 @@ type WithUseCacheCallback = (payload: { }) => Promise; }) => void | Promise; +/** + * Wraps tests for `useCache` by creating fresh cached functions backed by a + * mock cache. + * + * @param testFn - The test body receiving the cached functions. + * @returns A promise that resolves when the test function completes. + */ // eslint-disable-next-line @typescript-eslint/no-unused-vars async function withUseCache(testFn: WithUseCacheCallback): Promise { // Reset mocks for each test