diff --git a/eslint-suppressions.json b/eslint-suppressions.json index 06d37eb0..f31d7f0e 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -1641,11 +1641,6 @@ "count": 1 } }, - "packages/tron-wallet-snap/src/caching/useCache.test.ts": { - "@typescript-eslint/explicit-function-return-type": { - "count": 4 - } - }, "packages/tron-wallet-snap/src/caching/useCacheUntil.test.ts": { "@typescript-eslint/explicit-function-return-type": { "count": 2 diff --git a/packages/tron-wallet-snap/src/caching/useCache.test.ts b/packages/tron-wallet-snap/src/caching/useCache.test.ts index a0efd1f5..59d1be2d 100644 --- a/packages/tron-wallet-snap/src/caching/useCache.test.ts +++ b/packages/tron-wallet-snap/src/caching/useCache.test.ts @@ -1,11 +1,9 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ import type { Serializable } from '../utils/serialization/types'; import type { ICache } from './ICache'; -import { useCache } from './useCache'; -import type { CacheOptions } from './useCache'; +import { CacheOptions, useCache } from './useCache'; // Define common cache options -const cacheOptions = { +const cacheOptions: CacheOptions = { ttlMilliseconds: 1000, functionName: 'testFunction', }; @@ -83,78 +81,6 @@ async function withUseCache(testFn: WithUseCacheCallback): Promise { } describe('useCache', () => { - // Spy to check if the original function was executed or not - let actualExecutionSpy: jest.Mock; - - // Mock cache - let cache: ICache; - - // Common cache options - let cacheOptions: CacheOptions; - - // Original test functions - let testFunction: () => Promise; - let testFunctionWithArgs: (arg1: string, arg2: number) => Promise; - let testFunctionWithComplexArgs: (obj: { - name: string; - age: number; - }) => Promise; - - // Cached versions - let cachedTestFunction: () => Promise; - let cachedTestFunctionWithArgs: ( - arg1: string, - arg2: number, - ) => Promise; - let cachedTestFunctionWithComplexArgs: (obj: { - name: string; - age: number; - }) => Promise; - - beforeEach(() => { - // Reset mocks for each test - actualExecutionSpy = jest.fn().mockResolvedValue('test'); - - // Create a mock cache - cache = { - get: jest.fn().mockResolvedValue(undefined), - set: jest.fn().mockResolvedValue(undefined), - } as unknown as ICache; - - // Define common cache options - cacheOptions = { - ttlMilliseconds: 1000, - functionName: 'testFunction', - }; - - // Define original functions - testFunction = async () => actualExecutionSpy(); - testFunctionWithArgs = async (arg1: string, arg2: number) => - actualExecutionSpy(arg1, arg2); - testFunctionWithComplexArgs = async (obj: { name: string; age: number }) => - actualExecutionSpy(obj); - - // Create cached versions - cachedTestFunction = useCache(testFunction, cache, { - ...cacheOptions, - functionName: 'testFunction', - }); - - cachedTestFunctionWithArgs = useCache(testFunctionWithArgs, cache, { - ...cacheOptions, - functionName: 'testFunctionWithArgs', - }); - - cachedTestFunctionWithComplexArgs = useCache( - testFunctionWithComplexArgs, - cache, - { - ...cacheOptions, - functionName: 'testFunctionWithComplexArgs', - }, - ); - }); - describe('when the data is not cached', () => { it('should cache the result of a function', async () => { await withUseCache( @@ -333,33 +259,41 @@ describe('useCache', () => { describe('falsy but valid cache values', () => { it('should handle falsy but valid cache values (false, 0, empty string)', async () => { - // Test with false - jest.spyOn(cache, 'get').mockResolvedValue(false); - let result = await cachedTestFunction(); - expect(result).toBe(false); - expect(actualExecutionSpy).not.toHaveBeenCalled(); - - // Test with 0 - jest.spyOn(cache, 'get').mockResolvedValue(0); - result = await cachedTestFunction(); - expect(result).toBe(0); - expect(actualExecutionSpy).not.toHaveBeenCalled(); - - // Test with empty string - jest.spyOn(cache, 'get').mockResolvedValue(''); - result = await cachedTestFunction(); - expect(result).toBe(''); - expect(actualExecutionSpy).not.toHaveBeenCalled(); + await withUseCache( + async ({ actualExecutionSpy, cache, cachedTestFunction }) => { + // Test with false + cache.get.mockResolvedValue(false); + let result = await cachedTestFunction(); + expect(result).toBe(false); + expect(actualExecutionSpy).not.toHaveBeenCalled(); + + // Test with 0 + cache.get.mockResolvedValue(0); + result = await cachedTestFunction(); + expect(result).toBe(0); + expect(actualExecutionSpy).not.toHaveBeenCalled(); + + // Test with empty string + cache.get.mockResolvedValue(''); + result = await cachedTestFunction(); + expect(result).toBe(''); + expect(actualExecutionSpy).not.toHaveBeenCalled(); + }, + ); }); it('should execute the function when cache returns undefined', async () => { - jest.spyOn(cache, 'get').mockResolvedValue(undefined); - actualExecutionSpy.mockResolvedValueOnce('test'); + await withUseCache( + async ({ actualExecutionSpy, cache, cachedTestFunction }) => { + cache.get.mockResolvedValue(undefined); + actualExecutionSpy.mockResolvedValueOnce('test'); - const result = await cachedTestFunction(); + const result = await cachedTestFunction(); - expect(result).toBe('test'); - expect(actualExecutionSpy).toHaveBeenCalledTimes(1); + expect(result).toBe('test'); + expect(actualExecutionSpy).toHaveBeenCalledTimes(1); + }, + ); }); }); });