|
| 1 | +import { useNavigationTransition } from "@src/app/_components/context/NavigationContext"; |
| 2 | +import { TransitionLink } from "@src/app/_components/navigation/TransitionLink"; |
| 3 | +import { fireEvent, render, screen } from "@testing-library/react"; |
| 4 | +import React from "react"; |
| 5 | + |
| 6 | +jest.mock("@src/app/_components/context/NavigationContext", () => ({ |
| 7 | + useNavigationTransition: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +const mockNavigate = jest.fn(); |
| 11 | + |
| 12 | +describe("TransitionLink", () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("calls navigate with href when clicked", () => { |
| 18 | + (useNavigationTransition as jest.Mock).mockReturnValue({ |
| 19 | + navigate: mockNavigate, |
| 20 | + isPending: false, |
| 21 | + }); |
| 22 | + |
| 23 | + render(<TransitionLink href="/test-route">Go to test</TransitionLink>); |
| 24 | + |
| 25 | + fireEvent.click(screen.getByText("Go to test")); |
| 26 | + |
| 27 | + expect(mockNavigate).toHaveBeenCalledWith("/test-route"); |
| 28 | + expect(mockNavigate).toHaveBeenCalledTimes(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it("sets aria-disabled when navigation is pending", () => { |
| 32 | + (useNavigationTransition as jest.Mock).mockReturnValue({ |
| 33 | + navigate: mockNavigate, |
| 34 | + isPending: true, |
| 35 | + }); |
| 36 | + |
| 37 | + render(<TransitionLink href="/test-route">Go to test</TransitionLink>); |
| 38 | + |
| 39 | + const link = screen.getByRole("link"); |
| 40 | + |
| 41 | + expect(link).toHaveAttribute("aria-disabled", "true"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("passes className and target through to the link", () => { |
| 45 | + (useNavigationTransition as jest.Mock).mockReturnValue({ |
| 46 | + navigate: mockNavigate, |
| 47 | + isPending: false, |
| 48 | + }); |
| 49 | + |
| 50 | + render( |
| 51 | + <TransitionLink href="/test-route" className="test-class" target="_blank"> |
| 52 | + Go to test |
| 53 | + </TransitionLink>, |
| 54 | + ); |
| 55 | + |
| 56 | + const link = screen.getByRole("link"); |
| 57 | + |
| 58 | + expect(link).toHaveClass("test-class"); |
| 59 | + expect(link).toHaveAttribute("target", "_blank"); |
| 60 | + }); |
| 61 | +}); |
0 commit comments