From 25ce95a8b30324555127caaac1fff4b224f9850e Mon Sep 17 00:00:00 2001 From: Christian Paul Date: Wed, 17 Oct 2018 22:41:01 -0700 Subject: [PATCH 1/3] Fix: test the variable color --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 94d09cf..b9aade2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -59,7 +59,7 @@ const utils = { hex2rgb: (color) => { if (typeof color !== 'string') return [255, 0, 0]; - if (/^#[a-fA-F0-9]{3,6}$/.test()) { + if (/^#[a-fA-F0-9]{3,6}$/.test(color)) { throw new Error('#{color} isn\'t a supported hex color'); } From 46dd1a985365d8c30e01962577a6a5de80c2f7c7 Mon Sep 17 00:00:00 2001 From: Christian Paul Date: Thu, 18 Oct 2018 00:38:10 -0700 Subject: [PATCH 2/3] Fix: invert test and fix error message --- src/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index b9aade2..6d0701c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -59,8 +59,8 @@ const utils = { hex2rgb: (color) => { if (typeof color !== 'string') return [255, 0, 0]; - if (/^#[a-fA-F0-9]{3,6}$/.test(color)) { - throw new Error('#{color} isn\'t a supported hex color'); + if (!/^#[a-fA-F0-9]{3,6}$/.test(color)) { + throw new Error(`${color} isn't a supported hex color`); } color = color.substr(1); From ac273b240938d23eca761795025a87f3388cff28 Mon Sep 17 00:00:00 2001 From: Christian Paul Date: Thu, 18 Oct 2018 00:38:44 -0700 Subject: [PATCH 3/3] Add tests for utils.hex2rgb --- src/utils.spec.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/utils.spec.js diff --git a/src/utils.spec.js b/src/utils.spec.js new file mode 100644 index 0000000..fe946a9 --- /dev/null +++ b/src/utils.spec.js @@ -0,0 +1,28 @@ +'use strict'; +const utils = require('./utils'); + +describe('utils', () => { + describe('hex2rgb', () => { + test('#ff0000', () => { + expect(utils.hex2rgb('#ff0000')).toEqual([255,0,0]); + }); + test('#ffff00', () => { + expect(utils.hex2rgb('#ffff00')).toEqual([255,255,0]); + }); + test('#0000ff', () => { + expect(utils.hex2rgb('#0000ff')).toEqual([0,0,255]); + }); + test('#112233', () => { + expect(utils.hex2rgb('#112233')).toEqual([17,34,51]); + }); + test('#888', () => { + expect(utils.hex2rgb('#888')).toEqual([136,136,136]); + }); + test('33', () => { + function wrapper() { + utils.hex2rgb('33'); + } + expect(wrapper).toThrowError('isn\'t a supported hex color'); + }); + }); +});