Skip to content

Commit

Permalink
test: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Feb 22, 2024
1 parent 8b0cebe commit 158aa51
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/__mocks__/classic-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/naming-convention */

import { jest } from '@jest/globals';

const instance = {
get: jest.fn(async (): Promise<string> => Promise.resolve('get')),
put: jest.fn(async (): Promise<void> => Promise.resolve()),
};

export const mockClassicLevel = {
ClassicLevel: jest.fn(() => instance),
} as unknown as jest.Mocked<typeof import('classic-level')>;
58 changes: 58 additions & 0 deletions src/lib/cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/naming-convention */
import { jest } from '@jest/globals';

const { mockClassicLevel } = await import('./__mocks__/classic-level');
jest.unstable_mockModule('classic-level', () => mockClassicLevel);
const { ClassicLevel } = await import('classic-level');

const { cache } = await import('./cache');

describe('cache', () => {
// Mocked database instance for easier reference
const mockedDB = new ClassicLevel('');

beforeEach(() => {
// Clear mock call history before each test
jest.clearAllMocks();
});

it('retrieves a value from the cache if it exists', async () => {
const testKey = 'testKey';
const testValue = Buffer.from('testValue');

// @ts-expect-error too lazy
jest.mocked(mockedDB.get).mockReturnValue(Promise.resolve(testValue));

const result = await cache(testKey, async () => Promise.resolve(Buffer.from('newValue')));

expect(mockedDB.get).toHaveBeenCalledWith(testKey);
expect(result).toEqual(testValue);
expect(mockedDB.put).not.toHaveBeenCalled();
});

it('caches and returns a new value if the key does not exist in the cache', async () => {
const testKey = 'newKey';
const newValue = Buffer.from('newValue');
// Simulate a cache miss by throwing an error
// @ts-expect-error too lazy
jest.mocked(mockedDB.get).mockRejectedValue(new Error('Key not found'));

const result = await cache(testKey, async () => Promise.resolve(newValue));

expect(mockedDB.get).toHaveBeenCalledWith(testKey);
expect(mockedDB.put).toHaveBeenCalledWith(testKey, newValue);
expect(result).toEqual(newValue);
});

it('throws an error if the callback does not return a Buffer', async () => {
const testKey = 'invalidKey';

// Simulate a cache miss
// @ts-expect-error too lazy
jest.mocked(mockedDB.get).mockRejectedValue(new Error('Key not found'));

await expect(cache(testKey, async () => Promise.resolve('notABuffer' as unknown as Buffer)))
.rejects.toThrow('The callback function must return a Buffer');
});
});

0 comments on commit 158aa51

Please sign in to comment.