Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parseToRGB): Not covered invalid input #323

Merged
merged 4 commits into from Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -87,7 +87,7 @@
"eslint": "^4.17.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
"flow-bin": "^0.64.0",
"flow-bin": "^0.75.0",
"flow-copy-source": "^1.2.2",
"flow-coverage-report": "^0.4.1",
"github-slugger": "^1.2.0",
Expand Down
6 changes: 6 additions & 0 deletions src/color/parseToRgb.js
Expand Up @@ -64,6 +64,9 @@ function parseToRgb(color: string): RgbColor | RgbaColor {
const lightness = parseInt(`${hslMatched[3]}`, 10) / 100
const rgbColorString = `rgb(${hslToRgb(hue, saturation, lightness)})`
const hslRgbMatched = rgbRegex.exec(rgbColorString)
if (!hslRgbMatched) {
throw new Error(`Couldn't generate valid rgb string from ${normalizedColor}, it returned ${rgbColorString}.`)
}
return {
red: parseInt(`${hslRgbMatched[1]}`, 10),
green: parseInt(`${hslRgbMatched[2]}`, 10),
Expand All @@ -77,6 +80,9 @@ function parseToRgb(color: string): RgbColor | RgbaColor {
const lightness = parseInt(`${hslaMatched[3]}`, 10) / 100
const rgbColorString = `rgb(${hslToRgb(hue, saturation, lightness)})`
const hslRgbMatched = rgbRegex.exec(rgbColorString)
if (!hslRgbMatched) {
throw new Error(`Couldn't generate valid rgb string from ${normalizedColor}, it returned ${rgbColorString}.`)
}
return {
red: parseInt(`${hslRgbMatched[1]}`, 10),
green: parseInt(`${hslRgbMatched[2]}`, 10),
Expand Down
12 changes: 12 additions & 0 deletions src/color/test/parseToRgb.test.js
Expand Up @@ -40,4 +40,16 @@ describe('parseToRgb', () => {
parseToRgb(12345)
}).toThrow('Passed an incorrect argument to a color function, please pass a string representation of a color.')
})

it('should throw an error if an invalid hsl string is provided', () => {
expect(() => {
parseToRgb('hsl(210,120%,4%)')
}).toThrow(`Couldn't generate valid rgb string from ${'hsl(210,120%,4%)'}, it returned ${'rgb(-2,10,22)'}.`)
})

it('should throw an error if an unparsable hsla string is provided', () => {
expect(() => {
parseToRgb('hsla(210,120%,4%,0.7)')
}).toThrow(`Couldn't generate valid rgb string from ${'hsla(210,120%,4%,0.7)'}, it returned ${'rgb(-2,10,22)'}.`)
})
})
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -3106,9 +3106,9 @@ flow-annotation-check@1.8.0:
glob "7.1.1"
load-pkg "^3.0.1"

flow-bin@^0.64.0:
version "0.64.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.64.0.tgz#ddd3fb3b183ab1ab35a5d5dec9caf5ebbcded167"
flow-bin@^0.75.0:
version "0.75.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.75.0.tgz#b96d1ee99d3b446a3226be66b4013224ce9df260"

flow-copy-source@^1.2.2:
version "1.2.2"
Expand Down