mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-29 21:03:41 -05:00
feat(curriculum): update lunch picker test to prevent mutation (#60145)
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
This commit is contained in:
@@ -307,29 +307,57 @@ const temp = console.log;
|
||||
|
||||
try {
|
||||
console.log = obj => tempArr.push(obj);
|
||||
|
||||
// check that it correctly outputs the first item
|
||||
Math.random = () => 0;
|
||||
getRandomLunch(testLunches);
|
||||
getRandomLunch([...testLunches]);
|
||||
assert.strictEqual(tempArr[0], `Randomly selected lunch: ${testLunches[0]}`);
|
||||
|
||||
// second item
|
||||
// Second item
|
||||
Math.random = () => 0.4;
|
||||
getRandomLunch(testLunches);
|
||||
getRandomLunch([...testLunches]);
|
||||
assert.strictEqual(tempArr[1], `Randomly selected lunch: ${testLunches[1]}`);
|
||||
|
||||
// third item
|
||||
// Third item
|
||||
Math.random = () => 0.8;
|
||||
getRandomLunch(testLunches);
|
||||
getRandomLunch([...testLunches]);
|
||||
assert.strictEqual(tempArr[2], `Randomly selected lunch: ${testLunches[2]}`);
|
||||
|
||||
} finally {
|
||||
// restore Math.random
|
||||
Math.random = tempRandom;
|
||||
console.log = temp;
|
||||
}
|
||||
```
|
||||
|
||||
The `getRandomLunch` function should not change the array passed to it as argument.
|
||||
|
||||
```js
|
||||
const testLunches = ["Fish", "Fries", "Roast"];
|
||||
let originalLunches = [...testLunches];
|
||||
const tempRandom = Math.random;
|
||||
|
||||
try {
|
||||
Math.random = () => 0;
|
||||
getRandomLunch(testLunches);
|
||||
assert.sameOrderedMembers(testLunches, originalLunches);
|
||||
|
||||
Math.random = () => 0.4;
|
||||
getRandomLunch(testLunches);
|
||||
assert.sameOrderedMembers(testLunches, originalLunches);
|
||||
|
||||
Math.random = () => 0.8;
|
||||
getRandomLunch(testLunches);
|
||||
assert.sameOrderedMembers(testLunches, originalLunches);
|
||||
|
||||
const emptyLunches = [];
|
||||
let originalEmpty = [...emptyLunches];
|
||||
getRandomLunch(emptyLunches);
|
||||
assert.sameOrderedMembers(emptyLunches, originalEmpty);
|
||||
|
||||
} finally {
|
||||
Math.random = tempRandom;
|
||||
}
|
||||
```
|
||||
|
||||
You should define a function `showLunchMenu`.
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user