Skip to content

Instantly share code, notes, and snippets.

@mjackson
Last active May 10, 2018 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjackson/9318e71a912633c9ec97d23f3189d3d4 to your computer and use it in GitHub Desktop.
Save mjackson/9318e71a912633c9ec97d23f3189d3d4 to your computer and use it in GitHub Desktop.
React TestUtils Simulate regression between 15.6.2 and 16.3.2
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@15.6.2/dist/react-with-addons.js"></script>
<script src="https://unpkg.com/react-dom@15.6.2/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const root = document.getElementById("root");
class Test extends React.Component {
constructor(props) {
super(props);
this.state = { clicked: false };
}
render() {
return (
<div>
<p>
<button onClick={() => this.setState({ clicked: true })}>click me</button>
</p>
<p>{this.state.clicked ? "after" : "before"}</p>
</div>
);
}
}
function assert(what, message) {
if (!what) throw new Error(message);
}
ReactDOM.render(<Test />, root, () => {
const node = root.firstChild;
assert(node.innerHTML.match(/before/), 'HTML should say "before"');
React.addons.TestUtils.Simulate.click(node.querySelector("button"));
// State has been flushed to the DOM. Hooray!
assert(node.innerHTML.match(/after/), 'HTML should say "after"');
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@16.3.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.3.2/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/react-dom@16.3.2/umd/react-dom-test-utils.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const root = document.getElementById("root");
class Test extends React.Component {
constructor(props) {
super(props);
this.state = { clicked: false };
}
render() {
return (
<div>
<p>
<button onClick={() => this.setState({ clicked: true })}>click me</button>
</p>
<p>{this.state.clicked ? "after" : "before"}</p>
</div>
);
}
}
function assert(what, message) {
if (!what) throw new Error(message);
}
ReactDOM.render(<Test />, root, () => {
const node = root.firstChild;
assert(node.innerHTML.match(/before/), 'HTML should say "before"');
ReactTestUtils.Simulate.click(node.querySelector("button"));
// State has not yet been flushed to the DOM, so we have to wrap
// this assertion in a setTimeout in order to make it work :/
assert(node.innerHTML.match(/after/), 'HTML should say "after"');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment