score:1

Accepted answer

found the solution in here: enzyme simulate an onchange event

test('input should call handlechange on change event', () => {

  const event = {target: {name: 'username', value: 'usertest'}};
  const login = mount(<login />);
  const handlechange = jest.spyon(login.instance(), 'handlechange');
  login.update(); // <--- needs this to force re-render
  const userinput = login.find('.username');

  userinput.simulate('change', event);

  expect(handlechange).tobecalled();

})

it needed this login.update(); in order to work!

thank everyone for your help!

score:0

handlechange isn't currently being mocked. a couple of approaches:

pass change event handler as prop to login component.

<div classname="input-group">
  <input 
    onchange={this.props.handlechange} 
    value={this.state.username}
    classname="form-control login__input username" 
    type="text"
    placeholder="user name"
    name={'username'}
    autofocus
    />
</div>

login.spec.js

...
const handlechange = jest.fn();
const login = mount(<login handlechange={handlechange}/>);
...

replace handlechange with the mock function.

...
const handlechange = jest.fn();
const login = mount(<login />);
login['handlechange'] = handlechange // replace instance
...
expect(handlechange).tobecalled();

use jest spyon to create a mock function that wraps the original function.

...
const handlechange = jest.spyon(object, 'handlechange') // will call the original method
expect(handlechange).tobecalled();

replace handlechange on the login component with a mock function. ... const handlechange = jest.spyon(object, 'handlechange').mock // will call the original method expect(handlechange).tobecalled();

score:1

yes, you'll need to pass an event object to you simulate function.

  const event = {target: {name: "special", value: "party"}};

  element.simulate('change', event);

edit: oh, and you'll also need to do something like:

jest.spyon(login.instance(), 'handlechange')

but that's unrelated to your error


Related Query

More Query from same tag