From 17988ddcc8641a21d2e4ef55aafff20944a52512 Mon Sep 17 00:00:00 2001 From: Gonzalo Alonso Fernandez Date: Mon, 4 Mar 2024 14:34:59 +0100 Subject: [PATCH] feat: creating the tests for the dashboard. --- webapp/src/pages/Rules.jsx | 3 +- webapp/src/tests/Dashboard.test.js | 52 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 webapp/src/tests/Dashboard.test.js diff --git a/webapp/src/pages/Rules.jsx b/webapp/src/pages/Rules.jsx index e0fb6fc6..c0838a14 100644 --- a/webapp/src/pages/Rules.jsx +++ b/webapp/src/pages/Rules.jsx @@ -1,9 +1,8 @@ import { Center } from "@chakra-ui/layout"; -import { Text, Grid, Flex, Heading, Button, Box } from "@chakra-ui/react"; +import { Text, Flex, Heading, Button, Box } from "@chakra-ui/react"; import React from "react"; import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; -import ButtonEf from '../components/ButtonEf'; export default function Rules() { const navigate = useNavigate(); diff --git a/webapp/src/tests/Dashboard.test.js b/webapp/src/tests/Dashboard.test.js new file mode 100644 index 00000000..3e0afbea --- /dev/null +++ b/webapp/src/tests/Dashboard.test.js @@ -0,0 +1,52 @@ +import React from 'react'; +import { render, fireEvent, screen, waitFor, act, getByLabelText, getByTestId } from '@testing-library/react'; +import axios from 'axios'; +import MockAdapter from 'axios-mock-adapter'; +import Login from '../pages/Login'; +import { MemoryRouter, createMemoryRouter } from 'react-router'; +import router from '../components/Router'; +import Dashboard from '../pages/Dashboard'; + +const mockAxios = new MockAdapter(axios); +const mockRouter = createMemoryRouter(router); + +describe('Dashboard component', () => { + it('renders dashboard elements correctly', async () => { + const { getByText } = render(); + + // Check if the heading is rendered + expect(getByText("common.dashboard")).toBeInTheDocument(); + + // Check if the buttons are rendered + expect(screen.getByTestId('Rules')).toBeInTheDocument(); + expect(screen.getByTestId('Play')).toBeInTheDocument(); + expect(screen.getByTestId('Statistics')).toBeInTheDocument(); + + // Check if the logout button is rendered + expect(screen.getByText(/logout/i)).toBeInTheDocument(); + }); + + it('navigates to the rules route on button click', () => { + // Render the component + render(); + + const rulesButton = screen.getByTestId('Rules'); + fireEvent.click(rulesButton); + + // Check the change of path + expect(screen.getByText("common.rules")).toBeInTheDocument(); + }); + + it('do not navigates to the statistics route on button click', () => { + // Render the component + render(); + + const statisticsButton = screen.getByTestId('Statistics'); + fireEvent.click(statisticsButton); + + // Check the change of path + expect(screen.getByText("common.dashboard")).toBeInTheDocument(); + }); + + // Test the play and log out buttons. +});