Skip to content

Commit f3907f2

Browse files
authored
Merge pull request #55 from Srokap/more_assertions
More assertions
2 parents 88075bf + 4b5cb4d commit f3907f2

File tree

3 files changed

+369
-15
lines changed

3 files changed

+369
-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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* We make ourselves compatible with AsymmetricMatcher class used by Jest
3+
* @see https://github.com/facebook/jest/blob/master/packages/expect/src/asymmetric_matchers.js
4+
*
5+
* Perhaps we can simplify that a bit and write only Jasmine-compatible matchers.
6+
* @see https://jasmine.github.io/2.4/introduction.html#section-Custom_asymmetric_equality_tester
7+
*/
8+
class CustomAsymmetricMatcher {
9+
constructor() {
10+
// $$typeof is used internally by Jest and just to be sure let's use jest Symbol
11+
this.$$typeof = Symbol.for("jest.asymmetricMatcher")
12+
}
13+
}
14+
15+
/**
16+
* Expect metric and imperial volume numbers in object.
17+
*/
18+
class SpacexVolume extends CustomAsymmetricMatcher {
19+
asymmetricMatch(any) {
20+
expect(any).toEqual(expect.anything())
21+
expect(any).toHaveProperty("cubic_meters", expect.any(Number))
22+
expect(any).toHaveProperty("cubic_feet", expect.any(Number))
23+
expect(any.cubic_meters).toBeGreaterThanOrEqual(0)
24+
expect(any.cubic_feet).toBeGreaterThanOrEqual(0)
25+
return true
26+
}
27+
28+
toString() {
29+
return "SpacexVolume"
30+
}
31+
32+
getExpectedType() {
33+
return "object"
34+
}
35+
36+
toAsymmetricMatcher() {
37+
return "SpacexVolume"
38+
}
39+
}
40+
41+
/**
42+
* Expect metric and imperial length (or dimension) numbers in object.
43+
*/
44+
class SpacexLength extends CustomAsymmetricMatcher {
45+
asymmetricMatch(any) {
46+
expect(any).toEqual(expect.anything())
47+
expect(any).toHaveProperty("meters", expect.any(Number))
48+
expect(any).toHaveProperty("feet", expect.any(Number))
49+
expect(any.meters).toBeGreaterThanOrEqual(0)
50+
expect(any.feet).toBeGreaterThanOrEqual(0)
51+
return true
52+
}
53+
54+
toString() {
55+
return "SpacexLength"
56+
}
57+
58+
getExpectedType() {
59+
return "object"
60+
}
61+
62+
toAsymmetricMatcher() {
63+
return "SpacexLength"
64+
}
65+
}
66+
67+
/**
68+
* Expect metric and imperial mass numbers in object.
69+
*/
70+
class SpacexMass extends CustomAsymmetricMatcher {
71+
asymmetricMatch(any) {
72+
expect(any).toEqual(expect.anything())
73+
expect(any).toHaveProperty("kg", expect.any(Number))
74+
expect(any).toHaveProperty("lb", expect.any(Number))
75+
expect(any.kg).toBeGreaterThanOrEqual(0)
76+
expect(any.lb).toBeGreaterThanOrEqual(0)
77+
return true
78+
}
79+
80+
toString() {
81+
return "SpacexMass"
82+
}
83+
84+
getExpectedType() {
85+
return "object"
86+
}
87+
88+
toAsymmetricMatcher() {
89+
return "SpacexMass"
90+
}
91+
}
92+
93+
module.exports = {
94+
volume: () => {
95+
return new SpacexVolume()
96+
},
97+
length: () => {
98+
return new SpacexLength()
99+
},
100+
mass: () => {
101+
return new SpacexMass()
102+
}
103+
}

0 commit comments

Comments
 (0)