- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
171 lines
5.1 KiB
TypeScript
171 lines
5.1 KiB
TypeScript
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(<NavigationProgressBar />);
|
|
// 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(
|
|
<div>
|
|
<NavigationProgressBar />
|
|
<Link href="/shop" data-testid="internal-link">
|
|
فروشگاه
|
|
</Link>
|
|
</div>
|
|
);
|
|
|
|
// 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(
|
|
<div>
|
|
<NavigationProgressBar />
|
|
<a href="#section" data-testid="hash-link">Hash</a>
|
|
<a href="mailto:test@canina.ir" data-testid="mail-link">Mail</a>
|
|
<a href="tel:02112345678" data-testid="tel-link">Tel</a>
|
|
<a href="javascript:void(0)" data-testid="js-link">JS</a>
|
|
<a href="/shop" target="_blank" data-testid="blank-link">New Tab</a>
|
|
</div>
|
|
);
|
|
|
|
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(
|
|
<div>
|
|
<NavigationProgressBar />
|
|
<Link href="/shop" data-testid="internal-link">فروشگاه</Link>
|
|
</div>
|
|
);
|
|
|
|
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(
|
|
<div>
|
|
<NavigationProgressBar />
|
|
<Link href="/shop" data-testid="internal-link">فروشگاه</Link>
|
|
</div>
|
|
);
|
|
|
|
// 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(
|
|
<div>
|
|
<NavigationProgressBar />
|
|
<Link href="/shop" data-testid="internal-link">فروشگاه</Link>
|
|
</div>
|
|
);
|
|
|
|
// 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();
|
|
});
|
|
});
|