Fundamentos del Testing en React
Importancia del Testing en el Desarrollo de Software
Configuraci贸n del entorno de testing en React
Introducci贸n a los Tests:Tu primer test en React
Estrategias de Testing en React
Testing de Componentes UI: Conceptos B谩sicos
Table Driven Testing en React
Desarrollo Guiado por Pruebas (TDD) en React
Mocks en Testing
Testing de Flujos de Usuario en React
Testing a un Proceso de Autenticaci贸n (Parte 1)
Testing a un Proceso de Autenticaci贸n (Parte 2)
Testing de Gesti贸n de Datos con Mocks
Testing de Componentes Condicionales con data-testid
Testing de Hooks en React
SOLID y Refactorizaci贸n de Hooks
Testing de Hooks con Mocks
Testing de Hooks con Spies
Pruebas de Integraci贸n y APIs en React
Mock Service Worker (MSW)
Testing de APIs Externas con MSW (Parte 1)
Testing de APIs Externas con MSW (Parte 2)
Reflexiones sobre Testing en React
Innovaci贸n en Testing con IA
Test Coverage en React
Estrategias de Testing: Cu谩ndo no debes hacer Testing
You don't have access to this class
Keep learning! Join and start boosting your career
Welcome to a session where we will explore how to ensure the quality of our application through tests on the dashboard, the core where all the magic of orders is displayed! Testing our functionalities is crucial to deliver a robust product. In this session, we will use a simulated environment to help us validate that our orders are displayed correctly.
To start, make sure you have your test environment set up correctly. The first step is to familiarize ourselves with the package.json
, which details how our simulated backend runs on port 3001. There, a db.json
file contains an array of commands that we must simulate, also known as "mocking".
src/containers/orders
, we create a new file named orders.test.tsx
.describe
, it
, and expect
pattern to organize our test cases.vitest
and testing-library/react
such as render
and screen
.import { render, screen } from '@testing-library/react';import { MemoryRouter } from 'react-router-dom';import Orders from './Orders';
// Our test suitedescribe('Orders Component', () => { it('should show orders', () => { // Test logic will be implemented here });});
Import and initial configuration:
Orders
component and other functions relevant to testing.MemoryRouter
and SessionProvider
to simulate the structure of our application.Additional configuration:
handleRenderOrders
that will use render
to assemble our component.vi
to create the necessary function mocks like getOrders
.const handleRenderOrders = () => { render( <MemoryRouter> <SessionProvider> <Orders/> </SessionProvider> </MemoryRouter> );};
To test effectively, we need to ensure that the correct data is being used in each test.
mockOrders
and assign an array of mock orders to it.const mockOrders = [ { id: 1, product: 'Example product', quantity: 2, },];
useSession
function to simulate that a user is already logged in.vi.mock('path/to/auth/context', () => ({. ..jest.requireActual('path/to/auth/context'), useSession: jest.fn().mockReturnValue({ user: { role: 'Visualizer' }, }),}),}));
With all the structure and mocks ready, it's time to check that everything is working correctly.
getAllByRole
to get all the orders in the component, making sure that their length matches that of mockOrders
.const orders = screen.getAllByRole('heading', { level: 3 });expect(orders).toHaveLength(mockOrders.length);
And there you have it! You have mastered the ability to perform effective tests in a simulated environment, ensuring that the orders on our dashboard are displayed as they should be. This knowledge is essential to maintain the integrity of the application as it evolves. Keep practicing and you'll see your testing skills become more and more solid!
Contributions 3
Questions 1
Want to see more contributions, questions and answers from the community?