import React from "react";
import Link from "next/link";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen, fireEvent, act } from "@testing-library/react";
import NavigationProgressBar from "../NavigationProgressBar";
let mockPathname = "/";
let mockSearchParams = new URLSearchParams();
vi.mock("next/navigation", () => ({
usePathname: () => mockPathname,
useSearchParams: () => mockSearchParams,
}));
describe("NavigationProgressBar Component", () => {
beforeEach(() => {
vi.useFakeTimers();
mockPathname = "/";
mockSearchParams = new URLSearchParams();
});
afterEach(() => {
vi.clearAllTimers();
vi.useRealTimers();
});
it("initially does not render anything when progress is 0 and invisible", () => {
const { container } = render();
// On mount, completeProgress is triggered: progress = 100, then after 250ms visible=false, then after 200ms progress=0
act(() => {
vi.advanceTimersByTime(500);
});
expect(container.firstChild).toBeNull();
});
it("starts progress bar when clicking an internal link with different pathname", () => {
const { container } = render(
فروشگاه
);
// Settle initial completeProgress on mount
act(() => {
vi.advanceTimersByTime(500);
});
expect(container.querySelector(".fixed")).toBeNull();
// Click internal link
const link = screen.getByTestId("internal-link");
act(() => {
fireEvent.click(link);
});
// Bar should now be visible with initial progress of 20%
const barWrapper = container.querySelector(".fixed");
expect(barWrapper).toBeInTheDocument();
expect(barWrapper).toHaveStyle({ opacity: "1" });
const progressBar = barWrapper?.querySelector(".h-\\[3\\.5px\\]");
expect(progressBar).toHaveStyle({ width: "20%" });
// Advance timer to trigger interval increments
act(() => {
vi.advanceTimersByTime(300);
});
// Progress should have increased beyond 20%
const currentWidth = parseFloat(progressBar?.getAttribute("style")?.match(/width:\s*([\d.]+)%/)?.[1] || "0");
expect(currentWidth).toBeGreaterThan(20);
});
it("ignores clicks on external, anchor, mailto, tel, and blank target links", () => {
const { container } = render(
);
act(() => {
vi.advanceTimersByTime(500);
});
// Click each link and assert progress bar remains not rendered
["hash-link", "mail-link", "tel-link", "js-link", "blank-link"].forEach((id) => {
act(() => {
fireEvent.click(screen.getByTestId(id));
});
expect(container.querySelector(".fixed")).toBeNull();
});
});
it("ignores clicks with modifier keys (ctrlKey, metaKey, shiftKey, altKey)", () => {
const { container } = render(
فروشگاه
);
act(() => {
vi.advanceTimersByTime(500);
});
const link = screen.getByTestId("internal-link");
act(() => {
fireEvent.click(link, { ctrlKey: true });
});
expect(container.querySelector(".fixed")).toBeNull();
act(() => {
fireEvent.click(link, { metaKey: true });
});
expect(container.querySelector(".fixed")).toBeNull();
});
it("completes progress and hides when pathname or searchParams change", () => {
const { container, rerender } = render(
فروشگاه
);
// Initial mount flush
act(() => {
vi.advanceTimersByTime(500);
});
// Start progress
act(() => {
fireEvent.click(screen.getByTestId("internal-link"));
});
const barWrapper = container.querySelector(".fixed");
expect(barWrapper).toBeInTheDocument();
// Route transition completes: pathname changes
mockPathname = "/shop";
rerender(
فروشگاه
);
// Progress moves to 100%
const progressBar = container.querySelector(".h-\\[3\\.5px\\]");
expect(progressBar).toHaveStyle({ width: "100%" });
// After 250ms opacity fades out (visible = false)
act(() => {
vi.advanceTimersByTime(250);
});
expect(container.querySelector(".fixed")).toHaveStyle({ opacity: "0" });
// After another 200ms progress returns to 0 and bar unmounts
act(() => {
vi.advanceTimersByTime(200);
});
expect(container.querySelector(".fixed")).toBeNull();
});
});