Conteúdo do curso
React.js: Understand and Apply the Lifecycle
Sobre a Aula

Component tests (unit and integration)

Testing components in React is essential to ensure they work correctly and maintain code quality.

Unit Tests

Unit tests verify isolated parts of the code, such as functions or small components, to ensure each unit functions as expected. Use tools like Jest and react-testing-library.

// Example of unit test with Jest
test('sum of 1 + 2 equals 3', () => {
  expect(1 + 2).toBe(3);
});

Integration Tests

Integration tests verify how various parts of the system work together. They ensure components behave correctly when combined. Test interactions between components with real or simulated data.

// Example of integration test with react-testing-library
test('correctly renders the list component', () => {
  render(<List items={['item1', 'item2', 'item3']} />);
  const renderedItems = screen.getAllByRole('listitem');
  expect(renderedItems.length).toBe(3);
});

Testing Tools

Use libraries like Jest, react-testing-library, Enzyme, and Cypress to write and execute tests. These tools provide straightforward methods to simulate events, check states, and validate component outputs.

Benefits of Testing

Tests ensure that changes in the code do not break existing functionalities. They enhance confidence in the code and facilitate maintenance over time.

Conclusion

By implementing unit and integration tests in your React components, you ensure your application functions as expected. Invest time in testing to prevent future issues and maintain software quality.

Entrar na conversa
Rolar para cima