Reputation: 25
I'm trying to check that that formIsValid
method in my InformationGatheringFormContainer
component is calling one of the components props (isInfoFormValid
) when executing:
export class InformationGatheringFormContainer extends React.Component{
...
formIsValid() {
this.props.isInfoFormValid(this.state.invalid);
}
To do that, I'm using the sinon spy function:
it('formIsValid changes the state', () => {
const mockFunction = sinon.spy();
const baseProps = {
isInfoFormValid: mockFunction,
}
const wrapper = shallow(<InformationGatheringFormContainer {...baseProps} />);
wrapper.instance().formIsValid();
expect(mockFunction).to.have.been.calledOnce.equal(true);
})
I would expect it to work, however this test gives:
AssertionError: expect(received).to.equal(expected)
Expected value to equal:
true
Received:
[Function proxy]
Difference:
Comparing two different types of values. Expected boolean but received function.
So the function call is indeed detected but the .to.have.been.calledOnce
enzyme method does not return a boolean here apparently.
I'm new to Reactjs Unit tests, and I'm a bit lost. How can the return of the .to.have.been.calledOnce
have a different type than boolean ?
Thank you in advance for your help
Upvotes: 0
Views: 907
Reputation: 25
I also found another way to do it:
expect(mockFunction.callCount).toEqual(1);
Upvotes: 1
Reputation: 5944
Looks like calledOnce
is a sinon spy property, not jest's expect.
So, something like:
expect(mockFunction.calledOnce).toEqual(true);
should work (if you prefer sinon
).
Worth to note that jest has his own mocking mechanism:
it('formIsValid changes the state', () => {
const isInfoFormValid = jest.fn();
const baseProps = {
isInfoFormValid,
}
const wrapper = shallow(<InformationGatheringFormContainer {...baseProps} />);
wrapper.instance().formIsValid();
expect(isInfoFormValid).toHaveBeenCalledTimes(1);
})
Upvotes: 0