How to simulate user input for unit tests

I’m writing unit tests for code mirror editor.

I’m trying to write a unit test, so that when user inputs some value, a certain state is present.

I tried to simulate it by creating this event and dispatching it on view.contentDOM

function keyDownEvent(shift) {
  const event = new CustomEvent('keydown', {bubbles: false, cancelable: true});
  event.shiftKey = shift;
  return event;

and also

function keyType(letter) {
  return new KeyboardEvent('keypress', {
    bubbles: true,
    cancelable: true,
    key: letter,
    keyCode: letter.charCodeAt(0)

But it doesn’t work, it doesn’t change the state.

What almost work is

view.dispatch(dispatch(state.replaceSelection('F'))); // this simulates F key press

but it doesn’t work all the way, because it doesn’t have userEvent set, so when I simulate it like that, your previous soltuion from New lines are lost to history - #3 by Danon doesn’t work.

To simulate Ctrl+Z, I can just call undo from @codemirror/history, to simulate other actions I can just call commands. To simulate pasting stuff, I dispatch CustomEvent with clipboardData and it works. To simulate prepopulating the value in editor I can just set doc, but what to do to simulate real user interaction with the library; without hacks?

I want to write unit tests for the application.