Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There is no direct method to cause a React.ChangeEvent<HTMLInputElement> to occur with no input value as the event is triggered by the user's interaction with the input element.

However, you could simulate the event by creating a new instance of the event with an empty string as the value property and then dispatching it on the input element using the dispatchEvent method.

Here's an example:

const inputElement = document.getElementById('myInput');

const event = new Event('change', { bubbles: true });
Object.defineProperty(event, 'target', {
  value: inputElement,
});

const changeEvent = new InputEvent('input', { inputType: 'insertText', data: '', ...event });
inputElement.dispatchEvent(changeEvent);

Keep in mind that this approach is not a recommended approach, and using synthetic events to trigger changes or events is often an anti-pattern. It's better to adjust your tests to mimic what a user would do to trigger the event.