Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19f4b89

Browse files
author
Andrew Mead
committed
Lesson: Advanced Assertions
1 parent c3e0ece commit 19f4b89

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

task-manager/tests/user.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,34 @@ beforeEach(async () => {
2121
})
2222

2323
test('Should signup a new user', async () => {
24-
await request(app).post('/users').send({
24+
const response = await request(app).post('/users').send({
2525
name: 'Andrew',
2626
2727
password: 'MyPass777!'
2828
}).expect(201)
29+
30+
// Assert that the database was changed correctly
31+
const user = await User.findById(response.body.user._id)
32+
expect(user).not.toBeNull()
33+
34+
// Assertions about the response
35+
expect(response.body).toMatchObject({
36+
user: {
37+
name: 'Andrew',
38+
39+
},
40+
token: user.tokens[0].token
41+
})
42+
expect(user.password).not.toBe('MyPass777!')
2943
})
3044

3145
test('Should login existing user', async () => {
32-
await request(app).post('/users/login').send({
46+
const response = await request(app).post('/users/login').send({
3347
email: userOne.email,
3448
password: userOne.password
3549
}).expect(200)
50+
const user = await User.findById(userOneId)
51+
expect(response.body.token).toBe(user.tokens[1].token)
3652
})
3753

3854
test('Should not login nonexistent user', async () => {
@@ -63,6 +79,8 @@ test('Should delete account for user', async () => {
6379
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
6480
.send()
6581
.expect(200)
82+
const user = await User.findById(userOneId)
83+
expect(user).toBeNull()
6684
})
6785

6886
test('Should not delete account for unauthenticate user', async () => {

0 commit comments

Comments
 (0)