Skip to content

Commit

Permalink
adding the tests from the multiSelect branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzgrewal committed Nov 5, 2024
1 parent 7b27b18 commit c6cb9ca
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
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 { OpeningsSearchProvider } from "../../../../contexts/search/OpeningsSearch";
import { Pagination } from "@carbon/react";
import {Provider } from "react-redux";
import PaginationProvider from "../../../../contexts/PaginationProvider";
import RecentOpeningsDataTable from "../../../../components/Dashboard/Opening/RecentOpeningsDataTable";
import { MemoryRouter } from "react-router-dom";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { render, screen, fireEvent } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import "@testing-library/jest-dom";
import AdvancedSearchDropdown from "../../../../components/SilvicultureSearch/Openings/AdvancedSearchDropdown";
import { useOpeningFiltersQuery } from "../../../../services/queries/search/openingQueries";
import { useOpeningsSearch } from "../../../../contexts/search/OpeningsSearch";
import React from "react";

// Mocking the toggleShowFilters function
const toggleShowFilters = vi.fn();

// Mocking useOpeningFiltersQuery to return mock data for filters
vi.mock("../../../../services/queries/search/openingQueries", () => ({
useOpeningFiltersQuery: vi.fn(),
}));

// Mocking useOpeningsSearch to return the necessary functions and state
vi.mock("../../../../contexts/search/OpeningsSearch", () => ({
useOpeningsSearch: vi.fn(),
}));

describe("AdvancedSearchDropdown", () => {
beforeEach(() => {
// Mock data to return for the filters query
(useOpeningFiltersQuery as jest.Mock).mockReturnValue({
data: {
categories: ["FTML", "CONT"],
orgUnits: ["DCK", "DCR"],
dateTypes: ["Disturbance", "Free Growing"],
},
isLoading: false,
isError: false,
});

// Mock implementation of useOpeningsSearch context
(useOpeningsSearch as jest.Mock).mockReturnValue({
filters: {
openingFilters: [],
orgUnit: [],
category: [],
clientAcronym: "",
clientLocationCode: "",
cutBlock: "",
cuttingPermit: "",
timberMark: "",
dateType: "",
startDate: null,
endDate: null,
status: [],
},
setFilters: vi.fn(),
clearFilters: vi.fn(),
});
});

it("displays an error message if there is an error", () => {
(useOpeningFiltersQuery as jest.Mock).mockReturnValue({
isLoading: false,
isError: true,
data: null,
});

render(<AdvancedSearchDropdown toggleShowFilters={toggleShowFilters} />);
expect(
screen.getByText("There was an error while loading the advanced filters.")
).toBeInTheDocument();
});
});
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();
});
});

0 comments on commit c6cb9ca

Please sign in to comment.