|
1 | | -import { getPercentage } from "../getPercentage"; |
| 1 | +import { getPercentage, GetPercentageOptions } from "../getPercentage"; |
2 | 2 |
|
3 | 3 | describe("getPercentage", () => { |
4 | 4 | it("should throw a RangeError if the min is greater than the max", () => { |
5 | 5 | const expected = new RangeError( |
6 | 6 | "A range must have the min value less than the max value" |
7 | 7 | ); |
8 | | - expect(() => getPercentage(0, -100, 0)).toThrow(expected); |
9 | | - expect(() => getPercentage(0, 0, 0)).toThrow(expected); |
10 | | - expect(() => getPercentage(0, -100, 20)).toThrow(expected); |
11 | | - expect(() => getPercentage(0, 0, 20)).toThrow(expected); |
| 8 | + const options1: GetPercentageOptions = { |
| 9 | + min: 0, |
| 10 | + max: -100, |
| 11 | + value: 0, |
| 12 | + }; |
| 13 | + const options2: GetPercentageOptions = { |
| 14 | + min: 0, |
| 15 | + max: 0, |
| 16 | + value: 0, |
| 17 | + }; |
| 18 | + const options3: GetPercentageOptions = { |
| 19 | + min: 0, |
| 20 | + max: -100, |
| 21 | + value: 20, |
| 22 | + }; |
| 23 | + const options4: GetPercentageOptions = { |
| 24 | + min: 0, |
| 25 | + max: 0, |
| 26 | + value: 20, |
| 27 | + }; |
| 28 | + expect(() => getPercentage(options1)).toThrow(expected); |
| 29 | + expect(() => getPercentage(options2)).toThrow(expected); |
| 30 | + expect(() => getPercentage(options3)).toThrow(expected); |
| 31 | + expect(() => getPercentage(options4)).toThrow(expected); |
| 32 | + expect(() => getPercentage({ ...options1, validate: false })).not.toThrow( |
| 33 | + expected |
| 34 | + ); |
| 35 | + expect(() => getPercentage({ ...options2, validate: false })).not.toThrow( |
| 36 | + expected |
| 37 | + ); |
| 38 | + expect(() => getPercentage({ ...options3, validate: false })).not.toThrow( |
| 39 | + expected |
| 40 | + ); |
| 41 | + expect(() => getPercentage({ ...options4, validate: false })).not.toThrow( |
| 42 | + expected |
| 43 | + ); |
12 | 44 | }); |
13 | 45 |
|
14 | 46 | it("should throw a RangeError if the value is not between the min anx max", () => { |
15 | 47 | const expected = new RangeError( |
16 | 48 | "A value must be between the min and max values" |
17 | 49 | ); |
18 | | - expect(() => getPercentage(0, 100, -1)).toThrow(expected); |
19 | | - expect(() => getPercentage(0, 1, -1)).toThrow(expected); |
20 | | - expect(() => getPercentage(0, 1, -0.5)).toThrow(expected); |
| 50 | + const options1: GetPercentageOptions = { |
| 51 | + min: 0, |
| 52 | + max: 100, |
| 53 | + value: -1, |
| 54 | + }; |
| 55 | + const options2: GetPercentageOptions = { |
| 56 | + min: 0, |
| 57 | + max: 1, |
| 58 | + value: -1, |
| 59 | + }; |
| 60 | + const options3: GetPercentageOptions = { |
| 61 | + min: 0, |
| 62 | + max: 1, |
| 63 | + value: -0.5, |
| 64 | + }; |
| 65 | + |
| 66 | + expect(() => getPercentage(options1)).toThrow(expected); |
| 67 | + expect(() => getPercentage(options2)).toThrow(expected); |
| 68 | + expect(() => getPercentage(options3)).toThrow(expected); |
| 69 | + expect(() => getPercentage({ ...options1, validate: false })).not.toThrow( |
| 70 | + expected |
| 71 | + ); |
| 72 | + expect(() => getPercentage({ ...options2, validate: false })).not.toThrow( |
| 73 | + expected |
| 74 | + ); |
| 75 | + expect(() => getPercentage({ ...options3, validate: false })).not.toThrow( |
| 76 | + expected |
| 77 | + ); |
21 | 78 | }); |
22 | 79 |
|
23 | 80 | it("should return the value as a decimal between 0 and 1 representing the current percentage", () => { |
24 | | - expect(getPercentage(0, 100, 20)).toBe(0.2); |
25 | | - expect(getPercentage(0, 10, 3)).toBe(0.3); |
26 | | - expect(getPercentage(0, 1, 0.5)).toBe(0.5); |
| 81 | + expect(getPercentage({ min: 0, max: 100, value: 20 })).toBe(0.2); |
| 82 | + expect(getPercentage({ min: 0, max: 10, value: 3 })).toBe(0.3); |
| 83 | + expect(getPercentage({ min: 0, max: 1, value: 0.5 })).toBe(0.5); |
27 | 84 | }); |
28 | 85 |
|
29 | 86 | it("should always return a positive percentage", () => { |
30 | | - expect(getPercentage(-100, 0, -20)).toBe(0.8); |
31 | | - expect(getPercentage(-10, 0, -3)).toBe(0.7); |
32 | | - expect(getPercentage(-1, 0, -0.5)).toBe(0.5); |
| 87 | + expect(getPercentage({ min: -100, max: 0, value: -20 })).toBe(0.8); |
| 88 | + expect(getPercentage({ min: -10, max: 0, value: -3 })).toBe(0.7); |
| 89 | + expect(getPercentage({ min: -1, max: 0, value: -0.5 })).toBe(0.5); |
33 | 90 |
|
34 | | - expect(getPercentage(-100, 100, 0)).toBe(0.5); |
35 | | - expect(getPercentage(-100, 0, 0)).toBe(1); |
36 | | - expect(getPercentage(-100, 0, -25)).toBe(0.75); |
| 91 | + expect(getPercentage({ min: -100, max: 100, value: 0 })).toBe(0.5); |
| 92 | + expect(getPercentage({ min: -100, max: 0, value: 0 })).toBe(1); |
| 93 | + expect(getPercentage({ min: -100, max: 0, value: -25 })).toBe(0.75); |
37 | 94 | }); |
38 | 95 | }); |
0 commit comments