Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Second wave of assertions. #58

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 93 additions & 26 deletions test/utilities/custom-asymmetric-matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ class CustomAsymmetricMatcher {
// $$typeof is used internally by Jest and just to be sure let's use jest Symbol
this.$$typeof = Symbol.for("jest.asymmetricMatcher")
}

toAsymmetricMatcher() {
return this.toString()
}
}

class SpacexComposite extends CustomAsymmetricMatcher {
asymmetricMatch(any) {
return any !== undefined && typeof any === "object"
}

getExpectedType() {
return "object"
}
}

/**
* Expect metric and imperial volume numbers in object.
*/
class SpacexVolume extends CustomAsymmetricMatcher {
class SpacexVolume extends SpacexComposite {
asymmetricMatch(any) {
expect(any).toEqual(expect.anything())
if (!super.asymmetricMatch(any)) {
return false
}
expect(any).toHaveProperty("cubic_meters", expect.any(Number))
expect(any).toHaveProperty("cubic_feet", expect.any(Number))
expect(any.cubic_meters).toBeGreaterThanOrEqual(0)
Expand All @@ -28,22 +44,16 @@ class SpacexVolume extends CustomAsymmetricMatcher {
toString() {
return "SpacexVolume"
}

getExpectedType() {
return "object"
}

toAsymmetricMatcher() {
return "SpacexVolume"
}
}

/**
* Expect metric and imperial length (or dimension) numbers in object.
*/
class SpacexLength extends CustomAsymmetricMatcher {
class SpacexLength extends SpacexComposite {
asymmetricMatch(any) {
expect(any).toEqual(expect.anything())
if (!super.asymmetricMatch(any)) {
return false
}
expect(any).toHaveProperty("meters", expect.any(Number))
expect(any).toHaveProperty("feet", expect.any(Number))
expect(any.meters).toBeGreaterThanOrEqual(0)
Expand All @@ -54,22 +64,16 @@ class SpacexLength extends CustomAsymmetricMatcher {
toString() {
return "SpacexLength"
}

getExpectedType() {
return "object"
}

toAsymmetricMatcher() {
return "SpacexLength"
}
}

/**
* Expect metric and imperial mass numbers in object.
*/
class SpacexMass extends CustomAsymmetricMatcher {
class SpacexMass extends SpacexComposite {
asymmetricMatch(any) {
expect(any).toEqual(expect.anything())
if (!super.asymmetricMatch(any)) {
return false
}
expect(any).toHaveProperty("kg", expect.any(Number))
expect(any).toHaveProperty("lb", expect.any(Number))
expect(any.kg).toBeGreaterThanOrEqual(0)
Expand All @@ -80,13 +84,67 @@ class SpacexMass extends CustomAsymmetricMatcher {
toString() {
return "SpacexMass"
}
}

getExpectedType() {
return "object"
/**
* Expect metric and imperial thrust numbers in object.
*/
class SpacexThrust extends SpacexComposite {
asymmetricMatch(any) {
if (!super.asymmetricMatch(any)) {
return false
}
expect(any).toHaveProperty("kN", expect.any(Number))
expect(any).toHaveProperty("lbf", expect.any(Number))
expect(any.kN).toBeGreaterThanOrEqual(0)
expect(any.lbf).toBeGreaterThanOrEqual(0)
return true
}

toAsymmetricMatcher() {
return "SpacexMass"
toString() {
return "SpacexThrust"
}
}

/**
* Expect composite payload weight object.
*/
class SpacexPayloadWeight extends SpacexComposite {
asymmetricMatch(any) {
if (!super.asymmetricMatch(any)) {
return false
}
expect(any).toHaveProperty("id", expect.any(String))
expect(any).toHaveProperty("name", expect.any(String))
expect(any).toEqual(new SpacexMass())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought we could just inherit from SpacexMass matcher

return true
}

toString() {
return "SpacexPayloadWeight"
}
}

/**
* Expect composite stage information object.
*/
class SpacexVehicleStage extends SpacexComposite {
asymmetricMatch(any) {
if (!super.asymmetricMatch(any)) {
return false
}
// expect(any).toHaveProperty("reusable", expect.any(Boolean)) // TODO missing from some stages
// expect(any).toHaveProperty("engines", expect.any(String)) // TODO inconsistent, sometimes number, sometimes composite
// expect(any).toHaveProperty("fuel_amount_tons", expect.any(Number)) // TODO missing from some stages
expect(any).toHaveProperty("burn_time_sec", expect.any(Number))
// expect(any).toHaveProperty("thrust_sea_level", new SpacexThrust()) // TODO missing from some stages
// expect(any).toHaveProperty("thrust_vacuum", new SpacexThrust()) // TODO missing from some stages
// expect(any).toHaveProperty("payloads", expect.any(String)) // composite object present only in 2nd stage
return true
}

toString() {
return "SpacexPayloadWeight"
}
}

Expand All @@ -99,5 +157,14 @@ module.exports = {
},
mass: () => {
return new SpacexMass()
},
thrust: () => {
return new SpacexThrust()
},
payloadWeight: () => {
return new SpacexPayloadWeight()
},
vehicleStage: () => {
return new SpacexVehicleStage()
}
}
12 changes: 7 additions & 5 deletions test/v1-all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ test("It should return all vehicle info", () => {
// expect(item).toHaveProperty("diameter", customMatchers.length()) // missing from FH
// expect(item).toHaveProperty("total_width",customMatchers.length()) // missing from falcon1
expect(item).toHaveProperty("mass", customMatchers.mass())
expect(item).toHaveProperty("payload_weights", expect.any(Array)) // TODO test it deeper
expect(item).toHaveProperty("first_stage", expect.any(Object)) // TODO test it deeper
expect(item).toHaveProperty("second_stage", expect.any(Object)) // TODO test it deeper
expect(item).toHaveProperty("payload_weights", expect.any(Array))
item.payload_weights.forEach(weight => {
expect(weight).toEqual(customMatchers.payloadWeight())
})
expect(item).toHaveProperty("first_stage", customMatchers.vehicleStage())
expect(item).toHaveProperty("second_stage", customMatchers.vehicleStage())
// expect(item).toHaveProperty("landing_legs", expect.any(Number)) // missing from falcon1
expect(item).toHaveProperty("description", expect.any(String))
}
Expand All @@ -139,8 +142,7 @@ test("It should return all vehicle info", () => {
expect(item).toHaveProperty("thrusters.fuel_1", expect.any(String))
expect(item).toHaveProperty("thrusters.fuel_2", expect.any(String))
expect(item).toHaveProperty("thrusters.pods", expect.any(Number))
expect(item).toHaveProperty("thrusters.thrust.kN", expect.any(Number))
expect(item).toHaveProperty("thrusters.thrust.lbf", expect.any(Number))
expect(item).toHaveProperty("thrusters.thrust", customMatchers.thrust())
expect(item).toHaveProperty("thrusters.type", expect.any(String))
expect(item).toHaveProperty("launch_payload_mass", customMatchers.mass())
expect(item).toHaveProperty("launch_payload_vol", customMatchers.volume())
Expand Down