From b0dc9f9e7535da4b50bf21a21b356e6ee4d8dce9 Mon Sep 17 00:00:00 2001 From: Karen Skauge Date: Wed, 4 Nov 2026 19:51:19 +0100 Subject: [PATCH] test: cover mobile search behaviour around pagination --- frontend/src/test/CatalogueControls.test.tsx | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 frontend/src/test/CatalogueControls.test.tsx diff --git a/frontend/src/test/CatalogueControls.test.tsx b/frontend/src/test/CatalogueControls.test.tsx new file mode 100644 index 0000000..d256673 --- /dev/null +++ b/frontend/src/test/CatalogueControls.test.tsx @@ -0,0 +1,79 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { render, screen } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import CatalogueControls from "../components/CatalogueControls"; +import { useArtists } from "../hooks/useArtists"; +import type { ComponentProps } from "react"; + +vi.mock("../hooks/useArtists", () => ({ + useArtists: vi.fn(), +})); + +const mockedUseArtists = vi.mocked(useArtists); + +function createProps(): ComponentProps { + return { + onSearch: vi.fn(), + artist: "", + onArtistChange: vi.fn(), + sort: undefined, + onSortChange: vi.fn(), + totalCount: 25, + onReset: vi.fn(), + showFilters: false, + onToggleFilters: vi.fn(), + paginationVisible: false, + }; +} + +describe("CatalogueControls mobile search", () => { + beforeEach(() => { + vi.resetAllMocks(); + + mockedUseArtists.mockReturnValue({ + artists: [], + loading: false, + error: "", + }); + }); + + it("closes mobile search when pagination appears and allows reopening later", async () => { + const user = userEvent.setup(); + const props = createProps(); + + const { rerender } = render(); + + const findMobileSearch = () => + screen.queryByLabelText("Search artworks by title or artist", { + selector: "#mobile-artwork-search", + }); + + expect(findMobileSearch()).not.toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: "Open search" })); + + expect(findMobileSearch()).toBeInTheDocument(); + expect( + screen.getByRole("button", { name: "Close search" }), + ).toHaveAttribute("aria-expanded", "true"); + + rerender(); + + expect(findMobileSearch()).not.toBeInTheDocument(); + + rerender(); + + expect(findMobileSearch()).not.toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Open search" })).toHaveAttribute( + "aria-expanded", + "false", + ); + + await user.click(screen.getByRole("button", { name: "Open search" })); + + expect(findMobileSearch()).toBeInTheDocument(); + expect( + screen.getByRole("button", { name: "Close search" }), + ).toHaveAttribute("aria-expanded", "true"); + }); +});