From 4d406b2ceb2f5506edc05e6a3d45a9bb1b1e1c84 Mon Sep 17 00:00:00 2001 From: Lara Date: Tue, 9 Apr 2024 21:09:09 +0200 Subject: [PATCH] Pruebas TDD de Home al 100% --- webapp/src/components/Pages/Home.test.js | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 webapp/src/components/Pages/Home.test.js diff --git a/webapp/src/components/Pages/Home.test.js b/webapp/src/components/Pages/Home.test.js new file mode 100644 index 0000000..64d220e --- /dev/null +++ b/webapp/src/components/Pages/Home.test.js @@ -0,0 +1,64 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import Home from './Home'; +import { BrowserRouter as Router } from "react-router-dom"; + +const mockNavigate = jest.fn(); + +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useNavigate: () => mockNavigate, + })); + +describe('Home component', () => { + it('renders "INICIA SESIÓN" and "REGÍSTRATE" buttons when not logged in', () => { + const { getByText } = render(); + + // Verifica que el título del juego esté presente + expect(getByText('WIQ 5A')).toBeInTheDocument(); + + // Verifica que los botones "INICIA SESIÓN" y "REGÍSTRATE" estén presentes + expect(getByText('INICIA SESIÓN')).toBeInTheDocument(); + expect(getByText('REGÍSTRATE')).toBeInTheDocument(); + }); + + it('renders "JUGAR" and "ESTADÍSTICAS" buttons when logged in', () => { + const { getByText } = render(); + + // Verifica que el título del juego esté presente + expect(getByText('WIQ 5A')).toBeInTheDocument(); + + // Verifica que los botones "JUGAR" y "ESTADÍSTICAS" estén presentes + expect(getByText('JUGAR')).toBeInTheDocument(); + expect(getByText('ESTADÍSTICAS')).toBeInTheDocument(); + }); + + it('clicking "INICIA SESIÓN" button navigates to "/login"', () => { + const { getByText } = render(); + const button = getByText('INICIA SESIÓN'); + fireEvent.click(button); + expect(mockNavigate).toHaveBeenCalledWith('/login'); + }); + + it('clicking "REGÍSTRATE" button navigates to "/register"', () => { + const { getByText } = render(); + const button = getByText('REGÍSTRATE'); + fireEvent.click(button); + expect(mockNavigate).toHaveBeenCalledWith('/register'); + }); + + it('clicking "JUGAR" button navigates to "/game"', () => { + const { getByText } = render(); + const button = getByText('JUGAR'); + fireEvent.click(button); + expect(mockNavigate).toHaveBeenCalledWith('/game'); + }); + + it('clicking "ESTADÍSTICAS" button navigates to "/stats"', () => { + const { getByText } = render(); + const button = getByText('ESTADÍSTICAS'); + fireEvent.click(button); + expect(mockNavigate).toHaveBeenCalledWith('/stats'); + }) + +}); \ No newline at end of file