Created
December 26, 2017 21:53
-
-
Save toboid/d5d905c29fd41348abddbae92cd7db66 to your computer and use it in GitHub Desktop.
Using ReactTestUtils.renderIntoDocument and ReactTestUtils.Simulate
This file contains hidden or 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
import React from "react"; | |
import ReactTestUtils from "react-dom/test-utils"; | |
const PageLink = ({ page, onClick }) => ( | |
<a href={`?page=${page}`} onClick={onClick}> | |
Page {page} | |
</a> | |
); | |
// ReactTestUtils.renderIntoDocument doesn't work with functional components | |
class App extends React.Component { | |
render() { | |
const { onClick } = this.props; | |
return ( | |
<div> | |
<PageLink page={2} onClick={onClick} /> | |
</div> | |
); | |
} | |
} | |
it("calls click handler", () => { | |
const onClick = jest.fn(); | |
const component = ReactTestUtils.renderIntoDocument( | |
<App onClick={onClick} /> | |
); | |
const [page2] = ReactTestUtils.findAllInRenderedTree( | |
component, | |
e => e && e.tagName === "A" && e.href === "?page=2" | |
); | |
ReactTestUtils.Simulate.click(page2); | |
expect(onClick).toHaveBeenCalled(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment