-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathcodegrade_mvp.test.js
More file actions
151 lines (147 loc) · 6.73 KB
/
codegrade_mvp.test.js
File metadata and controls
151 lines (147 loc) · 6.73 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const request = require('supertest')
const server = require('./api/server')
const db = require('./data/db-config')
const { users: initialUsers } = require('./data/seeds/02-users')
const { posts: initialPosts } = require('./data/seeds/03-posts')
beforeAll(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})
beforeEach(async () => {
await db.seed.run()
})
afterAll(async () => {
await db.destroy()
})
test('[0] sanity check', () => {
expect(true).not.toBe(false)
})
describe('server.js', () => {
describe('[GET] /api/users', () => {
test('[1] can get the correct number of users', async () => {
let res = await request(server).get('/api/users')
expect(res.body).toHaveLength(initialUsers.length)
}, 750)
test('[2] can get all the correct users', async () => {
let res = await request(server).get('/api/users')
expect(res.body).toMatchObject(initialUsers)
}, 750)
})
describe('[GET] /api/users/:id', () => {
test('[3] can get the correct user', async () => {
let res = await request(server).get('/api/users/1')
expect(res.body).toMatchObject(initialUsers[0])
expect(res.body).toHaveProperty('id')
res = await request(server).get('/api/users/2')
expect(res.body).toMatchObject(initialUsers[1])
expect(res.body).toHaveProperty('id')
}, 750)
test('[4] responds with a 404 if id does not exist', async () => {
let res = await request(server).get('/api/users/111')
expect(res.status).toBe(404)
}, 750)
test('[5] responds with the correct error message if id does not exist', async () => {
let res = await request(server).get('/api/users/111')
expect(res.body.message).toMatch(/not found/i)
}, 750)
})
describe('[POST] /api/users', () => {
test('[6] creates a new user in the database', async () => {
await request(server).post('/api/users').send({ name: 'foo' })
let users = await db('users')
expect(users).toHaveLength(initialUsers.length + 1)
await request(server).post('/api/users').send({ name: 'bar' })
users = await db('users')
expect(users).toHaveLength(initialUsers.length + 2)
}, 750)
test('[7] responds with the newly created user', async () => {
let res = await request(server).post('/api/users').send({ name: 'foo' })
expect(res.body).toMatchObject({ id: 10, name: 'foo' })
res = await request(server).post('/api/users').send({ name: 'bar' })
expect(res.body).toMatchObject({ id: 11, name: 'bar' })
}, 750)
test('[8] responds with a 400 if missing name', async () => {
let res = await request(server).post('/api/users').send({ random: 'thing' })
expect(res.status).toBe(400)
}, 750)
test('[9] responds with the correct error message if missing name', async () => {
let res = await request(server).post('/api/users').send({ random: 'thing' })
expect(res.body.message).toMatch(/missing required name/i)
}, 750)
})
describe('[PUT] /api/users/:id', () => {
test('[10] writes the updates in the database', async () => {
await request(server).put('/api/users/1').send({ name: 'FRODO BAGGINS' })
let users = await db('users')
expect(users[0]).toMatchObject({ id: 1, name: 'FRODO BAGGINS' })
}, 750)
test('[11] responds with the newly updated user', async () => {
let res = await request(server).put('/api/users/1').send({ name: 'FRODO BAGGINS' })
expect(res.body).toMatchObject({ id: 1, name: 'FRODO BAGGINS' })
}, 750)
test('[12] responds with a 404 if user id does not exist', async () => {
let res = await request(server).put('/api/users/111').send({ name: 'FRODO BAGGINS' })
expect(res.status).toBe(404)
}, 750)
test('[13] responds with a 400 if missing name', async () => {
let res = await request(server).put('/api/users/1').send({ no: 'FRODO BAGGINS' })
expect(res.status).toBe(400)
}, 750)
test('[14] responds with the correct error message if missing name', async () => {
let res = await request(server).put('/api/users/1').send({ no: 'FRODO BAGGINS' })
expect(res.body.message).toMatch(/missing required name/i)
}, 750)
})
describe('[DELETE] /api/users/:id', () => {
test('[15] deletes the user from the database', async () => {
await request(server).delete('/api/users/1')
let users = await db('users')
expect(users[0]).toMatchObject({ name: 'Samwise Gamgee' })
}, 750)
test('[16] responds with the newly deleted user', async () => {
let res = await request(server).delete('/api/users/1')
expect(res.body).toMatchObject({ id: 1, name: 'Frodo Baggins' })
}, 750)
test('[17] responds with a 404 if user id does not exist', async () => {
let res = await request(server).delete('/api/users/111')
expect(res.status).toBe(404)
}, 750)
})
describe('[GET] /api/users/:id/posts', () => {
test('[18] gets the correct number of user posts', async () => {
const res = await request(server).get('/api/users/1/posts')
expect(res.body).toHaveLength(initialPosts.filter(p => p.user_id == 1).length)
}, 750)
test('[19] responds with a 404 if user id does not exist', async () => {
const res = await request(server).get('/api/users/111/posts')
expect(res.status).toBe(404)
}, 750)
})
describe('[POST] /api/users/:id/posts', () => {
test('[20] creates a new user post in the database', async () => {
await request(server).post('/api/users/1/posts').send({ text: 'foo' })
let posts = await db('posts').where('user_id', 1)
expect(posts).toHaveLength(initialPosts.filter(p => p.user_id == 1).length + 1)
await request(server).post('/api/users/1/posts').send({ text: 'bar' })
posts = await db('posts').where('user_id', 1)
expect(posts).toHaveLength(initialPosts.filter(p => p.user_id == 1).length + 2)
}, 750)
test('[21] responds with the newly created user post', async () => {
let res = await request(server).post('/api/users/1/posts').send({ text: 'foo' })
expect(res.body).toHaveProperty('id')
expect(res.body).toMatchObject({ text: 'foo' })
}, 750)
test('[22] responds with a 404 if user id does not exist', async () => {
let res = await request(server).post('/api/users/111/posts').send({ text: 'foo' })
expect(res.status).toBe(404)
}, 750)
test('[23] responds with a 400 if missing text', async () => {
let res = await request(server).post('/api/users/1/posts').send({ no: 'foo' })
expect(res.status).toBe(400)
}, 750)
test('[24] responds with the correct error message if missing text', async () => {
let res = await request(server).post('/api/users/1/posts').send({ no: 'foo' })
expect(res.body.message).toMatch(/missing required text/i)
}, 750)
})
})