From 5e9f3376114168b396ceef6ae6912e12c70e7020 Mon Sep 17 00:00:00 2001 From: phani Date: Sat, 23 May 2020 15:55:53 +0100 Subject: [PATCH] increase test converage --- lib/redisCache.js | 6 +++--- lib/redisCache.test.js | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/redisCache.js b/lib/redisCache.js index 3a3b0f8..07273f3 100644 --- a/lib/redisCache.js +++ b/lib/redisCache.js @@ -8,7 +8,7 @@ const NotInitialisedError = require('./NotInitialisedError') let options = {} let store = null -const getNumber = num => (!Number.isNaN(num) && num >= 0 ? num : null) +const getNumber = num => (!Number.isNaN(num) && num >= 0 ? num : 0) /** * @param {object} options @@ -32,7 +32,7 @@ exports.init = ({ redisOptions, poolOptions, logger: createLogger(logger), - ttlInSeconds + ttlInSeconds: getNumber(ttlInSeconds) } store = new RedisStore(options) @@ -145,7 +145,7 @@ exports.deleteAll = (pattern = '*') => this.getStore().deleteAll(pattern) exports.wrap = async (key, fn, { ttlInSeconds } = {}) => { const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds - if (ttl && ttl === 0) { + if (ttl === 0) { debug(`Not caching, invalid ttl: ${ttlInSeconds}`) return fn() } diff --git a/lib/redisCache.test.js b/lib/redisCache.test.js index 8f6a194..b583e43 100644 --- a/lib/redisCache.test.js +++ b/lib/redisCache.test.js @@ -8,6 +8,7 @@ const { getPoolOptions, getRedisOptions, getset, + getStore, getTtlInSeconds, keys, set, @@ -61,6 +62,12 @@ describe('redisCache', () => { }) }) + describe('getStore', () => { + test('returns store', () => { + expect(getStore()).not.toBeNull() + }) + }) + describe('status', () => { test('get store stats', () => { const { name: statusName, size, available, pending } = status() @@ -118,6 +125,10 @@ describe('redisCache', () => { beforeAll(() => deleteAll()) + afterEach(() => { + setTtlInSeconds(ttlInSeconds) + }) + test("set if key doesn't exist", async () => { const localKey = genRandomStr() @@ -138,6 +149,7 @@ describe('redisCache', () => { test('do nothing when ttlInSeconds=0', async () => { const localKey = genRandomStr() + setTtlInSeconds(0) const result = await wrap(localKey, fn, { ttlInSeconds: 0 @@ -146,8 +158,9 @@ describe('redisCache', () => { expect(result).toBe(newValue) }) - test('do nothing when ttlInSeconds=0', async () => { + test('do nothing when ttlInSeconds < 0', async () => { const localKey = genRandomStr() + setTtlInSeconds(0) const result = await wrap(localKey, fn, { ttlInSeconds: -1 @@ -158,6 +171,7 @@ describe('redisCache', () => { test('do nothing when ttlInSeconds is invalid', async () => { const localKey = genRandomStr() + setTtlInSeconds('NOT_NUMBER') const result = await wrap(localKey, fn, { ttlInSeconds: 'NOT_NUMBER'