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
79 changes: 79 additions & 0 deletions frontend/src/test/CatalogueControls.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof CatalogueControls> {
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(<CatalogueControls {...props} />);

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(<CatalogueControls {...props} paginationVisible={true} />);

expect(findMobileSearch()).not.toBeInTheDocument();

rerender(<CatalogueControls {...props} paginationVisible={false} />);

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");
});
});