@@ -10,14 +10,30 @@ class CustomAsymmetricMatcher {
1010 // $$typeof is used internally by Jest and just to be sure let's use jest Symbol
1111 this . $$typeof = Symbol . for ( "jest.asymmetricMatcher" )
1212 }
13+
14+ toAsymmetricMatcher ( ) {
15+ return this . toString ( )
16+ }
17+ }
18+
19+ class SpacexComposite extends CustomAsymmetricMatcher {
20+ asymmetricMatch ( any ) {
21+ return any !== undefined && typeof any === "object"
22+ }
23+
24+ getExpectedType ( ) {
25+ return "object"
26+ }
1327}
1428
1529/**
1630 * Expect metric and imperial volume numbers in object.
1731 */
18- class SpacexVolume extends CustomAsymmetricMatcher {
32+ class SpacexVolume extends SpacexComposite {
1933 asymmetricMatch ( any ) {
20- expect ( any ) . toEqual ( expect . anything ( ) )
34+ if ( ! super . asymmetricMatch ( any ) ) {
35+ return false
36+ }
2137 expect ( any ) . toHaveProperty ( "cubic_meters" , expect . any ( Number ) )
2238 expect ( any ) . toHaveProperty ( "cubic_feet" , expect . any ( Number ) )
2339 expect ( any . cubic_meters ) . toBeGreaterThanOrEqual ( 0 )
@@ -28,22 +44,16 @@ class SpacexVolume extends CustomAsymmetricMatcher {
2844 toString ( ) {
2945 return "SpacexVolume"
3046 }
31-
32- getExpectedType ( ) {
33- return "object"
34- }
35-
36- toAsymmetricMatcher ( ) {
37- return "SpacexVolume"
38- }
3947}
4048
4149/**
4250 * Expect metric and imperial length (or dimension) numbers in object.
4351 */
44- class SpacexLength extends CustomAsymmetricMatcher {
52+ class SpacexLength extends SpacexComposite {
4553 asymmetricMatch ( any ) {
46- expect ( any ) . toEqual ( expect . anything ( ) )
54+ if ( ! super . asymmetricMatch ( any ) ) {
55+ return false
56+ }
4757 expect ( any ) . toHaveProperty ( "meters" , expect . any ( Number ) )
4858 expect ( any ) . toHaveProperty ( "feet" , expect . any ( Number ) )
4959 expect ( any . meters ) . toBeGreaterThanOrEqual ( 0 )
@@ -54,22 +64,16 @@ class SpacexLength extends CustomAsymmetricMatcher {
5464 toString ( ) {
5565 return "SpacexLength"
5666 }
57-
58- getExpectedType ( ) {
59- return "object"
60- }
61-
62- toAsymmetricMatcher ( ) {
63- return "SpacexLength"
64- }
6567}
6668
6769/**
6870 * Expect metric and imperial mass numbers in object.
6971 */
70- class SpacexMass extends CustomAsymmetricMatcher {
72+ class SpacexMass extends SpacexComposite {
7173 asymmetricMatch ( any ) {
72- expect ( any ) . toEqual ( expect . anything ( ) )
74+ if ( ! super . asymmetricMatch ( any ) ) {
75+ return false
76+ }
7377 expect ( any ) . toHaveProperty ( "kg" , expect . any ( Number ) )
7478 expect ( any ) . toHaveProperty ( "lb" , expect . any ( Number ) )
7579 expect ( any . kg ) . toBeGreaterThanOrEqual ( 0 )
@@ -80,13 +84,67 @@ class SpacexMass extends CustomAsymmetricMatcher {
8084 toString ( ) {
8185 return "SpacexMass"
8286 }
87+ }
8388
84- getExpectedType ( ) {
85- return "object"
89+ /**
90+ * Expect metric and imperial thrust numbers in object.
91+ */
92+ class SpacexThrust extends SpacexComposite {
93+ asymmetricMatch ( any ) {
94+ if ( ! super . asymmetricMatch ( any ) ) {
95+ return false
96+ }
97+ expect ( any ) . toHaveProperty ( "kN" , expect . any ( Number ) )
98+ expect ( any ) . toHaveProperty ( "lbf" , expect . any ( Number ) )
99+ expect ( any . kN ) . toBeGreaterThanOrEqual ( 0 )
100+ expect ( any . lbf ) . toBeGreaterThanOrEqual ( 0 )
101+ return true
86102 }
87103
88- toAsymmetricMatcher ( ) {
89- return "SpacexMass"
104+ toString ( ) {
105+ return "SpacexThrust"
106+ }
107+ }
108+
109+ /**
110+ * Expect composite payload weight object.
111+ */
112+ class SpacexPayloadWeight extends SpacexComposite {
113+ asymmetricMatch ( any ) {
114+ if ( ! super . asymmetricMatch ( any ) ) {
115+ return false
116+ }
117+ expect ( any ) . toHaveProperty ( "id" , expect . any ( String ) )
118+ expect ( any ) . toHaveProperty ( "name" , expect . any ( String ) )
119+ expect ( any ) . toEqual ( new SpacexMass ( ) )
120+ return true
121+ }
122+
123+ toString ( ) {
124+ return "SpacexPayloadWeight"
125+ }
126+ }
127+
128+ /**
129+ * Expect composite stage information object.
130+ */
131+ class SpacexVehicleStage extends SpacexComposite {
132+ asymmetricMatch ( any ) {
133+ if ( ! super . asymmetricMatch ( any ) ) {
134+ return false
135+ }
136+ // expect(any).toHaveProperty("reusable", expect.any(Boolean)) // TODO missing from some stages
137+ // expect(any).toHaveProperty("engines", expect.any(String)) // TODO inconsistent, sometimes number, sometimes composite
138+ // expect(any).toHaveProperty("fuel_amount_tons", expect.any(Number)) // TODO missing from some stages
139+ expect ( any ) . toHaveProperty ( "burn_time_sec" , expect . any ( Number ) )
140+ // expect(any).toHaveProperty("thrust_sea_level", new SpacexThrust()) // TODO missing from some stages
141+ // expect(any).toHaveProperty("thrust_vacuum", new SpacexThrust()) // TODO missing from some stages
142+ // expect(any).toHaveProperty("payloads", expect.any(String)) // composite object present only in 2nd stage
143+ return true
144+ }
145+
146+ toString ( ) {
147+ return "SpacexPayloadWeight"
90148 }
91149}
92150
@@ -99,5 +157,14 @@ module.exports = {
99157 } ,
100158 mass : ( ) => {
101159 return new SpacexMass ( )
160+ } ,
161+ thrust : ( ) => {
162+ return new SpacexThrust ( )
163+ } ,
164+ payloadWeight : ( ) => {
165+ return new SpacexPayloadWeight ( )
166+ } ,
167+ vehicleStage : ( ) => {
168+ return new SpacexVehicleStage ( )
102169 }
103170}
0 commit comments