Skip to content

Instantly share code, notes, and snippets.

@toboid
Created December 26, 2017 21:53
Show Gist options
  • Save toboid/d5d905c29fd41348abddbae92cd7db66 to your computer and use it in GitHub Desktop.
Save toboid/d5d905c29fd41348abddbae92cd7db66 to your computer and use it in GitHub Desktop.
Using ReactTestUtils.renderIntoDocument and ReactTestUtils.Simulate
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