-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b0cebe
commit 158aa51
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |