Skip to content

Commit

Permalink
Switched scroll-to-top test over to @testing-library/react
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Jun 20, 2024
1 parent 949c415 commit b270dae
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/frontend/__tests__/components/ScrollToTop.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Link, MemoryRouter, Route, Routes } from "react-router-dom";
import renderer from "react-test-renderer";

import { ScrollToTop } from "../../src/components/ScrollToTop";

global.scrollTo = jest.fn();

describe("ScrollToTop", () => {
test("calls window.scrollTo when route changes", async () => {
expect.assertions(2);
expect.assertions(3);
const user = userEvent.setup();
const root = renderer.create(
const { getByText } = render(
<MemoryRouter initialEntries={["/home"]}>
<ScrollToTop />
<Routes>
Expand All @@ -26,9 +26,9 @@ describe("ScrollToTop", () => {
</Routes>
</MemoryRouter>,
);
expect(global.scrollTo).not.toHaveBeenCalled();
const link = root.root.findByProps({ className: "the-link" });
await user.click(link.instance as Element);
expect(global.scrollTo).toHaveBeenCalledWith(0, 0);
expect(global.scrollTo).toHaveBeenCalledTimes(1);
await user.click(getByText("Linkage"));
expect(global.scrollTo).toHaveBeenCalledTimes(2);
expect(global.scrollTo).toHaveBeenLastCalledWith(0, 0);
});
});

0 comments on commit b270dae

Please sign in to comment.