forked from bloominstituteoftechnology/node-db2-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegrade_mvp.test.js
More file actions
133 lines (130 loc) · 4.89 KB
/
codegrade_mvp.test.js
File metadata and controls
133 lines (130 loc) · 4.89 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
const request = require('supertest')
const server = require('./api/server')
const db = require('./data/db-config')
const cars = [
{
vin: '11111111111111111',
make: 'toyota',
model: 'prius',
mileage: 250000,
title: 'salvage',
transmission: 'CVT',
},
{
vin: '22222222222222222',
make: 'ford',
model: 'mustang',
mileage: 120000,
title: 'clean',
transmission: 'manual',
},
{
vin: '33333333333333333',
make: 'honda',
model: 'accord',
mileage: 220000,
title: 'clean',
transmission: 'automatic',
},
]
beforeAll(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})
afterAll(async () => {
await db.destroy()
})
test('[0] sanity check', () => {
expect(true).not.toBe(false)
})
describe('server.js', () => {
describe('[GET] /api/cars', () => {
beforeEach(async () => {
await db('cars').truncate()
await db('cars').insert(cars)
})
test('[1] gets the correct number of cars', async () => {
const res = await request(server).get('/api/cars')
expect(res.body).toHaveLength(cars.length)
}, 750)
test('[2] gets the correct cars', async () => {
const res = await request(server).get('/api/cars')
expect(res.body).toMatchObject(cars)
}, 750)
})
describe('[GET] /api/cars/:id', () => {
beforeEach(async () => {
await db('cars').truncate()
await db('cars').insert(cars)
})
test('[3] gets the correct car', async () => {
let res = await request(server).get('/api/cars/1')
expect(res.body).toMatchObject(cars[0])
res = await request(server).get('/api/cars/2')
expect(res.body).toMatchObject(cars[1])
res = await request(server).get('/api/cars/3')
expect(res.body).toMatchObject(cars[2])
}, 750)
test('[4] responds with 404 on id not found', async () => {
let res = await request(server).get('/api/cars/111')
expect(res.status).toBe(404)
}, 750)
})
describe('[POST] /api/cars', () => {
beforeEach(async () => {
await db('cars').truncate()
})
test('[5] creates a car in the database', async () => {
await request(server).post('/api/cars').send(cars[0])
await request(server).post('/api/cars').send(cars[1])
await request(server).post('/api/cars').send(cars[2])
const carsDb = await db('cars')
expect(carsDb[0]).toMatchObject(cars[0])
expect(carsDb[1]).toMatchObject(cars[1])
expect(carsDb[2]).toMatchObject(cars[2])
}, 750)
test('[6] responds with the newly created car', async () => {
const res1 = await request(server).post('/api/cars').send(cars[0])
const res2 = await request(server).post('/api/cars').send(cars[1])
const res3 = await request(server).post('/api/cars').send(cars[2])
expect(res1.body).toMatchObject({ id: 1, ...cars[0] })
expect(res2.body).toMatchObject({ id: 2, ...cars[1] })
expect(res3.body).toMatchObject({ id: 3, ...cars[2] })
}, 750)
test('[7] responds with a 400 and proper error on missing vin', async () => {
const { vin, ...badCar } = cars[0] // eslint-disable-line
const res = await request(server).post('/api/cars').send(badCar)
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'vin is missing')
}, 750)
test('[8] responds with a 400 and proper error on missing make', async () => {
const { make, ...badCar } = cars[0] // eslint-disable-line
const res = await request(server).post('/api/cars').send(badCar)
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'make is missing')
}, 750)
test('[9] responds with a 400 and proper error on missing model', async () => {
const { model, ...badCar } = cars[0] // eslint-disable-line
const res = await request(server).post('/api/cars').send(badCar)
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'model is missing')
}, 750)
test('[10] responds with a 400 and proper error on missing mileage', async () => {
const { mileage, ...badCar } = cars[0] // eslint-disable-line
const res = await request(server).post('/api/cars').send(badCar)
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'mileage is missing')
}, 750)
test('[11] responds with a 400 and proper error on invalid vin', async () => {
const badCar = { ...cars[0], vin: 'abc' }
const res = await request(server).post('/api/cars').send(badCar)
expect(res.status).toBe(400)
expect(res.body).toHaveProperty('message', 'vin abc is invalid')
}, 750)
test('[12] responds with a 400 and proper error on non-unique vin', async () => {
await request(server).post('/api/cars').send(cars[0])
const res = await request(server).post('/api/cars').send(cars[0])
expect(res.body).toHaveProperty('message', 'vin 11111111111111111 already exists')
}, 750)
})
})