-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
105 lines (89 loc) · 2.56 KB
/
models.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
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
const { expect } = require('chai');
const sinon = require('sinon');
require('sinon-mongoose');
const User = require('../models/User');
describe('User Model', () => {
it('should create a new user', (done) => {
const UserMock = sinon.mock(new User({ email: '[email protected]', password: 'root' }));
const user = UserMock.object;
UserMock
.expects('save')
.yields(null);
user.save((err) => {
UserMock.verify();
UserMock.restore();
expect(err).to.be.null;
done();
});
});
it('should return error if user is not created', (done) => {
const UserMock = sinon.mock(new User({ email: '[email protected]', password: 'root' }));
const user = UserMock.object;
const expectedError = {
name: 'ValidationError'
};
UserMock
.expects('save')
.yields(expectedError);
user.save((err, result) => {
UserMock.verify();
UserMock.restore();
expect(err.name).to.equal('ValidationError');
expect(result).to.be.undefined;
done();
});
});
it('should not create a user with the unique email', (done) => {
const UserMock = sinon.mock(User({ email: '[email protected]', password: 'root' }));
const user = UserMock.object;
const expectedError = {
name: 'MongoError',
code: 11000
};
UserMock
.expects('save')
.yields(expectedError);
user.save((err, result) => {
UserMock.verify();
UserMock.restore();
expect(err.name).to.equal('MongoError');
expect(err.code).to.equal(11000);
expect(result).to.be.undefined;
done();
});
});
it('should find user by email', (done) => {
const userMock = sinon.mock(User);
const expectedUser = {
_id: '5700a128bd97c1341d8fb365',
email: '[email protected]'
};
userMock
.expects('findOne')
.withArgs({ email: '[email protected]' })
.yields(null, expectedUser);
User.findOne({ email: '[email protected]' }, (err, result) => {
userMock.verify();
userMock.restore();
expect(result.email).to.equal('[email protected]');
done();
});
});
it('should remove user by email', (done) => {
const userMock = sinon.mock(User);
const expectedResult = {
nRemoved: 1
};
userMock
.expects('remove')
.withArgs({ email: '[email protected]' })
.yields(null, expectedResult);
User.remove({ email: '[email protected]' }, (err, result) => {
userMock.verify();
userMock.restore();
expect(err).to.be.null;
expect(result.nRemoved).to.equal(1);
done();
});
});
});