I am taking over a project from someone and it's been built in React. One of the tests is failing because a function isn't called after a click on a button has been simulated. The function is called ok when I use the app in the browser so not sure what's going on. I'm pretty new to React and haven't written any tests before so there might be something very obvious I'm overlooking.
This is the test:
it('Clicks save entry without meeting validation', function() {
expect(WorkHistoryItemContainer.state.saving).toBe(undefined);
expect(WorkHistoryItemContainer.state.errors.length).toBe(0);
var organisation = React.findDOMNode(WorkHistoryItemContainer).querySelector('input[name=organisation]'),
role = React.findDOMNode(WorkHistoryItemContainer).querySelector('input[name=role]'),
date = React.findDOMNode(WorkHistoryItemContainer).querySelector('input[name=period]'),
duration = React.findDOMNode(WorkHistoryItemContainer).querySelector('input[name=duration]');
organisation.value = mockValues.organisation;
role.value = mockValues.role;
date.value = mockValues.organisation;
duration.value = mockValues.duration;
console.log('----------------');
console.log(organisation.value);
ReactTestUtils.Simulate.change(organisation);
ReactTestUtils.Simulate.change(role);
ReactTestUtils.Simulate.change(date);
ReactTestUtils.Simulate.change(duration);
ReactTestUtils.Simulate.blur(organisation);
ReactTestUtils.Simulate.blur(role);
ReactTestUtils.Simulate.blur(date);
ReactTestUtils.Simulate.blur(duration);
console.log('----------------');
console.log(organisation.value);
var SaveButton = React.findDOMNode(WorkHistoryItemContainer).querySelectorAll('.Form-link')[1];
expect(SaveButton.innerText).toBe('Save');
console.log('----------------');
console.log(SaveButton);
ReactTestUtils.Simulate.click(SaveButton);
// THIS LINE FAILS:
expect(WorkHistoryItemContainer.handleSave).toHaveBeenCalled();
expect(WorkHistoryItemContainer.state.saving).toBe(true);
expect(spyFunc.handleSave).toHaveBeenCalled();
});
this is the handleSave function in case it is relevant:
handleSave: function (ev, eventAttrs) {
var isNew = this.state.id === '';
this.setState({
editing: false,
saving: true
});
this.props.saveEntry(ev, eventAttrs, this);
},
The error I'm getting in the console is:
PhantomJS 1.9.8 (Linux) Rendering a new work history item Clicks save entry without meeting validation FAILED
Expected spy handleSave to have been called.
at /tmp/d89697d9775596afa26fe8390227c689.browserify:3044
Expected undefined to be true.
at /tmp/d89697d9775596afa26fe8390227c689.browserify:3049
Expected spy handleSave to have been called.
at /tmp/d89697d9775596afa26fe8390227c689.browserify:3051
Any ideas? I'm not really sure where to start... I logged some stuff in the consle and it looks fine (i.e. there's a button etc.)