Last active
May 10, 2018 05:49
-
-
Save mjackson/9318e71a912633c9ec97d23f3189d3d4 to your computer and use it in GitHub Desktop.
React TestUtils Simulate regression between 15.6.2 and 16.3.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://unpkg.com/[email protected]/dist/react-with-addons.js"></script> | |
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script> | |
<script src="https://unpkg.com/[email protected]/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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script> | |
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script> | |
<script src="https://unpkg.com/[email protected]/umd/react-dom-test-utils.development.js"></script> | |
<script src="https://unpkg.com/[email protected]/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