Skip to content

Commit

Permalink
increase test converage
Browse files Browse the repository at this point in the history
  • Loading branch information
pasupulaphani committed May 23, 2020
1 parent 25f87e8 commit 5e9f337
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/redisCache.js
Expand Up @@ -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
Expand All @@ -32,7 +32,7 @@ exports.init = ({
redisOptions,
poolOptions,
logger: createLogger(logger),
ttlInSeconds
ttlInSeconds: getNumber(ttlInSeconds)
}

store = new RedisStore(options)
Expand Down Expand Up @@ -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()
}
Expand Down
16 changes: 15 additions & 1 deletion lib/redisCache.test.js
Expand Up @@ -8,6 +8,7 @@ const {
getPoolOptions,
getRedisOptions,
getset,
getStore,
getTtlInSeconds,
keys,
set,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -118,6 +125,10 @@ describe('redisCache', () => {

beforeAll(() => deleteAll())

afterEach(() => {
setTtlInSeconds(ttlInSeconds)
})

test("set if key doesn't exist", async () => {
const localKey = genRandomStr()

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'
Expand Down

0 comments on commit 5e9f337

Please sign in to comment.