forked from bloominstituteoftechnology/node-db3-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.test.js
201 lines (198 loc) · 9.64 KB
/
project.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
const request = require('supertest')
const server = require('./api/server')
const db = require('./data/db-config')
const { schemes } = require('./data/seeds/01-schemes')
const { steps } = require('./data/seeds/02-steps')
beforeAll(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})
beforeEach(async () => {
await db.seed.run()
})
afterAll(async (done) => {
await db.destroy()
done()
})
it('sanity check', () => {
expect(true).not.toBe(false)
})
describe('server.js', () => {
describe('[GET] /api/schemes', () => {
it('gets all the schemes from db, including schemes _without_ steps (LEFT VS. INNER JOIN !!!)', async () => {
const res = await request(server).get('/api/schemes')
expect(res.body).toHaveLength(schemes.length)
}, 500)
it('the schemes returned have a `scheme_id` key', async () => {
const res = await request(server).get('/api/schemes')
res.body.forEach((scheme) => {
expect(scheme).toHaveProperty('scheme_id')
})
}, 500)
it('the schemes returned have a `scheme_name` key', async () => {
const res = await request(server).get('/api/schemes')
res.body.forEach((scheme) => {
expect(scheme).toHaveProperty('scheme_name')
})
}, 500)
it('the schemes returned have a `number_of_steps` key', async () => {
const res = await request(server).get('/api/schemes')
res.body.forEach((scheme) => {
expect(scheme).toHaveProperty('number_of_steps')
})
}, 500)
it('the schemes arrive sorted by `scheme_id` ascending', async () => {
const res = await request(server).get('/api/schemes')
res.body.forEach((scheme, idx) => {
expect(scheme.scheme_id).toBe(idx + 1)
})
}, 500)
it('each scheme returned has the correct `number_of_steps`', async () => {
const res = await request(server).get('/api/schemes')
const stepCounts = [[1, 3], [2, 2], [3, 3], [4, 3], [5, 1], [6, 4], [7, 0]]
res.body.forEach((scheme) => {
const count = stepCounts.find(sc => sc[0] == scheme.scheme_id)[1]
expect(scheme.number_of_steps).toBe(count)
})
}, 500)
})
describe('[GET] /api/schemes/:scheme_id', () => {
it('the scheme returned has the correct `scheme_id` _number_', async () => {
const res = await request(server).get('/api/schemes/2')
expect(res.body).toHaveProperty('scheme_id', 2)
}, 500)
it('the scheme returned has the correct `scheme_name`', async () => {
const res = await request(server).get('/api/schemes/2')
expect(res.body).toHaveProperty('scheme_name', schemes[1].scheme_name)
}, 500)
it('the scheme returned has a `steps` property which is an array', async () => {
const res = await request(server).get('/api/schemes/2')
expect(res.body.steps).toBeInstanceOf(Array)
}, 500)
it('the scheme returned has an empty `steps` array if the scheme has no steps', async () => {
const res = await request(server).get('/api/schemes/7')
expect(res.body.steps).toBeInstanceOf(Array)
expect(res.body.steps).toHaveLength(0)
}, 500)
it('the scheme returned has the correct number of steps', async () => {
const stepCounts = [[1, 3], [2, 2], [3, 3], [4, 3], [5, 1], [6, 4], [7, 0]]
for (let idx = 0; idx < stepCounts.length; idx++) {
const res = await request(server).get('/api/schemes/' + stepCounts[idx][0])
expect(res.body.steps).toHaveLength(stepCounts[idx][1])
}
}, 500)
it('each step inside the scheme returned has `step_id`, `step_number` and `instructions` keys', async () => {
const res = await request(server).get('/api/schemes/2')
res.body.steps.forEach(st => {
expect(st).toHaveProperty('step_id')
expect(st).toHaveProperty('step_number')
expect(st).toHaveProperty('instructions')
})
}, 500)
it('the steps inside the scheme returned are ordered by `step_number` ascending', async () => {
const res = await request(server).get('/api/schemes/2')
expect(res.body.steps).toMatchObject([
{ step_number: 1, instructions: 'collect all the sheep in Scotland' },
{ step_number: 2, instructions: 'profit' },
])
}, 500)
it('responds with 404 and proper error on non-existing `scheme_id`', async () => {
const res = await request(server).get('/api/schemes/222')
expect(res.status).toBe(404)
expect(res.body).toHaveProperty('message', 'scheme with scheme_id 222 not found')
}, 500)
})
describe('[GET] /api/schemes/:scheme_id/steps', () => {
it('returns the correct number of steps for a `scheme_id`', async () => {
const stepCounts = [[1, 3], [2, 2], [3, 3], [4, 3], [5, 1], [6, 4], [7, 0]]
for (let idx = 0; idx < stepCounts.length; idx++) {
const res = await request(server).get(`/api/schemes/${stepCounts[idx][0]}/steps`)
expect(res.body).toHaveLength(stepCounts[idx][1])
}
}, 500)
it('the steps returned are ordered by `step_number` ascending', async () => {
const res = await request(server).get('/api/schemes/2/steps')
expect(res.body[0]).toMatchObject(
{ step_number: 1 },
)
expect(res.body[1]).toMatchObject(
{ step_number: 2 },
)
}, 500)
it('the steps returned have the proper `step_number`, `step_id`, `instructions` and `scheme_name`', async () => {
const res = await request(server).get('/api/schemes/5/steps')
expect(res.body[0]).toMatchObject(
{ step_number: 1, instructions: 'quest and quest some more', scheme_name: 'Find the Holy Grail' },
)
}, 500)
it('responds with 404 and proper error on non-existing `scheme_id`', async () => {
const res = await request(server).get('/api/schemes/222/steps')
expect(res.status).toBe(404)
expect(res.body).toHaveProperty('message', 'scheme with scheme_id 222 not found')
}, 500)
})
describe('[POST] /api/schemes', () => {
it('can create a new scheme in the database', async () => {
await request(server).post('/api/schemes').send({ scheme_name: 'foo' })
const updatedSchemes = await db('schemes')
expect(updatedSchemes).toHaveLength(schemes.length + 1)
}, 500)
it('responds with 201 status code', async () => {
const res = await request(server).post('/api/schemes').send({ scheme_name: 'foo' })
expect(res.status).toBe(201)
}, 500)
it('responds with the newly created scheme record', async () => {
const res = await request(server).post('/api/schemes').send({ scheme_name: 'foo' })
expect(res.body).toHaveProperty('scheme_id', 8)
expect(res.body).toMatchObject({ scheme_name: 'foo' })
}, 500)
it('responds with 400 and proper message on missing or bad `scheme_name`', async () => {
let res = await request(server).post('/api/schemes').send({ scheme_name: null })
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid scheme_name')
res = await request(server).post('/api/schemes').send({})
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid scheme_name')
res = await request(server).post('/api/schemes').send({ scheme_name: 7 })
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid scheme_name')
}, 500)
})
describe('[POST] /api/schemes/:scheme_id/steps', () => {
it('can create a new step in the database', async () => {
await request(server).post('/api/schemes/7/steps').send({ instructions: 'foo', step_number: 60 })
const updatedSteps = await db('steps')
expect(updatedSteps).toHaveLength(steps.length + 1)
}, 500)
it('responds with 201 status code', async () => {
const res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'foo', step_number: 60 })
expect(res.status).toBe(201)
}, 500)
it('responds with the complete list of steps for the given `scheme_id` including the new one', async () => {
let res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'foo', step_number: 60 })
expect(res.body).toHaveLength(1)
res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'bar', step_number: 61 })
expect(res.body).toHaveLength(2)
}, 500)
it('responds with well formed steps ordered by `step_number` ascending', async () => {
let res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'bar', step_number: 20 })
expect(res.body).toMatchObject([{ instructions: 'bar', step_number: 20 }])
res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'foo', step_number: 10 })
expect(res.body).toMatchObject([{ instructions: 'foo', step_number: 10 }, { instructions: 'bar', step_number: 20 }])
}, 500)
it('responds with 400 and proper message on missing or bad `step_number` or `instructions`', async () => {
let res = await request(server).post('/api/schemes/7/steps').send({ instructions: null, step_number: 20 })
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid step')
res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'foo', step_number: 'bar' })
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid step')
res = await request(server).post('/api/schemes/7/steps').send({ step_number: 20 })
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid step')
res = await request(server).post('/api/schemes/7/steps').send({ instructions: 'foo', step_number: -1 })
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'invalid step')
}, 500)
})
})