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