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
When creating applications, testing the authentication flow is crucial to ensure security and functionality. In this case, we will see how to implement a test for a successful login using the right tools in a React environment.
First, we need to make sure that our login redirects us correctly when we use the correct credentials. In our Platzi Orders application, we want a valid login to navigate to /orders
.
Component Rendering:
SessionProvider
and MemoryRouter
.handleLogin
to avoid repetitive code.const handleLogin = () => { return render( <MemoryRouter> <SessionProvider> <Login/> </SessionProvider> </MemoryRouter> );};
Test Structure:
/orders
.mock
for getAuth
promise.In the test file, we set up the mock and use the following method:
mockGetAuth.mockResolvedValue({ success: true,});
Navigation Mocking:
navigate
function.const mockNavigate = jest.fn();jest.mock('react-router-dom', () => ({. ..jest.requireActual('react-router-dom'), useNavigate: () => mockNavigate,}));
Interactions and Assertions:
waitFor
to handle asynchronous operations.await waitFor(() => { expect(mockNavigate).toHaveBeenCalledWith('/orders');});
It is important to verify that our tests are actually evaluating the expected scenarios. By changing the expected values in our assertions and seeing that the test fails, we can make sure that they are correctly configured.
Verify navigation:
toHaveBeenCalledWith('/orders')
to another such as '/incorrectRoute'
should force a failure, demonstrating that the test is effective.Correct errors:
When developing an application, it is possible to integrate additional functionality to improve the user experience. For example, in our use case, adding a test for the Show
button on the login form would enrich our tests.
Challenge:Create a new use case that verifies the Show
button toggles password visibility. This will allow you to practice creating more complex scenarios and manage variability in interface components.
In summary, performing thorough and well-configured tests on our application ensures that all functionalities, including authentication, work correctly and securely. Keep learning and improving your testing skills!
Contributions 5
Questions 0
Want to see more contributions, questions and answers from the community?