import { fireEvent, render, screen, within } from "@testing-library/react"; import type { ReactNode } from "react"; import { Header } from "./Header"; jest.mock("motion/react", () => { const React = require("react"); const motion = new Proxy( {}, { get: (_, tag: string) => { const Component = ({ children, animate: _animate, transition: _transition, whileHover: _whileHover, ...props }: { children?: ReactNode } & Record) => React.createElement(tag, props, children); Component.displayName = `motion.${tag}`; return Component; }, }, ); return { motion, useReducedMotion: () => false }; }); describe("Header", () => { it("exposes the primary navigation and the existing CV asset", () => { render(
); expect(screen.getByRole("link", { name: "Home" })).toHaveAttribute( "href", "/#home", ); expect(screen.getByRole("link", { name: "Casos" })).toHaveAttribute( "href", "/cases", ); expect(screen.getByRole("link", { name: "Serviços" })).toHaveAttribute( "href", "/servicos", ); expect(screen.getByRole("link", { name: "Currículo" })).toHaveAttribute( "href", "/curriculo", ); const cvLink = screen.getByRole("link", { name: "Download CV" }); expect(cvLink).toHaveAttribute("href", "/Profile.pdf"); expect(cvLink).toHaveAttribute("download", "Evandro_Fonseca_Junior_CV.pdf"); expect( screen.getByRole("navigation", { name: "Navegação principal" }), ).toHaveClass("site-header-nav"); }); it("announces and controls the mobile menu", () => { render(
); const menuButton = screen.getByRole("button", { name: "Abrir menu" }); expect(menuButton).not.toHaveAttribute("aria-controls"); expect(menuButton).toHaveAttribute("aria-expanded", "false"); expect(menuButton).toHaveClass("site-header-menu-toggle"); expect( screen.queryByRole("navigation", { name: "Navegação mobile" }), ).not.toBeInTheDocument(); fireEvent.click(menuButton); expect(screen.getByRole("button", { name: "Fechar menu" })).toHaveAttribute( "aria-controls", "site-navigation-menu", ); expect(screen.getByRole("button", { name: "Fechar menu" })).toHaveAttribute( "aria-expanded", "true", ); const mobileNavigation = screen.getByRole("navigation", { name: "Navegação mobile", }); expect(mobileNavigation).toHaveAttribute("id", "site-navigation-menu"); expect(mobileNavigation).toHaveAttribute("aria-hidden", "false"); const closeButton = screen.getByRole("button", { name: "Fechar menu" }); fireEvent.keyDown(closeButton, { key: "Escape" }); const openButton = screen.getByRole("button", { name: "Abrir menu" }); expect(openButton).toHaveAttribute("aria-expanded", "false"); expect(document.activeElement).toBe(openButton); }); it("returns focus after closing the menu from a mobile link", () => { render(
); fireEvent.click(screen.getByRole("button", { name: "Abrir menu" })); const mobileNavigation = screen.getByRole("navigation", { name: "Navegação mobile", }); fireEvent.click( within(mobileNavigation).getByRole("link", { name: "Home" }), ); const openButton = screen.getByRole("button", { name: "Abrir menu" }); expect(openButton).toHaveAttribute("aria-expanded", "false"); expect(document.activeElement).toBe(openButton); }); });