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 shuffle color for node 11 #51

Merged
merged 2 commits into from
Oct 21, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __mocks__/shuffle-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = jest.fn(array => array);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"redux-thunk": "^2.1.0",
"remove-accents": "^0.4.2",
"reselect": "^4.0.0",
"shuffle-array": "^1.0.1",
"sugarss": "^2.0.0",
"uuid": "^3.3.2",
"webpack": "^4.41.0",
Expand Down
7 changes: 3 additions & 4 deletions src/utils/colors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sort, isEmpty } from 'ramda';
import { isEmpty } from 'ramda';
import shuffle from 'shuffle-array';

export const COLORS = [
'#D84315',
Expand All @@ -25,13 +26,11 @@ export const COLORS = [
'#C2185B',
];

const shuffle = sort(() => Math.random() - 0.5);

const createRandomColorBuilder = () => {
let list = [];
return () => {
if (isEmpty(list)) {
list = shuffle(COLORS);
list = shuffle([...COLORS]);
}
return list.shift();
};
Expand Down
38 changes: 24 additions & 14 deletions src/utils/colors.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import { times } from 'ramda';
import { COLORS, getRandomColor } from './colors';
import shuffle from 'shuffle-array';
import { getRandomColor } from './colors';

describe('Colors Utils', () => {
describe('getRandomColor', () => {
beforeAll(() => {
jest.spyOn(Math, 'random').mockReturnValue(0.5);
});
describe('When getting the first color', () => {
it('returns "#D84315"', () => {
expect(getRandomColor()).toBe('#D84315');
});

afterAll(() => {
Math.random.mockRestore();
it('calls shuffle method', () => {
expect(shuffle).toHaveBeenCalledTimes(1);
});
});

it('returns "#388E3C" on the first time', () => {
expect(getRandomColor()).toBe('#388E3C');
});
describe('When getting the second color', () => {
it('returns "#BF360C"', () => {
expect(getRandomColor()).toBe('#BF360C');
});

it('returns "#D84315" on the second time', () => {
expect(getRandomColor()).toBe('#D84315');
it('does not call shuffle method', () => {
expect(shuffle).toHaveBeenCalledTimes(1);
});
});

it('returns "#388E3C" on the twenty first time', () => {
times(getRandomColor, COLORS.length - 2);
expect(getRandomColor()).toBe('#388E3C');
describe('When getting the 23˚ color', () => {
beforeEach(() => {
times(getRandomColor, 21);
});

it('calls shuffle method', () => {
expect(shuffle).toHaveBeenCalledTimes(2);
});
});
});
});
20 changes: 4 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,6 @@ acorn@^6.0.1, acorn@^6.0.7:
acorn@^6.2.1:
version "6.3.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.3.0.tgz#0087509119ffa4fc0a0041d1e93a417e68cb856e"
integrity sha512-/czfa8BwS88b9gWQVhc8eknunSA2DoJpJyTQkhheIf5E48u1N0R4q/YxxsAeqRrmK9TQ/uYfgLDfZo91UlANIA==

airbnb-prop-types@^2.13.2:
version "2.13.2"
Expand Down Expand Up @@ -1552,7 +1551,6 @@ bluebird@3.5.5:
bluebird@^3.5.5:
version "3.7.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.0.tgz#56a6a886e03f6ae577cffedeb524f8f2450293cf"
integrity sha512-aBQ1FxIa7kSWCcmKHlcHFlT2jt6J/l4FzC7KcPELkOJOsPOb/bccdhmIrKDfXhwFrmc7vDoDrrepFvGqjyXGJg==

bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.8"
Expand Down Expand Up @@ -1730,7 +1728,6 @@ bytes@3.1.0:
cacache@^12.0.2:
version "12.0.3"
resolved "https://registry.yarnpkg.com/cacache/-/cacache-12.0.3.tgz#be99abba4e1bf5df461cd5a2c1071fc432573390"
integrity sha512-kqdmfXEGFepesTuROHMs3MpFLWrPkSSpRqOw80RCflZXy/khxaArvFrQ7uJxSUduzAufc6G0g1VUCOZXxWavPw==
dependencies:
bluebird "^3.5.5"
chownr "^1.1.1"
Expand Down Expand Up @@ -1905,7 +1902,6 @@ chownr@^1.1.1:
chrome-trace-event@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/chrome-trace-event/-/chrome-trace-event-1.0.2.tgz#234090ee97c7d4ad1a2c4beae27505deffc608a4"
integrity sha512-9e/zx1jw7B4CO+c/RXoCsfg/x1AfUBioy4owYH0bJprEYAx5hRFLRhWBqHAG57D0ZM4H7vxbP7bPe0VwhQRYDQ==
dependencies:
tslib "^1.9.0"

Expand Down Expand Up @@ -2040,7 +2036,6 @@ commander@^2.11.0, commander@^2.18.0, commander@^2.19.0, commander@~2.20.0:
commander@^2.20.0:
version "2.20.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.1.tgz#3863ce3ca92d0831dcf2a102f5fb4b5926afd0f9"
integrity sha512-cCuLsMhJeWQ/ZpsFTbE765kvVfoeSddc4nU3up4fV+fDBcfUXnbITJ+JzhkdjzOqhURjZgujxaioam4RM9yGUg==

commander@~2.19.0:
version "2.19.0"
Expand Down Expand Up @@ -4080,7 +4075,6 @@ indexof@0.0.1:
infer-owner@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/infer-owner/-/infer-owner-1.0.4.tgz#c4cefcaa8e51051c2a40ba2ce8a3d27295af9467"
integrity sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==

inflight@^1.0.4:
version "1.0.6"
Expand Down Expand Up @@ -5080,7 +5074,6 @@ load-json-file@^4.0.0:
loader-runner@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-2.4.0.tgz#ed47066bfe534d7e84c4c7b9998c2a75607d9357"
integrity sha512-Jsmr89RcXGIwivFY21FcRrisYZfvLMTWx5kOLc+JTxtpBOG6xML0vzbc6SEQG2FO9/4Fc3wW4LVcB5DmGflaRw==

loader-utils@^0.2.16:
version "0.2.17"
Expand Down Expand Up @@ -5613,7 +5606,6 @@ node-int64@^0.4.0:
node-libs-browser@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.1.tgz#b64f513d18338625f90346d27b0d235e631f6425"
integrity sha512-h/zcD8H9kaDZ9ALUWwlBUDo6TKF8a7qBSCSEGfjTVIYeqsioSKaAX+BN7NgiMGp6iSIXZ3PxgCu8KS3b71YK5Q==
dependencies:
assert "^1.1.1"
browserify-zlib "^0.2.0"
Expand Down Expand Up @@ -6120,7 +6112,6 @@ path-browserify@0.0.0:
path-browserify@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.1.tgz#e6c4ddd7ed3aa27c68a20cc4e50e1a4ee83bbc4a"
integrity sha512-BapA40NHICOS+USX9SN4tyhq+A2RrN/Ws5F0Z5aMHDp98Fl86lX8Oti8B7uN93L4Ifv4fHOEA+pQw87gmMO/lQ==

path-case@^2.1.0:
version "2.1.1"
Expand Down Expand Up @@ -7515,6 +7506,10 @@ shellwords@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"

shuffle-array@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/shuffle-array/-/shuffle-array-1.0.1.tgz#c4ff3cfe74d16f93730592301b25e6577b12898b"

signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
Expand Down Expand Up @@ -7620,7 +7615,6 @@ source-map-support@^0.5.6:
source-map-support@~0.5.12:
version "0.5.13"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
integrity sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
Expand Down Expand Up @@ -7995,7 +7989,6 @@ tar@^4:
terser-webpack-plugin@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/terser-webpack-plugin/-/terser-webpack-plugin-1.4.1.tgz#61b18e40eaee5be97e771cdbb10ed1280888c2b4"
integrity sha512-ZXmmfiwtCLfz8WKZyYUuuHf3dMYEjg8NrjHMb0JqHVHVOSkzp3cW2/XG1fP3tRhqEqSzMwzzRQGtAPbs4Cncxg==
dependencies:
cacache "^12.0.2"
find-cache-dir "^2.1.0"
Expand All @@ -8010,7 +8003,6 @@ terser-webpack-plugin@^1.4.1:
terser@^4.1.2:
version "4.3.7"
resolved "https://registry.yarnpkg.com/terser/-/terser-4.3.7.tgz#9dafb4a6730868608c9c7af96d44182f4107b29f"
integrity sha512-rJFxzWIzJdgiOwYIPJHu6L3hDegEYJj2cHuKcngMraUfhMXGDEbok9Tqjw7yxzrU4IagvG74uTEKdiqeG6T7bg==
dependencies:
commander "^2.20.0"
source-map "~0.6.1"
Expand Down Expand Up @@ -8404,7 +8396,6 @@ vm-browserify@0.0.4:
vm-browserify@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.0.tgz#bd76d6a23323e2ca8ffa12028dc04559c75f9019"
integrity sha512-iq+S7vZJE60yejDYM0ek6zg308+UZsdtPExWP9VZoCFCz1zkJoXFnAX7aZfd/ZwrkidzdUZL0C/ryW+JwAiIGw==

w3c-hr-time@^1.0.1:
version "1.0.1"
Expand Down Expand Up @@ -8441,7 +8432,6 @@ warning@^3.0.0:
watchpack@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-1.6.0.tgz#4bc12c2ebe8aa277a71f1d3f14d685c7b446cd00"
integrity sha512-i6dHe3EyLjMmDlU1/bGQpEw25XSjkJULPuAVKCbNRefQVq48yXKUpwg538F7AZTf9kyr57zj++pQFltUa5H7yA==
dependencies:
chokidar "^2.0.2"
graceful-fs "^4.1.2"
Expand Down Expand Up @@ -8540,15 +8530,13 @@ webpack-sources@^1.1.0:
webpack-sources@^1.4.0, webpack-sources@^1.4.1:
version "1.4.3"
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.4.3.tgz#eedd8ec0b928fbf1cbfe994e22d2d890f330a933"
integrity sha512-lgTS3Xhv1lCOKo7SA5TjKXMjpSM4sBjNV5+q2bqesbSPs5FjGmU6jjtBSkX9b4qW87vDIsCIlUPOEhbZrMdjeQ==
dependencies:
source-list-map "^2.0.0"
source-map "~0.6.1"

webpack@^4.41.0:
version "4.41.0"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-4.41.0.tgz#db6a254bde671769f7c14e90a1a55e73602fc70b"
integrity sha512-yNV98U4r7wX1VJAj5kyMsu36T8RPPQntcb5fJLOsMz/pt/WrKC0Vp1bAlqPLkA1LegSwQwf6P+kAbyhRKVQ72g==
dependencies:
"@webassemblyjs/ast" "1.8.5"
"@webassemblyjs/helper-module-context" "1.8.5"
Expand Down