-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegrade_mvp.test.js
216 lines (207 loc) · 9.07 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
/*
IMPORTANT NOTES 🔥
IMPORTANT NOTES 🔥
IMPORTANT NOTES 🔥
1- Run tests using `npm test` script (see `package.json`)
2- Tests use their own database connection (see `knexfile.js` and `data/dbConfig.js`)
3- Tests will fail to run until server.js and migration(s) are sufficiently fleshed out
4- Opening the `test.db3` with SQLite Studio might prevent tests from working
5- If the tests crash due to a "locked migration table", stop tests and delete `test.db3` file
6- Manual testing with Postman or HTTPie is still necessary
*/
const request = require('supertest')
const db = require('./data/dbConfig')
const server = require('./api/server')
const projectA = { project_name: 'Web API', project_description: 'Build APIs' }
const projectB = { project_name: 'Databases', project_description: 'Learn SQL', project_completed: 1 }
const projectC = { project_name: 'Authentication' }
const resourceA = { resource_name: 'keyboard' }
const resourceB = { resource_name: 'computer', resource_description: 'Windows PC' }
const taskA = { task_description: 'Do foo', project_id: 1 }
const taskB = { task_description: 'Do bar', task_notes: 'Use Postman!', project_id: 1 }
const taskC = { task_description: 'Do baz', task_notes: 'Have fun!', task_completed: 1, project_id: 2 }
afterAll(async () => {
await db.destroy()
})
beforeEach(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})
test('[0] sanity check', () => {
expect(true).not.toBe(false)
})
describe('server.js', () => {
// 👉 PROJECTS
// 👉 PROJECTS
// 👉 PROJECTS
describe('projects endpoints', () => {
describe('[GET] /api/projects', () => {
beforeEach(async () => {
await db('projects').insert(projectA)
await db('projects').insert(projectB)
})
test('[1] can get all projects that exist in the table', async () => {
const res = await request(server).get('/api/projects')
expect(res.body).toHaveLength(2)
}, 750)
test('[2] each project contains project_name, project_description and project_completed (as a boolean)', async () => {
const res = await request(server).get('/api/projects')
expect(res.body[0]).toMatchObject({ ...projectA, project_completed: false })
expect(res.body[1]).toMatchObject({ ...projectB, project_completed: true })
}, 750)
})
describe('[POST] /api/projects', () => {
test('[3] can add a new project to the table', async () => {
await request(server).post('/api/projects').send(projectA)
await request(server).post('/api/projects').send(projectB)
await request(server).post('/api/projects').send(projectC)
const projects = await db('projects')
expect(projects).toHaveLength(3)
expect(projects[0]).toMatchObject(projectA)
expect(projects[1]).toMatchObject({ project_name: 'Databases', project_description: 'Learn SQL' })
expect(projects[2]).toMatchObject({ ...projectC, project_description: null })
}, 750)
test('[4] responds with the newly created project with its project_completed as a boolean', async () => {
let res = await request(server).post('/api/projects').send(projectA)
expect(res.body).toMatchObject({ ...projectA, project_completed: false })
res = await request(server).post('/api/projects').send(projectB)
expect(res.body).toMatchObject({ ...projectB, project_completed: true })
res = await request(server).post('/api/projects').send(projectC)
expect(res.body).toMatchObject({ ...projectC, project_completed: false })
}, 750)
test('[5] rejects projects lacking a project_name with an error status code', async () => {
const res = await request(server).post('/api/projects').send({})
const projects = await db('projects')
expect(res.status + '').toMatch(/4|5/)
expect(projects).toHaveLength(0)
}, 750)
})
})
// 👉 RESOURCES
// 👉 RESOURCES
// 👉 RESOURCES
describe('resources endpoints', () => {
describe('[GET] /api/resources', () => {
test('[6] can get all resources in the table', async () => {
await db('resources').insert(resourceA)
await db('resources').insert(resourceB)
const res = await request(server).get('/api/resources')
expect(res.body).toHaveLength(2)
expect(res.body[0]).toMatchObject(resourceA)
expect(res.body[1]).toMatchObject(resourceB)
}, 750)
})
describe('[POST] /api/resources', () => {
test('[7] can add a new resource to the table', async () => {
await request(server).post('/api/resources').send(resourceA)
await request(server).post('/api/resources').send(resourceB)
const resources = await db('resources')
expect(resources).toHaveLength(2)
expect(resources[0]).toMatchObject(resourceA)
expect(resources[1]).toMatchObject(resourceB)
}, 750)
test('[8] responds with the newly created resource', async () => {
const res = await request(server).post('/api/resources').send(resourceA)
expect(res.body).toMatchObject(resourceA)
}, 750)
test('[9] rejects a resource with an existing resource_name with an error status code', async () => {
await db('resources').insert(resourceA)
const res = await request(server).post('/api/resources').send(resourceA)
const resources = await db('resources')
expect(res.status + '').toMatch(/4|5/)
expect(resources).toHaveLength(1)
}, 750)
})
})
// 👉 TASKS
// 👉 TASKS
// 👉 TASKS
describe('tasks endpoints', () => {
beforeEach(async () => {
await db('projects').insert(projectA)
await db('projects').insert(projectB)
await db('tasks').insert(taskA)
await db('tasks').insert(taskB)
await db('tasks').insert(taskC)
})
describe('[GET] /api/tasks', () => {
test('[10] can get all tasks in the table', async () => {
const res = await request(server).get('/api/tasks')
expect(res.body).toHaveLength(3)
}, 750)
test('[11] each task contains task_notes and task_description and task_completed (as a boolean)', async () => {
const res = await request(server).get('/api/tasks')
expect(res.body[0]).toMatchObject({
task_description: 'Do foo',
task_notes: null,
task_completed: false,
})
expect(res.body[1]).toMatchObject({
task_description: 'Do bar',
task_notes: 'Use Postman!',
task_completed: false,
})
expect(res.body[2]).toMatchObject({
task_description: 'Do baz',
task_notes: 'Have fun!',
task_completed: true,
})
}, 750)
test('[12] each task contains the project_name and the project_description', async () => {
const res = await request(server).get('/api/tasks')
expect(res.body[0]).toMatchObject({
project_name: 'Web API',
project_description: 'Build APIs',
})
expect(res.body[1]).toMatchObject({
project_name: 'Web API',
project_description: 'Build APIs',
})
expect(res.body[2]).toMatchObject({
project_name: 'Databases',
project_description: 'Learn SQL',
})
}, 750)
})
describe('[POST] /api/tasks', () => {
test('[13] can add a new task to the db', async () => {
await db('tasks').truncate()
await request(server).post('/api/tasks').send(taskA)
await request(server).post('/api/tasks').send(taskB)
await request(server).post('/api/tasks').send(taskC)
const tasks = await db('tasks')
expect(tasks).toHaveLength(3)
}, 750)
test('[14] responds with the newly created task with the task_completed as a boolean', async () => {
await db('tasks').truncate()
const res = await request(server).post('/api/tasks').send(taskA)
expect(res.body).toMatchObject({
task_description: 'Do foo',
task_notes: null,
task_completed: false,
})
}, 750)
test('[15] rejects a task lacking a task_description with an error status code', async () => {
await db('tasks').truncate()
const res = await request(server).post('/api/tasks').send({ project_id: 1 })
const tasks = await db('tasks')
expect(res.status + '').toMatch(/4|5/)
expect(tasks).toHaveLength(0)
}, 750)
test('[16] rejects a task lacking a project_id with an error status code', async () => {
await db('tasks').truncate()
const res = await request(server).post('/api/tasks').send({ task_description: 'Execute order 66' })
const tasks = await db('tasks')
expect(res.status + '').toMatch(/4|5/)
expect(tasks).toHaveLength(0)
}, 750)
test('[17] rejects a task containing an invalid project_id with an error status code', async () => {
await db('tasks').truncate()
const res = await request(server).post('/api/tasks').send({ ...taskA, project_id: 66 })
const tasks = await db('tasks')
expect(res.status + '').toMatch(/4|5/)
expect(tasks).toHaveLength(0)
}, 750)
})
})
})