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
48 changes: 31 additions & 17 deletions packages/tron-wallet-snap/src/caching/useCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-shadow */
import type { Serializable } from '../utils/serialization/types';
import type { ICache } from './ICache';
import { useCache } from './useCache';
Expand Down Expand Up @@ -237,30 +237,44 @@ describe('useCache', () => {

describe('different argument types', () => {
it('should handle primitive arguments correctly', async () => {
jest.spyOn(cache, 'get').mockResolvedValue(undefined);
jest.spyOn(cache, 'set').mockResolvedValueOnce(undefined);
actualExecutionSpy.mockResolvedValueOnce('test with args');
await withUseCache(
async ({ actualExecutionSpy, cache, cachedTestFunctionWithArgs }) => {
cache.get.mockResolvedValue(undefined);
cache.set.mockResolvedValueOnce(undefined);
actualExecutionSpy.mockResolvedValueOnce('test with args');

const result = await cachedTestFunctionWithArgs('hello', 42);
const result = await cachedTestFunctionWithArgs('hello', 42);

expect(result).toBe('test with args');
expect(cache.get).toHaveBeenCalledWith('testFunctionWithArgs:"hello":42');
expect(actualExecutionSpy).toHaveBeenCalledWith('hello', 42);
expect(result).toBe('test with args');
expect(cache.get).toHaveBeenCalledWith(
'testFunctionWithArgs:"hello":42',
);
expect(actualExecutionSpy).toHaveBeenCalledWith('hello', 42);
},
);
});

it('should handle complex object arguments correctly', async () => {
jest.spyOn(cache, 'get').mockResolvedValue(undefined);
jest.spyOn(cache, 'set').mockResolvedValueOnce(undefined);
const testObj = { name: 'John', age: 30 };
actualExecutionSpy.mockResolvedValueOnce('test with complex args');
await withUseCache(
async ({
actualExecutionSpy,
cache,
cachedTestFunctionWithComplexArgs,
}) => {
cache.get.mockResolvedValue(undefined);
cache.set.mockResolvedValueOnce(undefined);
actualExecutionSpy.mockResolvedValueOnce('test with complex args');

const result = await cachedTestFunctionWithComplexArgs(testObj);
const testObj = { name: 'John', age: 30 };
const result = await cachedTestFunctionWithComplexArgs(testObj);

expect(result).toBe('test with complex args');
expect(cache.get).toHaveBeenCalledWith(
'testFunctionWithComplexArgs:{"name":"John","age":30}',
expect(result).toBe('test with complex args');
expect(cache.get).toHaveBeenCalledWith(
'testFunctionWithComplexArgs:{"name":"John","age":30}',
);
expect(actualExecutionSpy).toHaveBeenCalledWith(testObj);
},
);
expect(actualExecutionSpy).toHaveBeenCalledWith(testObj);
});
});

Expand Down