From 3cd5f6f49bad94ed70a1b1cfb750c9a74d7f5534 Mon Sep 17 00:00:00 2001 From: Pelayo Felgueroso Date: Mon, 27 Oct 2025 15:53:34 +0100 Subject: [PATCH] Add test to new QuickNav functionality --- packages/lib/src/quick-nav/QuickNav.test.tsx | 50 +++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/lib/src/quick-nav/QuickNav.test.tsx b/packages/lib/src/quick-nav/QuickNav.test.tsx index 0acb764971..a6b1408914 100644 --- a/packages/lib/src/quick-nav/QuickNav.test.tsx +++ b/packages/lib/src/quick-nav/QuickNav.test.tsx @@ -1,4 +1,4 @@ -import { render } from "@testing-library/react"; +import { render, fireEvent } from "@testing-library/react"; import DxcQuickNav from "./QuickNav"; const links = [ @@ -29,4 +29,52 @@ describe("QuickNav component tests", () => { expect(getByText("Spacing")).toBeTruthy(); expect(getByText("Button")).toBeTruthy(); }); + + test("should call scrollIntoView when clicking on a link in hash router mode", () => { + // Mock window.location.href to simulate hash router + Object.defineProperty(window, "location", { + value: { + href: "http://localhost:3000/#/components", + }, + writable: true, + }); + + // Mock document.getElementById and scrollIntoView + const mockScrollIntoView = jest.fn(); + const mockElement = { scrollIntoView: mockScrollIntoView }; + const mockGetElementById = jest.fn().mockReturnValue(mockElement); + document.getElementById = mockGetElementById; + + const { getByText } = render(); + const overviewLink = getByText("Overview"); + + fireEvent.click(overviewLink); + + expect(mockGetElementById).toHaveBeenCalledWith("overview"); + expect(mockScrollIntoView).toHaveBeenCalled(); + }); + + test("should call scrollIntoView when clicking on a sublink in hash router mode", () => { + // Mock window.location.href to simulate hash router + Object.defineProperty(window, "location", { + value: { + href: "http://localhost:3000/#/components", + }, + writable: true, + }); + + // Mock document.getElementById and scrollIntoView + const mockScrollIntoView = jest.fn(); + const mockElement = { scrollIntoView: mockScrollIntoView }; + const mockGetElementById = jest.fn().mockReturnValue(mockElement); + document.getElementById = mockGetElementById; + + const { getByText } = render(); + const colorLink = getByText("Color"); + + fireEvent.click(colorLink); + + expect(mockGetElementById).toHaveBeenCalledWith("principles-color"); + expect(mockScrollIntoView).toHaveBeenCalled(); + }); });