generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test for the OpeningsSearchBar
- Loading branch information
1 parent
c5d1de6
commit e8bcc4d
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
frontend/src/__test__/components/SilvicultureSearch/Openings/OpeningSearchBar.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
<QueryClientProvider client={queryClient}> | ||
<OpeningsSearchBar onSearchClick={() => {}} /> | ||
</QueryClientProvider> | ||
); | ||
|
||
// 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( | ||
<QueryClientProvider client={queryClient}> | ||
<OpeningsSearchBar onSearchClick={onSearchClick} /> | ||
</QueryClientProvider> | ||
); | ||
|
||
// Click the search button | ||
const searchButton = screen.getAllByRole("button", { name: "Search" })[1]; | ||
searchButton.click(); | ||
|
||
// Check if the onSearchClick function was called | ||
expect(onSearchClick).toHaveBeenCalled(); | ||
}); | ||
}); |