I'm trying to write a test for when my button is clicked, that the handleClick function will be called but I keep receiving false in the assertion from my test. Here's the React component:
function Profile() {
const [name, setName] = useState({
firstName: '',
lastName: ''
});
const [names, setNames] = useState([]);
const handleClick = (e) => {
e.preventDefault();
names.push(name);
}
return (
<form >
<input
type = "text"
value = {name.firstName}
onChange = {
e => setName({
...name,
firstName: e.target.value
})
}/>
<input
type = "text"
value = {name.lastName}
onChange = {
e => setName({
...name,
lastName: e.target.value
})
}/>
<button onClick = {handleClick} > Add Name < /button> </form>
)
}
export default Profile
And here's the test:
it('test 1 - call handleClick spy', () => {
const handleClick = sinon.spy();
let wrapper = shallow(<Profile handleClick={handleClick}/>);
wrapper.find('button').simulate('click',{preventDefault:()=>{}});
expect(handleClick.calledOnce).toBe(true);
});