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:
Ajay A
2025-06-20 16:40:28 +05:30
committed by GitHub
parent 3099a8844c
commit aa78ee52ae

View File

@@ -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