Giter Site home page Giter Site logo

react-test's Introduction

Let's Learn React Testing

Test File Structure

  1. For each component, create a separate test file with the naming convention componentName.test.js.

  2. Inside the test file, write test functions using the test or it keyword. Each function should have a descriptive name and a callback function containing the tests.

test("Your test name", () => {
  // Test code goes here
});

Test Setup

  1. Ensure compatibility with library versions to avoid any unexpected issues.

  2. Start the callback function with the render method to render the component being tested.

 test("Your test name", () => {
   // Test code goes here
    // render the target component

      render(<App />);

   });
  1. Next step is query and find element (elements) we want to use test on them To query and select a DOM element, there are two popular ways to do it using screen methods like screen.getByRole("..."), screen.getByText("..."), sreen.getByDisplayValue(".."), and more. However, in the case of a Table where you want to test the length of the table body rows to have a specific length, such as 2, it becomes a bit tricky. This is because the length will always include the table header rows, and it's not possible to differentiate between tr elements in the thead and tbody

Solution 1

Using container query selector :

This solution involves destructuring the container from the render method and using container query selectors. You can inspect the rendered DOM using screen.logTestingPlaygroundURL() to find an appropriate query. Here's an example:

 const container = screen.container;
 const firstRowCell = container.querySelector('#sandbox > div > table > tbody');

Solution 2

Using within and data-testid: In your JSX code, add a data-testid attribute to the table body element, such as data-testid="users". Then, in your test function, retrieve the table body rows using the following code:

<tbody data-testid="users">
 const rows = within(screen.getByTestId("users")).getAllByRole("row");

4.Handle User Events (Optional)

5.โ€โ€Expections

Running Tests

To run the tests, use the following command:

npm run test

react-test's People

Contributors

mohammadsobhansaffary avatar

Stargazers

 avatar  avatar sima ziyaei avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.