test(curriculum): reduce react test flakiness (#46615)

This commit is contained in:
Oliver Eyton-Williams
2022-06-23 18:05:22 +02:00
committed by GitHub
parent 81f4feb3be
commit ac7388dd1b

View File

@@ -123,7 +123,7 @@ Each time the button is clicked, the counter state should be incremented by a va
})();
```
When the `GameOfChance` component is first mounted to the DOM and each time the button is clicked thereafter, a single `h1` element should be returned that randomly renders either `You Win!` or `You Lose!`.
When the `GameOfChance` component is first mounted to the DOM and each time the button is clicked thereafter, a single `h1` element should be returned that randomly renders either `You Win!` or `You Lose!`. Note: this can fail randomly. If that happens, please try again.
```js
(() => {
@@ -265,6 +265,11 @@ class GameOfChance extends React.Component {
# --solutions--
```jsx
// We want this to be deterministic for testing purposes.
const randomSequence = [true, false, false, true, true, false, false, true, true, false];
let index = 0;
const fiftyFifty = () => randomSequence[index++ % randomSequence.length];
class Results extends React.Component {
constructor(props) {
super(props);
@@ -290,11 +295,10 @@ class GameOfChance extends React.Component {
});
}
render() {
const expression = Math.random() >= 0.5;
return (
<div>
<button onClick={this.handleClick}>Play Again</button>
<Results fiftyFifty={expression} />
<Results fiftyFifty={fiftyFifty()} />
<p>{'Turn: ' + this.state.counter}</p>
</div>
);