From a25a49a2b8190ed683a9a496ab97984f3195edce Mon Sep 17 00:00:00 2001 From: Sergey Ufocoder Date: Fri, 18 Oct 2019 12:26:42 +0300 Subject: [PATCH 1/3] Extract `colors` util. Use `getRandomColor` --- src/reducers/subnetwork.js | 33 ++------------------------------- src/utils/colors.js | 36 ++++++++++++++++++++++++++++++++++++ src/utils/colors.test.js | 16 ++++++++++++++++ 3 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 src/utils/colors.js create mode 100644 src/utils/colors.test.js diff --git a/src/reducers/subnetwork.js b/src/reducers/subnetwork.js index abfd075..e0b51d6 100644 --- a/src/reducers/subnetwork.js +++ b/src/reducers/subnetwork.js @@ -5,36 +5,7 @@ import { REMOVE_SUPER_NODE, } from 'actions'; -const colorsBuilder = () => { - let list = []; - const allColors = ['#D84315', '#BF360C', '#FBC02D', '#F9A825', '#F57F17', '#FFA000', '#FF8F00', '#FF6F00', '#F57C00', '#EF6C00', '#E65100', '#388E3C', '#2E7D32', '#689F38', '#558B2F', '#AFB42B', '#0288D1', '#0097A7', '#00838F', '#1976D2', '#1565C0', '#C2185B']; - const shuffle = (array) => { - let currentIndex = array.length; - let temporaryValue; - let randomIndex; - - while (currentIndex !== 0) { - randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex -= 1; - - temporaryValue = array[currentIndex]; - array[currentIndex] = array[randomIndex]; - array[randomIndex] = temporaryValue; - } - - return array; - }; - - return () => { - if (list.length === 0) { - list = shuffle([...allColors]); - } - - return list.shift(); - }; -}; - -const colorsNol = colorsBuilder(); +import { getRandomColor } from 'utils/colors'; const formatNetwork = (state) => { const { network, positions, nodes } = state; @@ -44,7 +15,7 @@ const formatNetwork = (state) => { ...network, nodes, positions, - color: colorsNol(), + color: getRandomColor(), }; } diff --git a/src/utils/colors.js b/src/utils/colors.js new file mode 100644 index 0000000..c5680e3 --- /dev/null +++ b/src/utils/colors.js @@ -0,0 +1,36 @@ +export const COLORS = [ + '#D84315', + '#BF360C', + '#FBC02D', + '#F9A825', + '#F57F17', + '#FFA000', + '#FF8F00', + '#FF6F00', + '#F57C00', + '#EF6C00', + '#E65100', + '#388E3C', + '#2E7D32', + '#689F38', + '#558B2F', + '#AFB42B', + '#0288D1', + '#0097A7', + '#00838F', + '#1976D2', + '#1565C0', + '#C2185B', +]; + +const shuffle = array => array.slice().sort(() => Math.random() - 0.5); + +export const getRandomColor = (() => { + let list = []; + return () => { + if (!list.length) { + list = shuffle(COLORS); + } + return list.shift(); + }; +})(); diff --git a/src/utils/colors.test.js b/src/utils/colors.test.js new file mode 100644 index 0000000..0787038 --- /dev/null +++ b/src/utils/colors.test.js @@ -0,0 +1,16 @@ +import { COLORS, getRandomColor } from './colors'; + +describe('Colors Utils', () => { + describe('getRandomColor', () => { + it('returns truthy (keyCode)', () => { + const firstRandomColor = getRandomColor(); + const secondRandomColor = getRandomColor(); + + if (COLORS.length === 1) { + expect(firstRandomColor).toBe(secondRandomColor); + } else { + expect(firstRandomColor).not.toBe(secondRandomColor); + } + }); + }); +}); From 0c2639f0bc6f1ac1990569c8ae8dc4c8577e1cc2 Mon Sep 17 00:00:00 2001 From: Sergey Ufocoder Date: Sun, 20 Oct 2019 17:01:24 +0300 Subject: [PATCH 2/3] Refactor `src/utils/colors` --- src/utils/colors.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/utils/colors.js b/src/utils/colors.js index c5680e3..eb6a35a 100644 --- a/src/utils/colors.js +++ b/src/utils/colors.js @@ -1,3 +1,5 @@ +import { sort, isEmpty } from 'ramda'; + export const COLORS = [ '#D84315', '#BF360C', @@ -23,14 +25,16 @@ export const COLORS = [ '#C2185B', ]; -const shuffle = array => array.slice().sort(() => Math.random() - 0.5); +const shuffle = sort(() => Math.random() - 0.5); -export const getRandomColor = (() => { +const createRandomColorBuilder = () => { let list = []; return () => { - if (!list.length) { + if (isEmpty(list)) { list = shuffle(COLORS); } return list.shift(); }; -})(); +}; + +export const getRandomColor = createRandomColorBuilder(); From f92f1d3e303248594b7f9709b6cd88aa8feeb32a Mon Sep 17 00:00:00 2001 From: Sergey Ufocoder Date: Sun, 20 Oct 2019 17:01:35 +0300 Subject: [PATCH 3/3] Improve test for `src/utils/colors` lib --- src/utils/colors.test.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/utils/colors.test.js b/src/utils/colors.test.js index 0787038..3572d2c 100644 --- a/src/utils/colors.test.js +++ b/src/utils/colors.test.js @@ -1,16 +1,27 @@ +import { times } from 'ramda'; import { COLORS, getRandomColor } from './colors'; describe('Colors Utils', () => { describe('getRandomColor', () => { - it('returns truthy (keyCode)', () => { - const firstRandomColor = getRandomColor(); - const secondRandomColor = getRandomColor(); + beforeAll(() => { + jest.spyOn(Math, 'random').mockReturnValue(0.5); + }); + + afterAll(() => { + Math.random.mockRestore(); + }); + + it('returns "#388E3C" on the first time', () => { + expect(getRandomColor()).toBe('#388E3C'); + }); + + it('returns "#D84315" on the second time', () => { + expect(getRandomColor()).toBe('#D84315'); + }); - if (COLORS.length === 1) { - expect(firstRandomColor).toBe(secondRandomColor); - } else { - expect(firstRandomColor).not.toBe(secondRandomColor); - } + it('returns "#388E3C" on the twenty first time', () => { + times(getRandomColor, COLORS.length - 2); + expect(getRandomColor()).toBe('#388E3C'); }); }); });