|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { hasAnyActiveFilters } from "../../../components/SilvicultureSearch/OpeningSearch/utils"; |
| 3 | +import { OpeningSearchFilterType } from "../../../components/SilvicultureSearch/OpeningSearch/definitions"; |
| 4 | +import CodeDescriptionDto from "../../../types/CodeDescriptionType"; |
| 5 | + |
| 6 | +describe("hasAnyActiveFilters", () => { |
| 7 | + const mockCodeDesc = (code: string, desc: string): CodeDescriptionDto => ({ |
| 8 | + code, |
| 9 | + description: desc, |
| 10 | + }); |
| 11 | + |
| 12 | + it("should return false when all filters are empty or undefined", () => { |
| 13 | + const filters: OpeningSearchFilterType = {}; |
| 14 | + expect(hasAnyActiveFilters(filters)).toBe(false); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should return false when only dateType is set", () => { |
| 18 | + const filters: OpeningSearchFilterType = { |
| 19 | + dateType: { code: "update", description: "Update" }, |
| 20 | + }; |
| 21 | + expect(hasAnyActiveFilters(filters)).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should return true if an array filter has values", () => { |
| 25 | + const filters: OpeningSearchFilterType = { |
| 26 | + orgUnit: [mockCodeDesc("DAS", "District A")], |
| 27 | + }; |
| 28 | + expect(hasAnyActiveFilters(filters)).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should return true if a string filter has non-empty value", () => { |
| 32 | + const filters: OpeningSearchFilterType = { |
| 33 | + mainSearchTerm: "test search", |
| 34 | + }; |
| 35 | + expect(hasAnyActiveFilters(filters)).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should return false if string filter is empty or whitespace", () => { |
| 39 | + expect(hasAnyActiveFilters({ mainSearchTerm: "" })).toBe(false); |
| 40 | + expect(hasAnyActiveFilters({ mainSearchTerm: " " })).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should return true if a boolean filter is true", () => { |
| 44 | + expect(hasAnyActiveFilters({ myOpenings: true })).toBe(true); |
| 45 | + expect(hasAnyActiveFilters({ submittedToFrpa: true })).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should return false if boolean is false or undefined", () => { |
| 49 | + expect(hasAnyActiveFilters({ myOpenings: false })).toBe(false); |
| 50 | + expect(hasAnyActiveFilters({ submittedToFrpa: false })).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return true if a date string is set", () => { |
| 54 | + const filters: OpeningSearchFilterType = { |
| 55 | + updateDateStart: "2024-01-01", |
| 56 | + }; |
| 57 | + expect(hasAnyActiveFilters(filters)).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should return true if a number filter is present", () => { |
| 61 | + const filters: OpeningSearchFilterType = { |
| 62 | + page: 1, |
| 63 | + }; |
| 64 | + expect(hasAnyActiveFilters(filters)).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should return true if multiple filters are present", () => { |
| 68 | + const filters: OpeningSearchFilterType = { |
| 69 | + statusList: [mockCodeDesc("APP", "Approved")], |
| 70 | + mainSearchTerm: "waldo", |
| 71 | + cuttingPermitId: "WA-001", |
| 72 | + myOpenings: true, |
| 73 | + dateType: { code: "update", description: "Update" }, |
| 74 | + }; |
| 75 | + expect(hasAnyActiveFilters(filters)).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return false if all filters are explicitly empty", () => { |
| 79 | + const filters: OpeningSearchFilterType = { |
| 80 | + mainSearchTerm: "", |
| 81 | + statusList: [], |
| 82 | + orgUnit: [], |
| 83 | + myOpenings: false, |
| 84 | + submittedToFrpa: false, |
| 85 | + disturbanceDateStart: "", |
| 86 | + updateDateEnd: "", |
| 87 | + dateType: { code: "update", description: "Update" }, |
| 88 | + }; |
| 89 | + expect(hasAnyActiveFilters(filters)).toBe(false); |
| 90 | + }); |
| 91 | +}); |
0 commit comments