diff --git a/packages/tron-wallet-snap/src/caching/useCache.test.ts b/packages/tron-wallet-snap/src/caching/useCache.test.ts index d026e6b5..c6ef774c 100644 --- a/packages/tron-wallet-snap/src/caching/useCache.test.ts +++ b/packages/tron-wallet-snap/src/caching/useCache.test.ts @@ -1,9 +1,12 @@ +/* eslint-disable @typescript-eslint/no-shadow */ import type { Serializable } from '../utils/serialization/types'; import type { ICache } from './ICache'; import { useCache } from './useCache'; import type { CacheOptions } from './useCache'; type WithUseCacheCallback = (payload: { + actualExecutionSpy: jest.Mock, Serializable[]>; + cache: ICache; cachedTestFunction: () => Promise; cachedTestFunctionWithArgs: (arg1: string, arg2: number) => Promise; cachedTestFunctionWithComplexArgs: (obj: { @@ -19,7 +22,7 @@ type WithUseCacheCallback = (payload: { * @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 const actualExecutionSpy = jest @@ -70,6 +73,8 @@ async function withUseCache(testFn: WithUseCacheCallback): Promise { ); await testFn({ + actualExecutionSpy, + cache, cachedTestFunction, cachedTestFunctionWithArgs, cachedTestFunctionWithComplexArgs, @@ -151,15 +156,19 @@ describe('useCache', () => { describe('when the data is not cached', () => { it('should cache the result of a function', async () => { - // No cached data - jest.spyOn(cache, 'get').mockResolvedValue(undefined); - - const result = await cachedTestFunction(); - - expect(result).toBe('test'); - expect(cache.get).toHaveBeenCalledTimes(1); - expect(actualExecutionSpy).toHaveBeenCalledTimes(1); - expect(cache.set).toHaveBeenCalledWith('testFunction:', 'test', 1000); + await withUseCache( + async ({ actualExecutionSpy, cache, cachedTestFunction }) => { + // No cached data + cache.get.mockResolvedValue(undefined); + + const result = await cachedTestFunction(); + + expect(result).toBe('test'); + expect(cache.get).toHaveBeenCalledTimes(1); + expect(actualExecutionSpy).toHaveBeenCalledTimes(1); + expect(cache.set).toHaveBeenCalledWith('testFunction:', 'test', 1000); + }, + ); }); });