Enzyme: Which kind of events can I simulate with Enzyme?

Created on 10 Aug 2017  路  7Comments  路  Source: enzymejs/enzyme

I am trying to trigger an event called onConfirm. I am trying to use:
component.find('DateTimePickerModal').simulate('confirm');

However, it is not working. Which kind of event can I simulate? From the API docs, it is not clear to me.

Context:

  • React-Native
  • Jest

Most helpful comment

Generally I'd only expect native events - "confirm" isn't one.

You probably want component.find(DateTimePickerModal).prop('onConfirm')(someFakeEventObject)

All 7 comments

Generally I'd only expect native events - "confirm" isn't one.

You probably want component.find(DateTimePickerModal).prop('onConfirm')(someFakeEventObject)

Since simulate is just a thin wrapper around ReactTestUtils.Simulate, you can reference the React docs on synthetic events for a list of event handlers that React supports: https://facebook.github.io/react/docs/events.html

Otherwise, explicitly calling the handler is your best bet.

@ljharb and @aweary many thanks calling the handler worked ;)

@ljharb @PolGuixe
What did you mean in someFakeEventObject?
Is the component.find(DateTimePickerModal).prop('onConfirm')(someFakeEventObject) supposed to trigger the onConfirm event?

@rahamin1 no, nothing will trigger actual events, but that doesn't matter because you don't need to test the browser itself. that will invoke your onConfirm prop function, which is what you're testing.

@ljharb what is someFakeEventObject? A mock function?

@rahamin1 no, it's an object, that's pretending to be an event object (the thing an event handler would get as its last argument)

Was this page helpful?
0 / 5 - 0 ratings