React Basics~unit test/user event

When I test a user event, I use the fireEvent function of the react-testing-library.

・src/Example.js

import Counter from “./components/Counter”;

const Example = () => {
const originCount = 0;

return <Counter originCount={originCount}&…


This content originally appeared on DEV Community and was authored by Ogasawara Kakeru

  • When I test a user event, I use the fireEvent function of the react-testing-library.

・src/Example.js

import Counter from "./components/Counter";

const Example = () => {
  const originCount = 0;

  return <Counter originCount={originCount}></Counter>;
};

export default Example;

・src/commponets/Counter.jsx

import { useState } from "react";

const Counter = (props) => {
  const { originCount } = props;
  const [count, setCount] = useState(originCount);

  const countUp = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <h2>A test of counter</h2>
      <div>
        <span>Current count:{count}</span>
      </div>
      <div>
        <button onClick={countUp}>Countup</button>
      </div>
    </div>
  );
};

export default Counter;

・src/commponets/Counter.test.jsx

import { render, screen, fireEvent } from "@testing-library/react";
import Counter from "./Counter";

test("Whether the current count counts up or not, in case the countUp button is clicked ", () => {
  const { debug } = render(<Counter originCount={0} />);

//test the initial state.
  const spanElBeforUpdate = screen.getByText("Current count:0");
  expect(spanElBeforUpdate).toBeInTheDocument();

//test the user event. In this case, a click event.
  const btn = screen.getByRole("button", { name: "Countup" });
  fireEvent.click(btn);

//test the state which is after clicked button.
  const spanEl = screen.getByText("Current count:1");
  expect(spanEl).toBeInTheDocument();
});

・Success
Image description

・Failure
Image description


This content originally appeared on DEV Community and was authored by Ogasawara Kakeru


Print Share Comment Cite Upload Translate Updates
APA

Ogasawara Kakeru | Sciencx (2024-10-21T02:33:15+00:00) React Basics~unit test/user event. Retrieved from https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/

MLA
" » React Basics~unit test/user event." Ogasawara Kakeru | Sciencx - Monday October 21, 2024, https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/
HARVARD
Ogasawara Kakeru | Sciencx Monday October 21, 2024 » React Basics~unit test/user event., viewed ,<https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/>
VANCOUVER
Ogasawara Kakeru | Sciencx - » React Basics~unit test/user event. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/
CHICAGO
" » React Basics~unit test/user event." Ogasawara Kakeru | Sciencx - Accessed . https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/
IEEE
" » React Basics~unit test/user event." Ogasawara Kakeru | Sciencx [Online]. Available: https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/. [Accessed: ]
rf:citation
» React Basics~unit test/user event | Ogasawara Kakeru | Sciencx | https://www.scien.cx/2024/10/21/react-basicsunit-test-user-event/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.