← Back to React Course | Chapter 12: Testing React Applications | Lesson 4 of 9

User Events को Simulate करना

Tests में user events simulate करना ऐसा है जैसे एक robot actor exactly वैसे ही buttons press और keyboard पर type करता है जैसे एक real visitor करेगा, ताकि आप देख सकें आपका app कैसे react करता है।
Syntax
markup
import userEvent from "@testing-library/user-event";

const user = userEvent.setup();
await user.click(screen.getByRole("button", { name: "label" }));
await user.type(screen.getByLabelText("label"), "text");

एक Click Simulate करना

userEvent.click(element) एक real user द्वारा एक element click करने को simulate करता है, events की full sequence सहित जो एक browser actually fire करेगा (pointerdown, mousedown, focus, आदि), ज़्यादा accurately than ज़्यादा basic fireEvent.click, जो सिर्फ एक single click event fire करता है।

Note: हमेशा userEvent methods को await करें — वे Promises return करते हैं real interactions की timing को accurately model करने के लिए।
Warning: Library के newer versions में, आपको पहले userEvent.setup() call करना होगा और returned object उपयोग करना होगा, directly userEvent.click call करने के बजाय।

उदाहरण: Simulating a Click

markup
// Run in your local React project (npm install required)
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Counter from './Counter';

test('increments on click', async () => {
  const user = userEvent.setup();
  render(<Counter />);
  await user.click(screen.getByText('Increment'));
  expect(screen.getByText('Count: 1')).toBeInTheDocument();
});

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

एक Input में Typing Simulate करना

userEvent.type(element, text) एक user को character by character typing simulate करता है, वही sequence of keyboard और input events trigger करते हुए जो एक real keystroke करेगा, जो directly एक input की value set करने से ज़्यादा realistic है।

Note: यह realism उन components के लिए matter करता है जिनकी logic individual keystrokes से जुड़ी है, जैसे एक live character counter या format-as-you-type input।
Warning: इस तरह typing simulated होना genuinely character-by-character है, इसलिए यह tests को directly एक value set करने से slightly slower बना सकता है — usually accuracy के लिए worthwhile tradeoff।

उदाहरण: Simulating Typing into an Input

markup
// Run in your local React project (npm install required)
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import SearchBox from './SearchBox';

test('updates as user types', async () => {
  const user = userEvent.setup();
  render(<SearchBox />);
  await user.type(screen.getByRole('textbox'), 'react');
  expect(screen.getByDisplayValue('react')).toBeInTheDocument();
});

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

एक Full Form Submission Flow Test करना

कई userEvent calls को sequence में combine करना आपको एक complete realistic flow simulate करने देता है — fields में type करना, फिर submit click करना — exactly वही mirror करते हुए जो एक real person करेगा, और resulting behavior verify करते हुए (जैसे एक success message appear होना)।

Note: userEvent calls को await के साथ chain करें हर step के बीच, real order mirror करते हुए जिस order में एक user उन्हें perform करेगा।
Warning: एक multi-step flow में हर userEvent call को await करना भूल जाने से steps एक दूसरे के साथ race कर सकते हैं, flaky, inconsistent test results produce करते हुए।

उदाहरण: Testing a Full Form Submission Flow

markup
// Run in your local React project (npm install required)
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import LoginForm from './LoginForm';

test('submits the login form', async () => {
  const user = userEvent.setup();
  render(<LoginForm />);
  await user.type(screen.getByLabelText('Email'), '[email protected]');
  await user.click(screen.getByRole('button', { name: 'Log In' }));
  expect(await screen.findByText('Welcome back!')).toBeInTheDocument();
});

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Typing के लिए fireEvent उपयोग करना जब userEvent real keystroke-by-keystroke typing behavior को ज़्यादा accurately simulate करता है।
  2. यह भूल जाना कि ज़्यादातर userEvent methods async हैं और await चाहिए।
  3. Library के newer versions में इसे उपयोग करने से पहले userEvent.setup() से userEvent setup न करना।
चैप्टर सारांश
  • @testing-library/user-event real user interactions को ज़्यादा realistically simulate करता है lower-level fireEvent API से।
  • userEvent.click, userEvent.type, और similar methods async हैं और await किए जाने चाहिए।
  • userEvent.setup() एक user-event instance बनाता है, typically एक test के top पर एक बार call किया जाता है।
  • Realistic interactions simulate करना वे bugs पकड़ता है जो एक ज़्यादा artificial fireEvent miss कर सकता है।
ब्राउज़र सपोर्ट

npm install @testing-library/user-event चाहिए — एक Node test environment में run होता है, इस browser sandbox में नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.