This repository has been archived by the owner on Jan 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessstore.test.js
73 lines (55 loc) · 1.78 KB
/
sessstore.test.js
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
/* eslint-disable */
process.env.DATABASE_USER = 'user'
process.env.DATABASE_PASSWORD = 'password'
process.env.DATABASE_PROTOCOL = 'mongodb'
process.env.DATABASE_ADDRESS = 'address'
process.env.DATABASE_NAME = 'name'
const uri = `${process.env.DATABASE_PROTOCOL}://${process.env.DATABASE_USER}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_ADDRESS}/${process.env.DATABASE_NAME}`
describe('session store', () => {
jest.mock('./logger')
jest.useFakeTimers()
const mockStore = {
on: jest.fn(),
}
const storeConstructor = jest.fn(() => mockStore)
jest.setMock('connect-mongodb-session', () => storeConstructor )
const m = require('./sessstore')
test('creates a correct uri', () => {
m({})
expect(storeConstructor)
.toBeCalledWith({ uri: uri, collection: 'sessions' })
})
test('resolves with store on connected', async () => {
mockStore.on.mockImplementation((event, cb) => {
if (event === 'connected')
cb()
})
await expect(m({})).resolves.toBe(mockStore)
})
test('retries after 2000ms for store error', done => {
mockStore.on.mockImplementation((event, cb) => {
if (event === 'error')
cb()
})
setTimeout.mockImplementation((fn, time) => {
expect(time).toEqual(2000)
done()
})
m({})
})
})
describe('adds uri options correctly', () => {
jest.resetModules()
jest.mock('./logger')
const storeConstructor = jest.fn(() => {
return { on: jest.fn() }
})
jest.setMock('connect-mongodb-session', () => storeConstructor )
process.env.DATABASE_OPTIONS = 'options'
const m = require('./sessstore')
test('creates a correct uri', () => {
m({})
expect(storeConstructor)
.toBeCalledWith({ uri: uri + `?${process.env.DATABASE_OPTIONS}`, collection: 'sessions' })
})
})