-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): Update getPercentage to optionally not throw errors
- Loading branch information
Showing
5 changed files
with
130 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,95 @@ | ||
import { getPercentage } from "../getPercentage"; | ||
import { getPercentage, GetPercentageOptions } from "../getPercentage"; | ||
|
||
describe("getPercentage", () => { | ||
it("should throw a RangeError if the min is greater than the max", () => { | ||
const expected = new RangeError( | ||
"A range must have the min value less than the max value" | ||
); | ||
expect(() => getPercentage(0, -100, 0)).toThrow(expected); | ||
expect(() => getPercentage(0, 0, 0)).toThrow(expected); | ||
expect(() => getPercentage(0, -100, 20)).toThrow(expected); | ||
expect(() => getPercentage(0, 0, 20)).toThrow(expected); | ||
const options1: GetPercentageOptions = { | ||
min: 0, | ||
max: -100, | ||
value: 0, | ||
}; | ||
const options2: GetPercentageOptions = { | ||
min: 0, | ||
max: 0, | ||
value: 0, | ||
}; | ||
const options3: GetPercentageOptions = { | ||
min: 0, | ||
max: -100, | ||
value: 20, | ||
}; | ||
const options4: GetPercentageOptions = { | ||
min: 0, | ||
max: 0, | ||
value: 20, | ||
}; | ||
expect(() => getPercentage(options1)).toThrow(expected); | ||
expect(() => getPercentage(options2)).toThrow(expected); | ||
expect(() => getPercentage(options3)).toThrow(expected); | ||
expect(() => getPercentage(options4)).toThrow(expected); | ||
expect(() => getPercentage({ ...options1, validate: false })).not.toThrow( | ||
expected | ||
); | ||
expect(() => getPercentage({ ...options2, validate: false })).not.toThrow( | ||
expected | ||
); | ||
expect(() => getPercentage({ ...options3, validate: false })).not.toThrow( | ||
expected | ||
); | ||
expect(() => getPercentage({ ...options4, validate: false })).not.toThrow( | ||
expected | ||
); | ||
}); | ||
|
||
it("should throw a RangeError if the value is not between the min anx max", () => { | ||
const expected = new RangeError( | ||
"A value must be between the min and max values" | ||
); | ||
expect(() => getPercentage(0, 100, -1)).toThrow(expected); | ||
expect(() => getPercentage(0, 1, -1)).toThrow(expected); | ||
expect(() => getPercentage(0, 1, -0.5)).toThrow(expected); | ||
const options1: GetPercentageOptions = { | ||
min: 0, | ||
max: 100, | ||
value: -1, | ||
}; | ||
const options2: GetPercentageOptions = { | ||
min: 0, | ||
max: 1, | ||
value: -1, | ||
}; | ||
const options3: GetPercentageOptions = { | ||
min: 0, | ||
max: 1, | ||
value: -0.5, | ||
}; | ||
|
||
expect(() => getPercentage(options1)).toThrow(expected); | ||
expect(() => getPercentage(options2)).toThrow(expected); | ||
expect(() => getPercentage(options3)).toThrow(expected); | ||
expect(() => getPercentage({ ...options1, validate: false })).not.toThrow( | ||
expected | ||
); | ||
expect(() => getPercentage({ ...options2, validate: false })).not.toThrow( | ||
expected | ||
); | ||
expect(() => getPercentage({ ...options3, validate: false })).not.toThrow( | ||
expected | ||
); | ||
}); | ||
|
||
it("should return the value as a decimal between 0 and 1 representing the current percentage", () => { | ||
expect(getPercentage(0, 100, 20)).toBe(0.2); | ||
expect(getPercentage(0, 10, 3)).toBe(0.3); | ||
expect(getPercentage(0, 1, 0.5)).toBe(0.5); | ||
expect(getPercentage({ min: 0, max: 100, value: 20 })).toBe(0.2); | ||
expect(getPercentage({ min: 0, max: 10, value: 3 })).toBe(0.3); | ||
expect(getPercentage({ min: 0, max: 1, value: 0.5 })).toBe(0.5); | ||
}); | ||
|
||
it("should always return a positive percentage", () => { | ||
expect(getPercentage(-100, 0, -20)).toBe(0.8); | ||
expect(getPercentage(-10, 0, -3)).toBe(0.7); | ||
expect(getPercentage(-1, 0, -0.5)).toBe(0.5); | ||
expect(getPercentage({ min: -100, max: 0, value: -20 })).toBe(0.8); | ||
expect(getPercentage({ min: -10, max: 0, value: -3 })).toBe(0.7); | ||
expect(getPercentage({ min: -1, max: 0, value: -0.5 })).toBe(0.5); | ||
|
||
expect(getPercentage(-100, 100, 0)).toBe(0.5); | ||
expect(getPercentage(-100, 0, 0)).toBe(1); | ||
expect(getPercentage(-100, 0, -25)).toBe(0.75); | ||
expect(getPercentage({ min: -100, max: 100, value: 0 })).toBe(0.5); | ||
expect(getPercentage({ min: -100, max: 0, value: 0 })).toBe(1); | ||
expect(getPercentage({ min: -100, max: 0, value: -25 })).toBe(0.75); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters