This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.test.js
68 lines (57 loc) · 1.54 KB
/
session.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
/* eslint-disable */
jest.mock('./logger')
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}`
// require module
let m = null
beforeAll(() => {
jest.mock('mongoose', () => {
const mongoose = {
Schema: function (o) { return o },
model: function (n, s) { return n },
}
Object.defineProperty(mongoose.Schema, 'Types', {
value: { ObjectId: "ObjectId" },
})
return mongoose
})
m = require('./session')
return m
})
test('requires module and sets it up', () => {
expect(m).toBeTruthy()
})
test('creates a correct uri', done => {
m((u, n) => new Promise((resolve, reject) => {
expect(u).toBe(uri)
done()
const connection = {
model: function (collectionName) { }
}
resolve(connection)
}))
})
test('connects to the correct collection', done => {
m((u, n) => new Promise((resolve, reject) => {
const connection = {
model: function (modelName) {
expect(modelName).toBe('Session')
done()
}
}
resolve(connection)
}))
})
test('exits if no database connection', done => {
jest
.spyOn(process, 'exit')
.mockImplementation((exitCode) => {
expect(exitCode).toBe(0)
done()
})
m((u, n) => Promise.reject('database error'))
})