|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | 2 | import { ohm } from "./Challenge41"; |
3 | 3 |
|
| 4 | +const errorMsg = "Invalid values"; |
| 5 | + |
4 | 6 | describe("Reto #41 - LA LEY DE OHM", () => { |
5 | 7 | it("ohm is a function", () => { |
6 | 8 | expect(ohm).toBeTypeOf("function"); |
7 | 9 | }); |
8 | 10 |
|
9 | 11 | it("returns message 'Invalid values' if the provided parameter is not an object", () => { |
10 | | - expect(ohm({ v: 12, r: 220 })).not.toBe("Invalid values"); |
11 | | - expect(ohm()).toBe("Invalid values"); |
12 | | - expect(ohm(1, 2)).toBe("Invalid values"); |
13 | | - expect(ohm([1, 2])).toBe("Invalid values"); |
14 | | - expect(ohm("1, 2")).toBe("Invalid values"); |
| 12 | + expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg); |
| 13 | + expect(ohm()).toBe(errorMsg); |
| 14 | + expect(ohm(1, 2)).toBe(errorMsg); |
| 15 | + expect(ohm([1, 2])).toBe(errorMsg); |
| 16 | + expect(ohm("1, 2")).toBe(errorMsg); |
15 | 17 | }); |
16 | 18 |
|
17 | 19 | it("returns message 'Invalid values' if the provided object has not two and only two properties", () => { |
18 | | - expect(ohm({ v: 12 })).toBe("Invalid values"); |
19 | | - expect(ohm({ v: 12, r: 220 })).not.toBe("Invalid values"); |
20 | | - expect(ohm({ v: 12, r: 220, i: 0.05 })).toBe("Invalid values"); |
21 | | - expect(ohm({ v: 12, r: 220, i: 0.05, p: 0.82 })).toBe("Invalid values"); |
| 20 | + expect(ohm({ v: 12 })).toBe(errorMsg); |
| 21 | + expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg); |
| 22 | + expect(ohm({ v: 12, r: 220, i: 0.05 })).toBe(errorMsg); |
| 23 | + expect(ohm({ v: 12, r: 220, i: 0.05, p: 0.82 })).toBe(errorMsg); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns message 'Invalid values' if any of the keys of the provided objects is not recognized", () => { |
| 27 | + expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg); |
| 28 | + expect(ohm({ r: 220, i: 0.05 })).not.toBe(errorMsg); |
| 29 | + expect(ohm({ v: 12, i: 0.05 })).not.toBe(errorMsg); |
| 30 | + expect(ohm({ i: 0.05, v: 12 })).not.toBe(errorMsg); |
| 31 | + expect(ohm({ v: 12, x: 220 })).toBe(errorMsg); |
| 32 | + expect(ohm({ volt: 12, resistance: 220 })).toBe(errorMsg); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns message 'Invalid values' if any of the values of the provided objects is not a number", () => { |
| 36 | + expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg); |
| 37 | + expect(ohm({ v: "1", r: 220 })).toBe(errorMsg); |
| 38 | + expect(ohm({ v: 12, r: true })).toBe(errorMsg); |
| 39 | + expect(ohm({ v: [1], r: 220 })).toBe(errorMsg); |
| 40 | + expect(ohm({ v: 12, r: { value: 220 } })).toBe(errorMsg); |
| 41 | + expect(ohm({ v: null, r: 220 })).toBe(errorMsg); |
| 42 | + expect(ohm({ v: 12, r: NaN })).toBe(errorMsg); |
22 | 43 | }); |
23 | 44 |
|
24 | | - it.skip("returns message 'Invalid values' if the object provided has not the required shape", () => {}); |
| 45 | + it("returns a number", () => { |
| 46 | + expect(ohm({ v: 12, r: 220 })).toBeTypeOf("number"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns the result of the ohm's law", () => { |
| 50 | + expect(ohm({ v: 12, r: 120 })).toBe(0.1); |
| 51 | + expect(ohm({ v: 12, i: 0.1 })).toBe(120); |
| 52 | + expect(ohm({ i: 0.1, r: 120 })).toBe(12); |
| 53 | + }); |
| 54 | + |
| 55 | + it("the result is rounded to two decimal digits", () => { |
| 56 | + expect(ohm({ v: 12, r: 120 })).toBe(0.1); |
| 57 | + expect(ohm({ v: 12, r: 220 })).toBe(0.05); |
| 58 | + expect(ohm({ v: 12, r: 220 })).not.toBe(12 / 220); |
| 59 | + expect(ohm({ v: 12, i: 5.35 })).toBe(2.24); |
| 60 | + expect(ohm({ v: 12, i: 5.35 })).not.toBe(12 / 5.35); |
| 61 | + }); |
25 | 62 | }); |
0 commit comments