This repository has been archived by the owner on Mar 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.test.js
78 lines (64 loc) · 1.9 KB
/
user.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
73
74
75
76
77
/* 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}`
describe('sets up correctly', () => {
jest.mock('mongoose', () => {
return {
Schema: function (o) { return { statics: jest.fn(), } },
model: function (n, s) { return n },
}
})
const m = require('./user')
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('User')
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'))
})
})
describe('adds uri options correctly', () => {
jest.resetModules()
jest.mock('mongoose', () => {
return {
Schema: function (o) { return { statics: jest.fn(), } },
model: function (n, s) { return n },
}
})
process.env.DATABASE_OPTIONS = 'options'
const m = require('./user')
test('creates a correct uri with options', done => {
m((u, n) => new Promise((resolve, reject) => {
expect(u).toBe(uri + '?' + process.env.DATABASE_OPTIONS)
done()
}))
})
})