Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions packages/tron-wallet-snap/src/caching/useCache.test.ts
Original file line number Diff line number Diff line change
@@ -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<Promise<string>, Serializable[]>;
cache: ICache<Serializable>;
cachedTestFunction: () => Promise<string>;
cachedTestFunctionWithArgs: (arg1: string, arg2: number) => Promise<string>;
cachedTestFunctionWithComplexArgs: (obj: {
Expand All @@ -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<void> {
// Reset mocks for each test
const actualExecutionSpy = jest
Expand Down Expand Up @@ -70,6 +73,8 @@ async function withUseCache(testFn: WithUseCacheCallback): Promise<void> {
);

await testFn({
actualExecutionSpy,
cache,
cachedTestFunction,
cachedTestFunctionWithArgs,
cachedTestFunctionWithComplexArgs,
Expand Down Expand Up @@ -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);
},
);
});
});

Expand Down