forked from fastify/session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemorystore.test.js
More file actions
107 lines (80 loc) · 2.77 KB
/
Copy pathmemorystore.test.js
File metadata and controls
107 lines (80 loc) · 2.77 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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'))
})
})