All files / test/InMemoryCache SharedTests.ts

95.91% Statements 47/49
100% Branches 0/0
89.47% Functions 17/19
97.67% Lines 42/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74    2x 2x 2x 2x 2x   2x   1x   6x   2x 8x   2x             2x 2x 14x     2x 2x 2x 2x 2x     2x 2x 2x   2x 2x 2x 2x     2x 2x 2x     2x 2x 2x     2x 2x 2x 2x     2x 2x 2x     2x 2x 2x        
import { InMemoryCache } from '../../src';
 
export const CURRENT_EPOCH_KEY = 'NetworkInfo_current_epoch';
export const TOTAL_STAKE_KEY = 'NetworkInfo_total_stake';
export const totalStakeCachedValue = 4_500_000_001;
export const currentEpoch = 250;
export const cacheMiss = undefined;
 
export const createNodeCacheMocked = (cachedValues: { [key: string]: unknown }) => ({
  close: jest.fn(),
  del: jest.fn(() => true),
  flushAll: jest.fn(),
  get: jest.fn((key) => cachedValues[key]),
  getVal: jest.fn((key) => cachedValues[key]),
  keys: jest.fn(() => [CURRENT_EPOCH_KEY, TOTAL_STAKE_KEY]),
  set: jest.fn(() => true)
});
export const sharedInMemoryCacheTests = (
  /* eslint-disable-next-line  @typescript-eslint/no-explicit-any */
  nodeCacheMocked: jest.MockedClass<any>,
  cachedValues: { [key: string]: unknown },
  cache: InMemoryCache,
  implName: string
) => {
  describe(`shared tests for ${implName}, with mocked node-cache`, () => {
    afterEach(async () => {
      jest.clearAllMocks();
    });
 
    it('get with cache hit', async () => {
      const response = await cache.get(TOTAL_STAKE_KEY, () => Promise.resolve());
      expect(response).toEqual(totalStakeCachedValue);
      expect(nodeCacheMocked.get).toBeCalled();
      expect(nodeCacheMocked.set).not.toBeCalled();
    });
 
    it('get with cache miss', async () => {
      const dbQueryTotalStakeResponse = '445566778899';
      nodeCacheMocked.get.mockImplementationOnce(() => cacheMiss);
 
      const response = await cache.get(TOTAL_STAKE_KEY, () => Promise.resolve(dbQueryTotalStakeResponse));
      expect(response).toEqual(dbQueryTotalStakeResponse);
      expect(nodeCacheMocked.get).toBeCalled();
      expect(nodeCacheMocked.set).toBeCalled();
    });
 
    it('set', async () => {
      expect(cache.set(CURRENT_EPOCH_KEY, currentEpoch, 120)).toEqual(true);
      expect(nodeCacheMocked.set).toBeCalled();
    });
 
    it('getVal', async () => {
      expect(cache.getVal(TOTAL_STAKE_KEY)).toEqual(cachedValues[TOTAL_STAKE_KEY]);
      expect(nodeCacheMocked.get).toBeCalled();
    });
 
    it('keys', async () => {
      const keys = cache.keys();
      expect(keys).toEqual([CURRENT_EPOCH_KEY, TOTAL_STAKE_KEY]);
      expect(nodeCacheMocked.keys).toBeCalled();
    });
 
    it('shutdown', async () => {
      cache.shutdown();
      expect(nodeCacheMocked.close).toBeCalled();
    });
 
    it('clear', async () => {
      cache.clear();
      expect(nodeCacheMocked.flushAll).toBeCalled();
    });
  });
};