From e8bcc4d9580f345ca88b416a7ebee0450f612eae Mon Sep 17 00:00:00 2001 From: jazzgrewal Date: Mon, 4 Nov 2024 17:01:00 -0800 Subject: [PATCH] added test for the OpeningsSearchBar --- .../Openings/OpeningSearchBar.test.tsx | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 frontend/src/__test__/components/SilvicultureSearch/Openings/OpeningSearchBar.test.tsx diff --git a/frontend/src/__test__/components/SilvicultureSearch/Openings/OpeningSearchBar.test.tsx b/frontend/src/__test__/components/SilvicultureSearch/Openings/OpeningSearchBar.test.tsx new file mode 100644 index 00000000..1b942549 --- /dev/null +++ b/frontend/src/__test__/components/SilvicultureSearch/Openings/OpeningSearchBar.test.tsx @@ -0,0 +1,56 @@ +// src/__test__/components/SilvicultureSearch/Openings/OpeningsSearchBar.test.tsx + +import React from "react"; +import { render, screen } from "@testing-library/react"; +import "@testing-library/jest-dom"; +import OpeningsSearchBar from "../../../../components/SilvicultureSearch/Openings/OpeningsSearchBar"; +import { vi } from "vitest"; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { useOpeningsSearch } from "../../../../contexts/search/OpeningsSearch"; + +// Mock the useOpeningsSearch context to avoid rendering errors +vi.mock("../../../../contexts/search/OpeningsSearch", () => ({ + useOpeningsSearch: vi.fn().mockReturnValue({ + filters: [], + clearFilters: vi.fn(), + searchTerm: "", + setSearchTerm: vi.fn(), + }), +})); + +describe("OpeningsSearchBar", () => { + // Create a new QueryClient instance for each test + const queryClient = new QueryClient(); + + it("renders the search input with the correct placeholder", () => { + render( + + {}} /> + + ); + + // Check if the search input field is present with the correct placeholder text + const searchInput = screen.getByPlaceholderText( + "Search by opening ID, opening number, timber mark or file ID" + ); + expect(searchInput).toBeInTheDocument(); + }); + + it("should call the onSearchClick function when the search button is clicked", () => { + // Create a mock function to pass as a prop + const onSearchClick = vi.fn(); + + render( + + + + ); + + // Click the search button + const searchButton = screen.getAllByRole("button", { name: "Search" })[1]; + searchButton.click(); + + // Check if the onSearchClick function was called + expect(onSearchClick).toHaveBeenCalled(); + }); +});