Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion packages/lib/src/quick-nav/QuickNav.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from "@testing-library/react";
import { render, fireEvent } from "@testing-library/react";
import DxcQuickNav from "./QuickNav";

const links = [
Expand Down Expand Up @@ -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(<DxcQuickNav links={links} />);
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(<DxcQuickNav links={links} />);
const colorLink = getByText("Color");

fireEvent.click(colorLink);

expect(mockGetElementById).toHaveBeenCalledWith("principles-color");
expect(mockScrollIntoView).toHaveBeenCalled();
});
});