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
46 changes: 28 additions & 18 deletions packages/tron-wallet-snap/src/caching/useCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,35 +193,45 @@ describe('useCache', () => {

describe('error handling', () => {
it('should propagate errors from the original function', async () => {
const error = new Error('Test error');
actualExecutionSpy.mockRejectedValueOnce(error);
await withUseCache(
async ({ actualExecutionSpy, cachedTestFunction, cache }) => {
const error = new Error('Test error');
actualExecutionSpy.mockRejectedValueOnce(error);

await expect(cachedTestFunction()).rejects.toThrow('Test error');
expect(cache.set).not.toHaveBeenCalled();
await expect(cachedTestFunction()).rejects.toThrow('Test error');
expect(cache.set).not.toHaveBeenCalled();
},
);
});

it('should handle cache get errors gracefully', async () => {
jest.spyOn(cache, 'get').mockRejectedValueOnce(new Error('Cache error'));
actualExecutionSpy.mockResolvedValueOnce('test');
await withUseCache(
async ({ actualExecutionSpy, cachedTestFunction, cache }) => {
cache.get.mockRejectedValueOnce(new Error('Cache error'));
actualExecutionSpy.mockResolvedValueOnce('test');

const result = await cachedTestFunction();
const result = await cachedTestFunction();

expect(result).toBe('test');
expect(actualExecutionSpy).toHaveBeenCalledTimes(1);
expect(cache.set).toHaveBeenCalledWith('testFunction:', 'test', 1000);
expect(result).toBe('test');
expect(actualExecutionSpy).toHaveBeenCalledTimes(1);
expect(cache.set).toHaveBeenCalledWith('testFunction:', 'test', 1000);
},
);
});

it('should handle cache set errors gracefully', async () => {
jest.spyOn(cache, 'get').mockResolvedValue(undefined);
jest
.spyOn(cache, 'set')
.mockRejectedValueOnce(new Error('Cache set error'));
actualExecutionSpy.mockResolvedValueOnce('test');
await withUseCache(
async ({ actualExecutionSpy, cachedTestFunction, cache }) => {
cache.get.mockResolvedValue(undefined);
cache.set.mockRejectedValueOnce(new Error('Cache set error'));
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);
},
);
});
});

Expand Down