Skip to content

Commit

Permalink
added simple test for AdvancedSearchDropDown
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzgrewal committed Nov 5, 2024
1 parent eae7a06 commit c5d1de6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
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
Expand Up @@ -110,12 +110,6 @@ const AdvancedSearchDropdown: React.FC<AdvancedSearchDropdownProps> = ({
value: item.value,
})) || [];

const blockStatusItems =
data.blockStatuses?.map((item: any) => ({
text: item.label,
value: item.value,
})) || [];

return (
<div className="advanced-search-dropdown">
<FlexGrid className="container-fluid advanced-search-container p-32">
Expand Down

0 comments on commit c5d1de6

Please sign in to comment.