Umair Umair - 1 year ago 160
React JSX Question

Enzyme: Test fails on BrowserHistory function

I am fairly new to unit tests. I am using react+redux and I have created a

page/component and I am writings its unit test. There's an onClick event that I need to test which is failing.


const GenericNotFound = () => {
const goBack = () => {
return (
<section >
<h1>Sorry. The requested URL is not found</h1>
<a onClick={goBack}>Go back</a>


const wrapper = shallow(<GenericNotFound />);
const browserHistory = {
goBack: sinon.spy()
const onClick = wrapper.find('a').props().onClick;

Even with this, it throws me an error
Cannot read property 'goBack' of undefined

Thank you in advance.

Answer Source

As updated on the comments,

some changes

  1. Use mount instead of shallow

const mount = mount(<GenericNotFound />);

  1. Use spy as below,

sinon.spy(browserHistory.prototype, 'goBack')