<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md Atikur Rahman</title>
    <description>The latest articles on DEV Community by Md Atikur Rahman (@atiksujon360).</description>
    <link>https://dev.to/atiksujon360</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1076883%2Fd533f6d7-e970-4d95-a3b1-3cc28d3e2136.jpeg</url>
      <title>DEV Community: Md Atikur Rahman</title>
      <link>https://dev.to/atiksujon360</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atiksujon360"/>
    <language>en</language>
    <item>
      <title>React Testing with UserEvent and Testing Library</title>
      <dc:creator>Md Atikur Rahman</dc:creator>
      <pubDate>Mon, 07 Oct 2024 16:17:24 +0000</pubDate>
      <link>https://dev.to/atiksujon360/react-testing-with-userevent-and-testing-library-41i4</link>
      <guid>https://dev.to/atiksujon360/react-testing-with-userevent-and-testing-library-41i4</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;In this blog, we will learn how to test a React application using the userEvent and testing-library&lt;/em&gt;. We will test a UserForm component that has two input fields for a username and  email and a submit button. We will test the input fields and the submit button." 
&lt;/h2&gt;

&lt;p&gt;*&lt;em&gt;tags: ["React", "Testing", "userEvent", "testing-library"]  *&lt;/em&gt;&lt;br&gt;
In this blog, we will learn how to test a React application using the userEvent and testing-library. We will test a UserForm component that has two input fields for a username and  email and a submit button. We will test the input fields and the submit button.&lt;/p&gt;



&lt;blockquote&gt;
&lt;p&gt;First, let's import the necessary libraries and components&lt;br&gt;
:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pmrhccdi0d2f8jx8th9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pmrhccdi0d2f8jx8th9.png" alt="Image description"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;Next, let's write a test to check the rendering of the UserForm component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('renders UserForm component', () =&amp;gt; {
     render(&amp;lt;UserForm /&amp;gt;);
     const button = screen.getByText('Submit');
     expect(button).toBeInTheDocument();
     });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's write a test to check the input fields and the submit button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('test using data-testid', () =&amp;gt; {
     const  handleUserAdd = jest.fn();
     render(&amp;lt;UserForm onUserAdd={handleUserAdd} /&amp;gt;);
     const username = screen.getByTestId('username');
     const email = screen.getByTestId('email');
      fireEvent.change(username, { target: { value: 'Atik' } });
      fireEvent.change(email, { target: { value: 'atiksujon7@gmail.com' } });
      expect(username.value).toBe('Atik');
      expect(email.value).toBe('atiksujon7@gmail.com');
      const submit = screen.getByRole('button');
      fireEvent.click(submit);
       expect(handleUserAdd).toHaveBeenCalledTimes(1);
     });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above test, we first render the UserForm component and then get the username and email input fields using their test-ids. We  simulate typing in the fields and then click the submit button. We check if the onUserAdd function has been called once.&lt;/p&gt;

&lt;p&gt;Finally, let's write another test using keyboard and click:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('test using keyboard and click', () =&amp;gt; {
  const handleUserAdd = jest.fn();
  render(&amp;lt;UserForm onUserAdd={handleUserAdd} /&amp;gt;);

  //Find the tow inputs
  const [nameInput, emailInput] = screen.getAllByRole('textbox');

  //simulate typng in a name
  userEvent.click(nameInput);
  userEvent.keyboard('Rahman');

  //simulate typing in an email

  userEvent.click(emailInput);
  userEvent.keyboard('atiksujon7@gmail.com');

  //Find the submit button
  const submitButton = screen.getByText('Submit');
  userEvent.click(submitButton);

  expect(handleUserAdd).toHaveBeenCalledTimes(1);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this  test, we use the userEvent library to simulate user interactions. We first click on the name input field and then type 'Rahman' using the keyboard method. We do the same for the email input field. After typing, we click the submit button. We check if the onUserAdd  function has been called once.&lt;/p&gt;

&lt;p&gt;In conclusion, we have successfully tested a React application using the userEvent and testing-library. We tested the input fields and the submit button of a UserForm component. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
