-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.test.js
94 lines (79 loc) · 2.61 KB
/
server.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import 'regenerator-runtime/runtime'
import request from 'supertest'
import models from './models/index'
const createServer = require("./server")
const app = createServer()
const { User } = models
describe('Users API', function() {
beforeAll(async () => {
await models.sequelize.sync({ force: true })
})
afterAll(async () => {
await models.sequelize.close()
})
const userEntity = ({ name }) => {
return { id: expect.anything(), name }
}
describe('GET /users', function() {
it('responds with json', async function() {
await User.create({ name: 'J' })
const response = await request(app).get('/users').expect(200)
expect(response.body).toEqual([userEntity({ name: 'J' })])
})
})
describe('POST /users', function() {
it('responds with json', async function() {
const response = await request(app)
.post('/users')
.set('Accept', 'application/json')
.send({ name: 'John'})
.expect(201)
expect(response.body).toEqual(userEntity({ name: 'John' }))
})
it('respond with error if no name', async function() {
const response = await request(app)
.post('/users')
.set('Accept', 'application/json')
.expect(422)
expect(response.body).toEqual({ errors: ['Name cannot be blank'] })
})
})
describe('PATCH /users', function() {
it('responds with json', async function() {
const user = await User.create({ name: 'Updated' })
const response = await request(app)
.patch(`/users/${user.id}`)
.set('Accept', 'application/json')
.send({ name: 'John updated'})
.expect(200)
expect(response.body).toEqual(userEntity({ name: 'John updated' }))
})
it('responds with error if user not exist', async function() {
await request(app)
.patch(`/users/-1`)
.send({ name: 'John updated'})
.expect(404)
})
it('respond with error if no name', async function() {
const user = await User.create({ name: 'Updated' })
const response = await request(app)
.patch(`/users/${user.id}`)
.set('Accept', 'application/json')
.expect(422)
expect(response.body).toEqual({ errors: ['Name cannot be blank'] })
})
})
describe('DELETE /users', function() {
it('responds with json', async function() {
const user = await User.create({ name: 'Deleted' })
await request(app)
.delete(`/users/${user.id}`)
.expect(200)
})
it('return error if user not exist', async function() {
await request(app)
.delete(`/users/-1`)
.expect(404)
})
})
})