Skip to content

Latest commit

 

History

History
 
 

introduction.to.React.Testing.Library

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Ερωτήσεις και σημειώσεις σχετικές με το βίντεο "Εισαγωγή στο Testing στην React με React Testing Library":

Remember: Always break your tests!

Resources and References:

Setup and Teardown: beforeEach(), afterEach(), beforeAll(), afterAll()


Ερωτήσεις:

Σε ποιες περιπτώσεις μπορεί να σπάσει το render(<Component/>) call μέσα στα tests της React Testing Library;

Some common conditions that might break a simple render(<App/>) call include:

  1. Missing Dependencies: If the component being rendered relies on external dependencies (e.g., libraries, APIs, or global objects) that are not available in the testing environment, the render call might fail or produce unexpected results. In such cases, you may need to provide mock data or mock functions to simulate the behavior of those dependencies during testing.

  2. Async Operations: If the component relies on asynchronous operations (e.g., fetching data from an API, using setTimeout, etc.), the render call might not wait for these operations to complete before asserting the component's behavior. This can lead to test failures or false positives. You should use waitFor or other asynchronous testing utilities provided by React Testing Library to handle such cases.

  3. Routing: If the component being tested relies on React Router or any other routing library, the rendering might not work as expected, especially if the routing is not properly configured in the testing environment. Ensure that you set up your testing environment to support routing if your component relies on it.

  4. Context and Providers: Components might rely on React context or external providers to access specific data or functionalities. If these are not properly set up in the test environment, the component's rendering and behavior could be affected. Make sure to provide appropriate context or mock providers as needed.

  5. Unmounted Components: If the component being tested performs operations after unmounting (e.g., clearing timers or subscriptions), it might throw errors or warnings when rendered in isolation. Ensure that you handle cleanup operations properly to avoid test failures.

  6. Incorrect Component Tree: If you render a wrong or incomplete component tree, the behavior might not match your expectations, leading to test failures. Double-check the component tree being rendered to ensure it is correct for your test scenario.


Πότε χρησιμοποιώ το getByTestID στην RTL?

You should use getByTestId in RTL when:

  1. Testing Components with Dynamic Content: When your component's content changes dynamically based on user interactions or data, using getByTestId can be more reliable than searching for elements by text content or class names, which might change frequently.

  2. Testing Components with No Unique Text Content or Class Names: Some components might not have unique text content or class names that you can reliably target. In such cases, using getByTestId allows you to have a stable, test-specific identifier to locate the elements.

  3. Avoiding Test Fragility: Relying on specific text content or class names for testing can lead to fragile tests that break easily when small UI changes occur. By using getByTestId, you establish a stable and less brittle testing interface.

  4. Decoupling Testing from Implementation Details: When testing, you want to focus on the functionality and behavior of your components rather than the specific implementation details. getByTestId helps decouple the testing logic from the implementation details, making your tests more maintainable.

Remember to use data-testid attributes judiciously and avoid overusing them, especially when other querying methods like getByText, getByRole, or getByPlaceholderText can serve the purpose effectively. Using getByTestId sparingly helps maintain the intent and readability of your tests.


Ποιές είναι μερικές καλές πρακτικές κατά τη χρήση των beforeAll() και beforeEach();

To clean up a render(<App />) call that was included in the beforeAll() hook, you need to ensure that you unmount the rendered component after each test to avoid interference between tests. You can achieve this by using the afterEach() hook to perform the cleanup.

In React Testing Library, the render() function returns a RenderResult object that contains the component, as well as a function called unmount(), which can be used to unmount the rendered component and clean up the virtual DOM.

Here's how you can do it:

describe("App Component", () => {
  let renderResult; // Declare a variable to hold the RenderResult

  beforeAll(() => {
    renderResult = render(<App />);
  });

  afterEach(() => {
    // Unmount the rendered component after each test
    renderResult.unmount();
  });

  test("should display the word 'test'", () => {
    // Test logic
    // The component is already rendered, and you can interact with it
  });

  test("should increment the count when the button is clicked", () => {
    // Test logic
    // The component is already rendered, and you can interact with it
  });

  // Add more tests as needed for other scenarios
});

By using renderResult.unmount() in the afterEach() hook, you ensure that the component is unmounted and removed from the virtual DOM after each test is executed. This guarantees a clean slate for each test and prevents any interference or side effects between test cases.

Remember to always clean up after your tests, especially when rendering components in a beforeAll() or beforeEach() hook. Failing to do so might lead to test interdependencies or unexpected behavior.