|
| 1 | +import { isSize, Size } from "./size.class"; |
| 2 | + |
| 3 | +describe("Size", () => { |
| 4 | + it("should calculate the correct area of a Size", () => { |
| 5 | + const size = new Size(100, 100); |
| 6 | + const expected = size.width * size.height; |
| 7 | + |
| 8 | + expect(size.area()).toEqual(expected); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should return a proper string representation", () => { |
| 12 | + const size = new Size(100, 100); |
| 13 | + const expected = "(100x100)"; |
| 14 | + |
| 15 | + expect(size.toString()).toEqual(expected); |
| 16 | + }); |
| 17 | + |
| 18 | + describe("isSize typeguard", () => { |
| 19 | + it("should identify a Size object", () => { |
| 20 | + // GIVEN |
| 21 | + const s = new Size(100, 100); |
| 22 | + |
| 23 | + // WHEN |
| 24 | + const result = isSize(s); |
| 25 | + |
| 26 | + // THEN |
| 27 | + expect(result).toBeTruthy(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should rule out non-size objects", () => { |
| 31 | + // GIVEN |
| 32 | + const r = "foo"; |
| 33 | + |
| 34 | + // WHEN |
| 35 | + const result = isSize(r); |
| 36 | + |
| 37 | + // THEN |
| 38 | + expect(result).toBeFalsy(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should rule out possible object with missing properties", () => { |
| 42 | + // GIVEN |
| 43 | + const r = { |
| 44 | + width: 100, |
| 45 | + }; |
| 46 | + |
| 47 | + // WHEN |
| 48 | + const result = isSize(r); |
| 49 | + |
| 50 | + // THEN |
| 51 | + expect(result).toBeFalsy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should rule out possible object with wrong property type", () => { |
| 55 | + // GIVEN |
| 56 | + const r = { |
| 57 | + width: "foo", |
| 58 | + height: 200, |
| 59 | + }; |
| 60 | + |
| 61 | + // WHEN |
| 62 | + const result = isSize(r); |
| 63 | + |
| 64 | + // THEN |
| 65 | + expect(result).toBeFalsy(); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments