What kind of events are accepted by Enzyme's simulate?

Viewed 113

I know simulate('click') is used to test clicking on a component, but why is that instead of simulate('onClick')? And why does it take 'change' instead of 'onChange'?

'click' and 'change' aren't native html events. The docs say it just takes an ol' string.

   /**
     * Simulate events.
     * Returns itself.
     * @param args?
     */
    simulate(event: string, ...args: any[]): this;

Is there a list of type that is a source of truth for what kind of string the event parameter accepts?

1 Answers

.simulate() will call .simulateEvent() method, and .simulateEvent() will call propFromEvent() function and it will call mapNativeEventNames() to map browser native event name to react event camel-cased name without prefix on. E.g. 'touchstart' will map to 'touchStart'.

Then, propFromEvent() will map the native event name to React element props event camel-cased name with prefix on. E.g. 'click' => 'onClick', 'mouseEnter' => 'onMouseEnter'

So, .simulate(event) accepts browser native event type same with addEventListener. It will help you map to React event props.

Full list of event types: w3c

Related