0

Is it possible to achive this fireEvent.keyDown(document, { key: '/' }); with userEvent?

This test passes:

test('is visible after pressing `/`', () => {
  render( < App / > );

  fireEvent.keyDown(document, {
    key: '/'
  });

  const input = screen.getByRole('textbox');

  expect(input).toBeVisible();
});

But when I try to do the same with userEvent:

test('is visible after pressing `/`', () => {
  render( < App / > );

  // Note: I'm not using `userEvent.type` because you can't type on `document`. Also, using `await` doesn't make the test pass.
  userEvent.keyboard('/')

  const input = screen.getByRole('textbox');

  expect(input).toBeVisible();
});

The test fails.

2

1 Answer 1

1

The / has a special meaning with keyboard: to release a held key.

Try escaping it: userEvent.keyboard('\/')

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I tried userEvent.keyboard('\/') and await userEvent.keyboard('\/'). The test still fails.
I'm confused--the test fails but the answer was accepted? What was the fix or did this work after all?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.