Skip to content

Commit 622bf1b

Browse files
committed
fix: use throw and segregate the test cases
1 parent ac30336 commit 622bf1b

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Conversions/RgbHslConversion.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
const checkRgbFormat = (colorRgb) => colorRgb.every(c => c >= 0 && c <= 255);
1919

2020
const rgbToHsl = (colorRgb) => {
21-
if (checkRgbFormat(colorRgb) === false) {
22-
return 'Input is not a valid RGB color.'
23-
}
21+
if (!checkRgbFormat(colorRgb)) {
22+
throw new Error('Input is not a valid RGB color.');
23+
}
2424

2525
let colorHsl = colorRgb
2626

Conversions/test/RgbHslConversion.test.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { rgbToHsl } from '../RgbHslConversion'
22
describe('RgbHslConversion', () => {
3-
const testCases = [
3+
const validTestCases = [
44
[
55
[215, 19, 180],
66
[311, 84, 46]
@@ -28,16 +28,27 @@ describe('RgbHslConversion', () => {
2828
[
2929
[255, 255, 255],
3030
[0, 0, 100]
31-
],
31+
]
32+
]
33+
34+
const errorTestCases = [
3235
[[256, 180, 9], 'Input is not a valid RGB color.'],
3336
[[-90, 46, 8], 'Input is not a valid RGB color.'],
3437
[[1, 39, 900], 'Input is not a valid RGB color.']
3538
]
3639

37-
test.each(testCases)(
40+
41+
test.each(validTestCases)(
3842
'Should return the color in HSL format.',
3943
(colorRgb, expected) => {
4044
expect(rgbToHsl(colorRgb)).toEqual(expected)
4145
}
4246
)
47+
48+
test.each(errorTestCases)(
49+
'Should return the error message.',
50+
(colorRgb, expected) => {
51+
expect(() => rgbToHsl(colorRgb)).toThrowError(expected)
52+
}
53+
)
4354
})

0 commit comments

Comments
 (0)