How to use the @testing-library/user-event.click function in @testing-library/user-event

To help you get started, we’ve selected a few @testing-library/user-event examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apollographql / space-kit / src / TextField / TextField.spec.tsx View on Github external
test("when the label is clicked it focuses the input", () => {
  const labelText = "label text";
  const { container, getByLabelText } = render();

  // Use `throw` so TypeScript knows `label` is not null
  const label = container.querySelector("label");
  if (!label) throw new Error("Could not find label");

  const input = getByLabelText(labelText);
  expect(input).not.toHaveFocus();

  userEvent.click(label);
  expect(input).toHaveFocus();
});
github kentcdodds / react-testing-library-course / src / __tests__ / app-03.js View on Github external
test('Can fill out a form across multiple pages', async () => {
  mockSubmitForm.mockResolvedValueOnce({success: true})
  const testData = {food: 'test food', drink: 'test drink'}
  const {findByLabelText, findByText} = render()

  user.click(await findByText(/fill.*form/i))

  user.type(await findByLabelText(/food/i), testData.food)
  user.click(await findByText(/next/i))

  user.type(await findByLabelText(/drink/i), testData.drink)
  user.click(await findByText(/review/i))

  expect(await findByLabelText(/food/i)).toHaveTextContent(testData.food)
  expect(await findByLabelText(/drink/i)).toHaveTextContent(testData.drink)

  user.click(await findByText(/confirm/i, {selector: 'button'}))

  expect(mockSubmitForm).toHaveBeenCalledWith(testData)
  expect(mockSubmitForm).toHaveBeenCalledTimes(1)

  user.click(await findByText(/home/i))

  expect(await findByText(/welcome home/i)).toBeInTheDocument()
})

@testing-library/user-event

Fire events the same way the user does

MIT
Latest version published 10 months ago

Package Health Score

87 / 100
Full package analysis

Popular @testing-library/user-event functions