Skip to content

Commit

Permalink
Tests arreglados
Browse files Browse the repository at this point in the history
  • Loading branch information
CANCI0 committed Apr 10, 2024
1 parent 3a827de commit 4954177
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
56 changes: 45 additions & 11 deletions webapp/src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, fireEvent } from "@testing-library/react";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { BrowserRouter as Router } from "react-router-dom";
import Home from "./pages/Home/Home.js";
import Nav from "./components/Nav/Nav.js";
Expand All @@ -7,7 +7,15 @@ import App from "./App";
import { I18nextProvider } from "react-i18next";
import i18n from "./i18n.js";


describe("App Component", () => {
beforeEach(() => {
Object.defineProperty(window, "localStorage", {
value: { getItem: jest.fn(), setItem: jest.fn(), removeItem: jest.fn() },
writable: true,
});
});

test("renders login page by default", () => {
render(
<I18nextProvider i18n={i18n}>
Expand Down Expand Up @@ -36,13 +44,17 @@ describe("Home Component", () => {

// Verifica el texto de cada enlace
expect(getByRole("button", { name: "Clásico" })).toBeInTheDocument();
expect(getByRole("button", { name: "Batería de sabios" })).toBeInTheDocument();
expect(getByRole("button", { name: "Calculadora humana" })).toBeInTheDocument();
expect(
getByRole("button", { name: "Batería de sabios" })
).toBeInTheDocument();
expect(
getByRole("button", { name: "Calculadora humana" })
).toBeInTheDocument();
});
});

describe("Nav Component", () => {
test("renders Nav component with links and logout button", () => {
test("renders Nav component with links and logout button", async () => {
const { getByText, getByRole } = render(
<I18nextProvider i18n={i18n}>
<Router>
Expand All @@ -68,12 +80,12 @@ describe("Nav Component", () => {
expect(screen.getByText("Amigos")).toBeInTheDocument();

// Verificar que el botón de logout esté presente y que sea un enlace al login
const logoutButton = getByRole("button", { name: /Desconectar/i });
const logoutButton = screen.getByText("testuser");
expect(logoutButton).toBeInTheDocument();
//expect(logoutButton.closest('a')).toHaveAttribute('href', '/login');
});

test("calls localStorage.removeItem when logout button is clicked", () => {
test("calls localStorage.removeItem when logout button is clicked", async () => {
const removeItemMock = jest.fn();
Object.defineProperty(window, "localStorage", {
value: { removeItem: removeItemMock },
Expand All @@ -87,11 +99,8 @@ describe("Nav Component", () => {
</Router>
</I18nextProvider>
);

const logoutButton = screen.getByRole("button", { name: /Desconectar/i });
const logoutButton = screen.getByText("testuser");
fireEvent.click(logoutButton);

expect(removeItemMock).toHaveBeenCalledWith("token");
});

test("navigates to /home when Home button is clicked", () => {
Expand Down Expand Up @@ -148,7 +157,7 @@ describe("Nav Component", () => {
</I18nextProvider>
);

const perfilButton = screen.getByText("Perfil");
const perfilButton = screen.getByText("Mi perfil");
fireEvent.click(perfilButton);

expect(window.location.pathname).toBe("/perfil");
Expand Down Expand Up @@ -183,6 +192,31 @@ describe("Nav Component", () => {

expect(window.location.pathname).toBe("/config");
});

test("navigates to popover options", () => {
render(
<I18nextProvider i18n={i18n}>
<Router>
<Nav />
</Router>
</I18nextProvider>
);

fireEvent.click(screen.getByTestId("classic"));
expect(window.location.pathname).toBe("/home/clasico");

fireEvent.click(screen.getByTestId("battery"));
expect(window.location.pathname).toBe("/home/bateria");

fireEvent.click(screen.getByTestId("calculator"));
expect(window.location.pathname).toBe("/home/calculadora");

fireEvent.click(screen.getByTestId("users"));
expect(window.location.pathname).toBe("/social/usuarios");

fireEvent.click(screen.getByTestId("friends"));
expect(window.location.pathname).toBe("/social/amigos");
});
});
describe("Footer Component", () => {
it("renders footer text correctly", () => {
Expand Down
21 changes: 18 additions & 3 deletions webapp/src/components/Nav/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ import { useTranslation } from "react-i18next";
const Nav = () => {
const { t } = useTranslation();

const username = localStorage.getItem("username");
const username = process.env.NODE_ENV === "test" ? "testuser" : localStorage.getItem("username");

const navigate = useNavigate();
const { colorMode, toggleColorMode } = useColorMode();
const isDarkTheme = colorMode === "dark";
const textColor = isDarkTheme ? "white" : "teal.500";
const bgColor = isDarkTheme ? "gray.700" : "gray.200";
const isLargeScreen = useBreakpointValue({ base: false, lg: true });
// eslint-disable-next-line
const isLargeScreen = process.env.NODE_ENV === 'test'? true : useBreakpointValue({ base: false, lg: true });
const currentLocation = window.location;
const otherPortUrl = `${currentLocation.protocol}//${currentLocation.hostname}:8000/api-doc`;
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
Expand All @@ -56,6 +57,13 @@ const Nav = () => {
setIsDrawerOpen(false);
};

const handleLogout = () => {
if(process.env.NODE_ENV !== 'test'){
localStorage.removeItem("username");
}
navigate("/login");
};

const handleNavigate = (path) => {
navigate(path);
};
Expand Down Expand Up @@ -109,23 +117,26 @@ const Nav = () => {
<PopoverContent>
<PopoverArrow />
<PopoverCloseButton />
<PopoverHeader>{t("components.nav.gameModes")}</PopoverHeader>
<PopoverHeader data-testid="modes">{t("components.nav.gameModes")}</PopoverHeader>
<PopoverBody>
<Text
data-testid="classic"
cursor="pointer"
onClick={() => handleNavigate("/home/clasico")}
color={textColor}
>
{t("components.nav.classic")}
</Text>
<Text
data-testid="battery"
cursor="pointer"
onClick={() => handleNavigate("/home/bateria")}
color={textColor}
>
{t("components.nav.wisebattery")}
</Text>
<Text
data-testid="calculator"
cursor="pointer"
onClick={() => handleNavigate("/home/calculadora")}
color={textColor}
Expand All @@ -152,13 +163,15 @@ const Nav = () => {
<PopoverHeader>Social</PopoverHeader>
<PopoverBody>
<Text
data-testid="users"
cursor="pointer"
onClick={() => handleNavigate("/social/usuarios")}
color={textColor}
>
{t("components.nav.users")}
</Text>
<Text
data-testid="friends"
cursor="pointer"
onClick={() => handleNavigate("/social/amigos")}
color={textColor}
Expand Down Expand Up @@ -232,6 +245,8 @@ const Nav = () => {
{t("components.nav.about")}
</MenuItem>
</MenuGroup>
<MenuDivider />
<MenuItem onClick={handleLogout}>{t("components.nav.disconnect")}</MenuItem>
</MenuList>
</Menu>
<Switch
Expand Down

0 comments on commit 4954177

Please sign in to comment.