fix(curriculum): removing long running loop in test by overriding Math.random (#55499)

Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org>
This commit is contained in:
Michael Ehme
2024-07-16 03:27:18 -04:00
committed by GitHub
parent 59037abfb9
commit 198523c778

View File

@@ -45,19 +45,24 @@ assert.strictEqual(scoreSpans[5].innerText, ", score = 0");
You should call your `detectFullHouse` function when your `rollDiceBtn` is clicked.
```js
const counts = (arr) => arr.reduce((counts, num) => {counts[num] = ++counts[num] || 1; return counts}, {});
const isFullHouse = (arr) => arr.includes(3) && arr.includes(2) && arr.length === 2;
// Absurdly high, but will either timeout or bail early when assertion runs.
for (let i = 0; i < 100_000; i++) {
rolls = 0;
// Temporarily modifies Math.random to guarantee full house.
const origMathRandom = Math.random
const myMathRandom = (() => {
let i = 0;
const values = [0, 0, 0, 1 / 6 + 0.000001, 1 / 6 + 0.000001];
return () => values[i++ % 5];
})();
Math.random = myMathRandom;
try {
rollDiceBtn.click();
if (isFullHouse(diceValuesArr)) {
assert.isFalse(scoreInputs[2].disabled);
assert.strictEqual(scoreInputs[2].value, "25");
assert.strictEqual(scoreSpans[2].innerText, ", score = 25");
break;
}
} finally {
Math.random = origMathRandom;
}
assert.isFalse(scoreInputs[2].disabled);
assert.strictEqual(scoreInputs[2].value, "25");
assert.strictEqual(scoreSpans[2].innerText, ", score = 25");
```
# --seed--