Skip to content

Commit 2ad0985

Browse files
authored
Merge pull request #459 from Jlu18/random-color
Remove cursor update from getRandomColor and add setRandomColor function
2 parents 17e6fc2 + 1a9592a commit 2ad0985

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/myr/Myr.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,14 @@ class Myr {
736736
i++;
737737
}
738738
}
739-
this.cursor.color = color;
740739
return color;
741740
}
742741

742+
setRandomColor = (colors = null) => {
743+
this.cursor.color = this.getRandomColor(colors);
744+
return this.cursor.color;
745+
}
746+
743747
drop = (outerElId) => {
744748
this.getEl(outerElId)["dynamic-body"] = "shape: box; mass: 5";
745749
return outerElId;

src/myr/reference.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,14 @@ let transformations = [
266266
{
267267
name: "getRandomColor",
268268
parameters: [{ type: "array", name: "colors" }],
269-
description: <span>The getRandomColor function returns a random color and changes the color of the cursor. If passed an array, getRandomColor will choose randomly from the given colors.</span>,
269+
description: <span>The getRandomColor function returns a random color. If passed an array, getRandomColor will choose randomly from the given colors.</span>,
270270
example: "getRandomColor"
271271
},
272+
{
273+
name: "setRandomColor",
274+
parameters: [{ type: "array", name: "colors" }],
275+
description: <span>The setRandomColor function changes the color of the cursor to a random color. If passed an array, setRandomColor will choose randomly from the given colors.</span>,
276+
},
272277
{
273278
name: "getColor",
274279
parameters: [],

src/tests/Myr.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,35 @@ describe("Updates to Myr's Model", () => {
139139
});
140140

141141
it("to makes Random Color", () => {
142+
myr.cursor.color = null;
142143
let color = myr.getRandomColor();
143144
expect(color).not.toBeUndefined();
145+
expect(myr.cursor.color).toBeNull();
144146
expect(colorRegEx.test(color)).toBeTruthy();
145147
});
146148

149+
it("to set Random Color", ()=> {
150+
myr.cursor.color = null;
151+
myr.setRandomColor();
152+
expect(myr.cursor.color).not.toBeNull();
153+
});
154+
147155
it("pick Random Color out of a list of colors", () => {
148156
let colors = ["blue", "green", "red", "hotpink", "FF00FF", "rgb(100,33,93)"];
157+
myr.cursor.color = null;
149158
let color = myr.getRandomColor(colors);
159+
expect(myr.cursor.color).toBeNull();
150160
expect(color).not.toBeUndefined();
151161
expect(colors.includes(color)).toBeTruthy();
152162
});
163+
164+
it("pick and set Random Color out of a list of colors", () => {
165+
let colors = ["blue", "green", "red", "hotpink", "FF00FF", "rgb(100,33,93)"];
166+
myr.cursor.color = null;
167+
myr.setRandomColor(colors);
168+
expect(myr.cursor.color).not.toBeNull();
169+
expect(colors.includes(myr.cursor.color)).toBeTruthy();
170+
});
153171
});
154172

155173
describe("Component Renders", () => {

0 commit comments

Comments
 (0)