Skip to content

Commit 28bce2c

Browse files
committed
First series of assertions. Latest launch returns single item instead of array with one item.
1 parent 2ac7597 commit 28bce2c

File tree

2 files changed

+114
-15
lines changed

2 files changed

+114
-15
lines changed

routes/v1-launches.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5+
const error = {error: "No results found"}
56

67
// Get most recent launch
78
v1.get("/latest", (req, res, next) => {
@@ -10,7 +11,11 @@ v1.get("/latest", (req, res, next) => {
1011
if (err) {
1112
return next(err)
1213
}
13-
res.json(doc)
14+
if (doc.length == 0) {
15+
res.status(404)
16+
return res.json(error)
17+
}
18+
res.json(doc[0])
1419
})
1520
})
1621

test/v1-all.test.js

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ beforeAll((done) => {
1515
test("It should return 404 endpoint error", () => {
1616
return request(app).get("/v2").then(response => {
1717
expect(response.statusCode).toBe(404)
18-
expect(response.text).toContain("No results found")
18+
expect(response.body).toMatchObject({
19+
error: "No results found"
20+
})
1921
})
2022
})
2123

@@ -51,7 +53,12 @@ test("It should return 500 error", () => {
5153
test("It should return home info", () => {
5254
return request(app).get("/v1").then(response => {
5355
expect(response.statusCode).toBe(200)
54-
expect(response.text).toContain("SpaceX-API")
56+
expect(response.body).toHaveProperty("description")
57+
expect(response.body).toHaveProperty("organization", "r/SpaceX")
58+
expect(response.body).toHaveProperty("organization_link", "https://github.com/r-spacex")
59+
expect(response.body).toHaveProperty("project_link", "https://github.com/r-spacex/SpaceX-API")
60+
expect(response.body).toHaveProperty("project_name", "SpaceX-API")
61+
expect(response.body).toHaveProperty("version")
5562
})
5663
})
5764

@@ -62,7 +69,22 @@ test("It should return home info", () => {
6269
test("It should return company info", () => {
6370
return request(app).get("/v1/info").then(response => {
6471
expect(response.statusCode).toBe(200)
65-
expect(response.text).toContain("Elon Musk")
72+
expect(response.body).toHaveProperty("name", "SpaceX")
73+
expect(response.body).toHaveProperty("founder", "Elon Musk")
74+
expect(response.body).toHaveProperty("founded", 2002)
75+
expect(response.body).toHaveProperty("employees")
76+
expect(response.body).toHaveProperty("vehicles")
77+
expect(response.body).toHaveProperty("launch_sites")
78+
expect(response.body).toHaveProperty("test_sites")
79+
expect(response.body).toHaveProperty("ceo")
80+
expect(response.body).toHaveProperty("cto")
81+
expect(response.body).toHaveProperty("coo")
82+
expect(response.body).toHaveProperty("cto_propulsion")
83+
expect(response.body).toHaveProperty("valuation")
84+
expect(response.body).toHaveProperty("headquarters.address", "Rocket Road")
85+
expect(response.body).toHaveProperty("headquarters.city", "Hawthorne")
86+
expect(response.body).toHaveProperty("headquarters.state", "California")
87+
expect(response.body).toHaveProperty("summary")
6688
})
6789
})
6890

@@ -73,38 +95,66 @@ test("It should return company info", () => {
7395
test("It should return all vehicle info", () => {
7496
return request(app).get("/v1/vehicles").then(response => {
7597
expect(response.statusCode).toBe(200)
76-
expect(response.text).toContain("Falcon 1")
77-
expect(response.text).toContain("Falcon 9")
78-
expect(response.text).toContain("Falcon Heavy")
79-
expect(response.text).toContain("Dragon")
98+
expect(response.body).toHaveLength(4)
99+
expect(response.body[0]).toHaveProperty("name", "Falcon 1")
100+
expect(response.body[1]).toHaveProperty("name", "Falcon 9")
101+
expect(response.body[2]).toHaveProperty("name", "Falcon Heavy")
102+
expect(response.body[3]).toHaveProperty("name", "Dragon 1")
103+
104+
response.body.forEach(item => {
105+
expect(item).toHaveProperty("id")
106+
expect(item).toHaveProperty("name")
107+
expect(item).toHaveProperty("active")
108+
})
80109
})
81110
})
82111

83112
test("It should return Falcon 1 info", () => {
84113
return request(app).get("/v1/vehicles/falcon1").then(response => {
85114
expect(response.statusCode).toBe(200)
86-
expect(response.text).toContain("Falcon 1")
115+
expect(response.body).toHaveProperty("name", "Falcon 1")
116+
expect(response.body).toHaveProperty("stages", 2)
117+
expect(response.body).toHaveProperty("cost_per_launch")
118+
expect(response.body).toHaveProperty("success_rate_pct")
119+
expect(response.body).toHaveProperty("first_flight", "2016-03-24")
120+
expect(response.body).toHaveProperty("country")
121+
expect(response.body).toHaveProperty("company", "SpaceX")
122+
expect(response.body).toHaveProperty("description")
87123
})
88124
})
89125

90126
test("It should return Falcon 9 info", () => {
91127
return request(app).get("/v1/vehicles/falcon9").then(response => {
92128
expect(response.statusCode).toBe(200)
93-
expect(response.text).toContain("Falcon 9")
129+
expect(response.body).toHaveProperty("name", "Falcon 9")
130+
expect(response.body).toHaveProperty("stages", 2)
131+
expect(response.body).toHaveProperty("cost_per_launch")
132+
expect(response.body).toHaveProperty("success_rate_pct")
133+
expect(response.body).toHaveProperty("first_flight", "2010-06-04")
134+
expect(response.body).toHaveProperty("country")
135+
expect(response.body).toHaveProperty("company", "SpaceX")
136+
expect(response.body).toHaveProperty("description")
94137
})
95138
})
96139

97140
test("It should return Falcon Heavy info", () => {
98141
return request(app).get("/v1/vehicles/falconheavy").then(response => {
99142
expect(response.statusCode).toBe(200)
100-
expect(response.text).toContain("Falcon Heavy")
143+
expect(response.body).toHaveProperty("name", "Falcon Heavy")
144+
expect(response.body).toHaveProperty("stages", 2)
145+
expect(response.body).toHaveProperty("cost_per_launch")
146+
// expect(response.body).toHaveProperty("success_rate_pct")
147+
// expect(response.body).toHaveProperty("first_flight")
148+
expect(response.body).toHaveProperty("country")
149+
expect(response.body).toHaveProperty("company", "SpaceX")
150+
expect(response.body).toHaveProperty("description")
101151
})
102152
})
103153

104154
test("It should return Dragon info", () => {
105155
return request(app).get("/v1/vehicles/dragon").then(response => {
106156
expect(response.statusCode).toBe(200)
107-
expect(response.text).toContain("Dragon")
157+
expect(response.body).toHaveProperty("name", "Dragon 1")
108158
})
109159
})
110160

@@ -115,7 +165,14 @@ test("It should return Dragon info", () => {
115165
test("It should return all launchpads", () => {
116166
return request(app).get("/v1/launchpads").then(response => {
117167
expect(response.statusCode).toBe(200)
118-
expect(response.text.length).toBe(3735)
168+
expect(response.body).toHaveLength(8)
169+
response.body.forEach(item => {
170+
expect(item).toHaveProperty("id")
171+
expect(item).toHaveProperty("full_name")
172+
expect(item).toHaveProperty("status")
173+
expect(item).toHaveProperty("vehicles_launched")
174+
expect(item).toHaveProperty("details")
175+
})
119176
})
120177
})
121178

@@ -140,14 +197,51 @@ test("It should return no launchpads found info", () => {
140197
test("It should return all past launches", () => {
141198
return request(app).get("/v1/launches").then(response => {
142199
expect(response.statusCode).toBe(200)
143-
expect(response.text.length).toBeGreaterThan(70000)
200+
expect(response.body.length).toBeGreaterThanOrEqual(50)
201+
response.body.forEach(item => {
202+
expect(item).toHaveProperty("flight_number", expect.anything())
203+
expect(item).toHaveProperty("launch_year", expect.stringMatching(/^[0-9]{4}$/))
204+
// expect(item).toHaveProperty("launch_date_unix")
205+
expect(item).toHaveProperty("launch_date_utc", expect.anything())
206+
expect(item).toHaveProperty("launch_date_local", expect.anything())
207+
expect(item).toHaveProperty("rocket.rocket_id")
208+
expect(item).toHaveProperty("rocket.rocket_name")
209+
expect(item).toHaveProperty("rocket.rocket_type")
210+
expect(item).toHaveProperty("telemetry.flight_club")
211+
expect(item).toHaveProperty("core_serial")
212+
expect(item).toHaveProperty("cap_serial")
213+
expect(item).toHaveProperty("reuse.core")
214+
expect(item).toHaveProperty("reuse.side_core1")
215+
expect(item).toHaveProperty("reuse.side_core2")
216+
expect(item).toHaveProperty("reuse.fairings")
217+
expect(item).toHaveProperty("reuse.capsule")
218+
expect(item).toHaveProperty("launch_site.site_id")
219+
expect(item).toHaveProperty("launch_site.site_name")
220+
expect(item).toHaveProperty("payloads")
221+
expect(item.payloads.length).toBeGreaterThan(0)
222+
item.payloads.forEach(payload => {
223+
expect(payload).toHaveProperty("payload_id")
224+
expect(payload).toHaveProperty("customers")
225+
expect(payload).toHaveProperty("payload_type")
226+
expect(payload).toHaveProperty("payload_mass_kg")
227+
expect(payload).toHaveProperty("payload_mass_lbs")
228+
expect(payload).toHaveProperty("orbit")
229+
})
230+
expect(item).toHaveProperty("launch_success")
231+
expect(item).toHaveProperty("reused")
232+
expect(item).toHaveProperty("land_success")
233+
expect(item).toHaveProperty("landing_type")
234+
expect(item).toHaveProperty("landing_vehicle")
235+
expect(item).toHaveProperty("links")
236+
expect(item).toHaveProperty("details")
237+
})
144238
})
145239
})
146240

147241
test("It should return the latest launch", () => {
148242
return request(app).get("/v1/launches/latest").then(response => {
149243
expect(response.statusCode).toBe(200)
150-
expect(response.text.length).toBeGreaterThan(0)
244+
expect(response.body).toHaveProperty("flight_number")
151245
})
152246
})
153247

0 commit comments

Comments
 (0)