-
Notifications
You must be signed in to change notification settings - Fork 65
/
user.test.ts
42 lines (33 loc) · 932 Bytes
/
user.test.ts
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
import mongoose from 'mongoose';
import User, { IUser } from '../user.model';
describe('User model', () => {
beforeAll(async () => {
await mongoose.connect(global.__MONGO_URI__, {
useNewUrlParser: true
});
});
afterAll(async () => {
mongoose.connection.close();
});
it('Should throw validation errors', () => {
const user = new User();
expect(user.validate).toThrow();
});
it('Should save a user', async () => {
expect.assertions(3);
const user: IUser = new User({
firstName: 'Test first name',
lastName: 'Test last name',
email: 'test@example.com'
});
const spy = jest.spyOn(user, 'save');
user.save();
expect(spy).toHaveBeenCalled();
expect(user).toMatchObject({
firstName: expect.any(String),
lastName: expect.any(String),
email: expect.any(String)
});
expect(user.email).toBe('test@example.com');
});
});