-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegrade_mvp.test.js
231 lines (227 loc) Β· 9.97 KB
/
codegrade_mvp.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// π You can run these tests in your terminal by executing `npm test`
const request = require('supertest')
const db = require('./data/dbConfig')
const Action = require('./api/actions/actions-model')
const Project = require('./api/projects/projects-model')
const server = require('./api/server')
const projectA = {
name: 'a', description: 'b', completed: false,
}
const projectB = {
name: 'c', description: 'd', completed: true,
}
const actionA = {
project_id: 1, description: 'x', notes: 'y', completed: false,
}
const actionB = {
project_id: 1, description: 'u', notes: 'v', completed: true,
}
const actions = [actionA, actionB]
beforeAll(async () => {
await db.migrate.latest()
})
beforeEach(async () => {
await db('actions').truncate()
await db('projects').truncate()
await db('projects').insert(projectA)
await db('projects').insert(projectB)
await db('actions').insert(actionA)
await db('actions').insert(actionB)
})
afterAll(async () => {
await db.destroy()
})
test('[0] sanity check', () => {
expect(true).not.toBe(false)
})
describe('server.js', () => {
// π PROJECTS
// π PROJECTS
// π PROJECTS
describe('projects endpoints', () => {
describe('[GET] /api/projects', () => {
test('[1] sends back all projects that exist', async () => {
const res = await request(server).get('/api/projects')
expect(res.body).toHaveLength(2)
expect(res.body[0]).toMatchObject(projectA)
expect(res.body[1]).toMatchObject(projectB)
}, 750)
test('[2] sends back empty array if no projects', async () => {
await db('projects').truncate()
const res = await request(server).get('/api/projects')
expect(res.body).toHaveLength(0)
}, 750)
})
describe('[GET] /api/projects/:id', () => {
test('[3] sends back the project with given id', async () => {
const res1 = await request(server).get('/api/projects/1')
const res2 = await request(server).get('/api/projects/2')
expect(res1.body).toMatchObject(projectA)
expect(res2.body).toMatchObject(projectB)
}, 750)
test('[4] responds with a 404 if no project with given id', async () => {
const res = await request(server).get('/api/projects/11')
expect(res.status).toBe(404)
}, 750)
})
describe('[POST] /api/projects', () => {
test('[5] responds with the newly created project', async () => {
const projectNew = { name: 'e', description: 'f', completed: true }
const res = await request(server).post('/api/projects').send(projectNew)
expect(res.body).toMatchObject(projectNew)
}, 750)
test('[6] inserts a new project into projects table', async () => {
const projectNew = { name: 'e', description: 'f', completed: true }
await request(server).post('/api/projects').send(projectNew)
const project = await Project.get(3)
expect(project).toMatchObject(projectNew)
}, 750)
test('[7] responds with a 400 if the request body is missing name or description', async () => {
let projectNew = { name: 'e' }
let res = await request(server).post('/api/projects').send(projectNew)
expect(res.status).toBe(400)
projectNew = { description: 'e' }
res = await request(server).post('/api/projects').send(projectNew)
expect(res.status).toBe(400)
projectNew = {}
res = await request(server).post('/api/projects').send(projectNew)
expect(res.status).toBe(400)
}, 750)
})
describe('[PUT] /api/projects/:id', () => {
test('[8] responds with the updated project', async () => {
let changes = { ...projectA, completed: !projectA.completed }
let res = await request(server).put('/api/projects/1').send(changes)
expect(res.body).toMatchObject(changes)
changes = { ...projectA, description: 'Lady Gaga' }
res = await request(server).put('/api/projects/1').send(changes)
expect(res.body).toMatchObject(changes)
}, 750)
test('[9] updates the project in the projects table', async () => {
let changes = { ...projectA, completed: !projectA.completed }
await request(server).put('/api/projects/1').send(changes)
let project = await Project.get(1)
expect(project.completed).toBe(true)
changes = { ...projectA, name: 'Gaga project' }
await request(server).put('/api/projects/1').send(changes)
project = await Project.get(1)
expect(project.name).toBe('Gaga project')
}, 750)
test('[10] responds with a 400 if the request body is missing name, description or completed', async () => {
let res = await request(server).put('/api/projects/1').send({ description: 'b', completed: false })
expect(res.status).toBe(400)
res = await request(server).put('/api/projects/1').send({ name: 'a', completed: false })
expect(res.status).toBe(400)
res = await request(server).put('/api/projects/1').send({ name: 'a', description: 'b' })
expect(res.status).toBe(400)
res = await request(server).put('/api/projects/1').send({})
expect(res.status).toBe(400)
}, 750)
})
describe('[DELETE] /api/projects/:id', () => {
test('[11] deletes the action with the given id', async () => {
await request(server).delete('/api/projects/1')
let res = await Project.get()
expect(res).toMatchObject([projectB])
await request(server).delete('/api/projects/2')
res = await Project.get()
expect(res).toMatchObject([])
}, 750)
test('[12] responds with a 404 if no project with given id', async () => {
const res = await request(server).delete('/api/projects/11')
expect(res.status).toBe(404)
}, 750)
})
describe('[GET] /api/projects/:id/actions', () => {
test('[13] sends back the actions in project with given id', async () => {
const res = await request(server).get('/api/projects/1/actions')
expect(res.body).toMatchObject(actions)
}, 750)
test('[14] sends back empty array if no actions in project with given id', async () => {
const res = await request(server).get('/api/projects/2/actions')
expect(res.body).toMatchObject([])
}, 750)
})
})
// π ACTIONS
// π ACTIONS
// π ACTIONS
describe('actions endpoints', () => {
describe('[GET] /api/actions', () => {
test('[15] sends back all actions that exist', async () => {
const res = await request(server).get('/api/actions')
expect(res.body).toHaveLength(2)
expect(res.body[0]).toMatchObject(actionA)
expect(res.body[1]).toMatchObject(actionB)
}, 750)
test('[16] sends back empty array if no actions', async () => {
await db('actions').truncate()
const res = await request(server).get('/api/actions')
expect(res.body).toHaveLength(0)
}, 750)
})
describe('[GET] /api/actions/:id', () => {
test('[17] sends back the action with given id', async () => {
const res1 = await request(server).get('/api/actions/1')
const res2 = await request(server).get('/api/actions/2')
expect(res1.body).toMatchObject(actionA)
expect(res2.body).toMatchObject(actionB)
}, 750)
test('[18] responds with a 404 if no action with given id', async () => {
const res = await request(server).get('/api/actions/11')
expect(res.status).toBe(404)
}, 750)
})
describe('[POST] /api/actions', () => {
test('[19] responds with the newly created action', async () => {
const actionNew = { project_id: 2, description: 'm', notes: 'n', completed: false }
const res = await request(server).post('/api/actions').send(actionNew)
expect(res.body).toMatchObject(actionNew)
}, 750)
test('[20] inserts a new action into actions table', async () => {
const actionNew = { project_id: 2, description: 'm', notes: 'n', completed: false }
await request(server).post('/api/actions').send(actionNew)
const action = await Action.get(3)
expect(action).toMatchObject(actionNew)
}, 750)
test('[21] responds with a 400 if the request body is missing notes, description or project_id', async () => {
const actionNew = { project_id: 2, description: 'm' }
const res = await request(server).post('/api/actions').send(actionNew)
expect(res.status).toBe(400)
}, 750)
})
describe('[PUT] /api/actions/:id', () => {
test('[22] responds with the updated action', async () => {
const action = await Action.get(1)
const changes = { ...action, completed: true }
expect(action.completed).toBe(false)
const res = await request(server).put('/api/actions/1').send(changes)
expect(res.body).toMatchObject(changes)
}, 750)
test('[23] updates the action in the actions table', async () => {
let action = await Action.get(1)
await request(server).put('/api/actions/1').send({ ...action, completed: !action.completed })
let updated = await Action.get(1)
expect(updated.completed).toBe(!action.completed)
}, 750)
test('[24] responds with a 400 if the request body is missing missing notes, description, completed or project_id', async () => {
const res = await request(server).put('/api/actions/1').send({})
expect(res.status).toBe(400)
}, 750)
})
describe('[DELETE] /api/actions/:id', () => {
test('[25] deletes the action with the given id', async () => {
await request(server).delete('/api/actions/1')
let actions = await Action.get()
expect(actions).toMatchObject([actionB])
await request(server).delete('/api/actions/2')
actions = await Action.get()
expect(actions).toMatchObject([])
}, 750)
test('[26] responds with a 404 if no action with given id', async () => {
const res = await request(server).get('/api/actions/11')
expect(res.status).toBe(404)
}, 750)
})
})
})