Skip to content

Commit

Permalink
🧪 updated unit tests for valueOnly flag + minor fix setM() response
Browse files Browse the repository at this point in the history
  • Loading branch information
akashchouhan16 committed Nov 15, 2023
1 parent fa8ac13 commit bd3d0b5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
76 changes: 76 additions & 0 deletions test/nodecache.config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,82 @@ describe("NodeCache params for instance config", () => {
expect(cache.cache[6]).toBeUndefined()
})
})
describe("NodeCache valueOnly configurations: Default (true) case", () => {
let cache
beforeEach(() => {
cache = new NodeCache({
// valueOnly: true // By default valueOnly is set to true -> backward compatibility with older npm versions.
})
})
afterEach(() => {
cache.close()
})

test("When not valueOnly flag with the instance", () => {
expect(cache.config["valueOnly"]).toEqual(true)
})

test("When using Get() and Set() calls - true case", () => {
cache.set(151123, { _id: "nC11Hque81", value: "test-data" })
expect(cache.get(151123)).toStrictEqual({ _id: "nC11Hque81", value: "test-data" })

cache.set(1511232, { _id: "nC11Hque82", value: "test-data-2" }, 60 * 1000)
expect(cache.get(1511232)).toStrictEqual({ _id: "nC11Hque82", value: "test-data-2" })
})

test("When using getM() and setM() calls - true case", () => {
const input = [{ key: 1511235, value: "test-data-5" }, { key: 1511236, value: "test-data-6", ttl: 2 * 60 * 1000 }]
cache.setM(input)

const responses = cache.getM([1511235, 1511236])
responses.forEach(response => {
expect(response).toStrictEqual(expect.any(String))
})
})
})

describe("NodeCache valueOnly configurations: New (false) case", () => {
let cache
beforeEach(() => {
cache = new NodeCache({
valueOnly: false
})
})
afterEach(() => {
cache.close()
})

test("When not valueOnly flag with the instance", () => {
expect(cache.config["valueOnly"]).toEqual(false)
})

test("When using get() and set() calls - false case", () => {
cache.set(1511233, { _id: "nC11Hque83", value: "test-data-3" })
expect(cache.get(1511233)).toStrictEqual({
value: expect.any(Object),
ttl: expect.any(Number)
})

cache.set(1511234, { _id: "nC11Hque84", value: "test-data-4" }, 60 * 1000)
expect(cache.get(1511234)).toStrictEqual({
value: expect.any(Object),
ttl: expect.any(Number)
})
})

test("When using getM() and setM() calls - false case", () => {
const input = [{ key: 1511235, value: "test-data-5" }, { key: 1511236, value: "test-data-6", ttl: 2 * 60 * 1000 }]
cache.setM(input)

const responses = cache.getM([1511235, 1511236])
responses.forEach(response => {
expect(response).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
})
})
})

describe("NodeCache Logger configurations for the instance (type: custom)", () => {
let cache
Expand Down
2 changes: 1 addition & 1 deletion test/nodecache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe("NodeCache public APIs", () => {
})

test("NodeCache::setM with empty array", () => {
expect(cache.setM([])).toEqual(false)
expect(cache.setM([])).toEqual([false])
})

test("NodeCache::setM with valid input", () => {
Expand Down

0 comments on commit bd3d0b5

Please sign in to comment.