Skip to content

Commit 5aaca4c

Browse files
authored
Merge pull request #2320 from dxc-technology/PelayoFelgueroso/quickNav-testing
Add test to new QuickNav functionality
2 parents f2502be + 64b072c commit 5aaca4c

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

packages/lib/src/quick-nav/QuickNav.test.tsx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from "@testing-library/react";
1+
import { render, fireEvent } from "@testing-library/react";
22
import DxcQuickNav from "./QuickNav";
33

44
const links = [
@@ -29,4 +29,52 @@ describe("QuickNav component tests", () => {
2929
expect(getByText("Spacing")).toBeTruthy();
3030
expect(getByText("Button")).toBeTruthy();
3131
});
32+
33+
test("should call scrollIntoView when clicking on a link in hash router mode", () => {
34+
// Mock window.location.href to simulate hash router
35+
Object.defineProperty(window, "location", {
36+
value: {
37+
href: "http://localhost:3000/#/components",
38+
},
39+
writable: true,
40+
});
41+
42+
// Mock document.getElementById and scrollIntoView
43+
const mockScrollIntoView = jest.fn();
44+
const mockElement = { scrollIntoView: mockScrollIntoView };
45+
const mockGetElementById = jest.fn().mockReturnValue(mockElement);
46+
document.getElementById = mockGetElementById;
47+
48+
const { getByText } = render(<DxcQuickNav links={links} />);
49+
const overviewLink = getByText("Overview");
50+
51+
fireEvent.click(overviewLink);
52+
53+
expect(mockGetElementById).toHaveBeenCalledWith("overview");
54+
expect(mockScrollIntoView).toHaveBeenCalled();
55+
});
56+
57+
test("should call scrollIntoView when clicking on a sublink in hash router mode", () => {
58+
// Mock window.location.href to simulate hash router
59+
Object.defineProperty(window, "location", {
60+
value: {
61+
href: "http://localhost:3000/#/components",
62+
},
63+
writable: true,
64+
});
65+
66+
// Mock document.getElementById and scrollIntoView
67+
const mockScrollIntoView = jest.fn();
68+
const mockElement = { scrollIntoView: mockScrollIntoView };
69+
const mockGetElementById = jest.fn().mockReturnValue(mockElement);
70+
document.getElementById = mockGetElementById;
71+
72+
const { getByText } = render(<DxcQuickNav links={links} />);
73+
const colorLink = getByText("Color");
74+
75+
fireEvent.click(colorLink);
76+
77+
expect(mockGetElementById).toHaveBeenCalledWith("principles-color");
78+
expect(mockScrollIntoView).toHaveBeenCalled();
79+
});
3280
});

0 commit comments

Comments
 (0)