Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a separate unit test file for memorystore #150

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ Store.prototype.destroy = function destroy (sessionId, callback) {
}

module.exports = Store
module.exports.default = Store
module.exports.Store = Store
module.exports.MemoryStore = Store
107 changes: 107 additions & 0 deletions test/memorystore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict'

const test = require('tap').test
const { MemoryStore } = require('../lib/store')
const { EventEmitter } = require('stream')

test('MemoryStore.constructor: created MemoryStore should be an EventEmitter', (t) => {
t.plan(2)

const store = new MemoryStore()

t.ok(store instanceof EventEmitter)
store.on('test', () => t.pass())
store.emit('test')
})

test('MemoryStore.constructor: should accept a Map as internal store', t => {
t.plan(1)

const internalStore = new Map()

const store = new MemoryStore(internalStore)

t.equal(store.store, internalStore)
})

test('MemoryStore.set: should successfully set a value to sessionId ', t => {
t.plan(4)

const internalStore = new Map()

t.equal(internalStore.size, 0)

const store = new MemoryStore(internalStore)
store.set('someId', { key: 'value' }, () => {
t.equal(internalStore.size, 1)
t.equal(internalStore.has('someId'), true)
t.strictSame(internalStore.get('someId'), { key: 'value' })
})
})

test('MemoryStore.get: should successfully get a value for a valid sessionId ', t => {
t.plan(1)

const internalStore = new Map()
internalStore.set('someId', { key: 'value' })

const store = new MemoryStore(internalStore)
store.get('someId', (_err, value) => {
t.strictSame(value, { key: 'value' })
})
})

test('MemoryStore.get: should return undefined for an invalid sessionId ', t => {
t.plan(1)

const internalStore = new Map()
internalStore.set('someId', { key: 'value' })

const store = new MemoryStore(internalStore)
store.get('invalidId', (_err, value) => {
t.strictSame(value, undefined)
})
})

test('MemoryStore.destroy: should remove a sessionId / 1', t => {
t.plan(2)

const internalStore = new Map()
internalStore.set('someId', { key: 'value' })
internalStore.set('anotherId', { key: 'value' })

const store = new MemoryStore(internalStore)
store.destroy('someId', () => {
t.equal(internalStore.size, 1)
t.ok(internalStore.has('anotherId'))
})
})

test('MemoryStore.destroy: should remove a sessionId / 2', t => {
t.plan(2)

const internalStore = new Map()
internalStore.set('someId', { key: 'value' })
internalStore.set('anotherId', { key: 'value' })

const store = new MemoryStore(internalStore)
store.destroy('anotherId', () => {
t.equal(internalStore.size, 1)
t.ok(internalStore.has('someId'))
})
})

test('MemoryStore.destroy: should not remove stored sessions if invalidId is provided', t => {
t.plan(3)

const internalStore = new Map()
internalStore.set('someId', { key: 'value' })
internalStore.set('anotherId', { key: 'value' })

const store = new MemoryStore(internalStore)
store.destroy('invalidId', () => {
t.equal(internalStore.size, 2)
t.ok(internalStore.has('someId'))
t.ok(internalStore.has('anotherId'))
})
})
10 changes: 0 additions & 10 deletions test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const test = require('tap').test
const fastifyPlugin = require('fastify-plugin')
const { buildFastify, DEFAULT_OPTIONS, DEFAULT_COOKIE, DEFAULT_SECRET, DEFAULT_SESSION_ID } = require('./util')
const { Store } = require('..')

test('should decorate request with sessionStore', async (t) => {
t.plan(2)
Expand Down Expand Up @@ -124,15 +123,6 @@ test('should set new session cookie if expired', async (t) => {
t.equal(cookie, undefined)
})

test('store should be an event emitter', t => {
t.plan(1)

const store = new Store()

store.on('test', () => t.pass())
store.emit('test')
})

class FailOnDestroyStore {
constructor () {
this.store = {}
Expand Down