|
1 | | -import { render, screen } from "@testing-library/react"; |
| 1 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
2 | 3 | import { describe, it, expect, beforeEach, vi } from "vitest"; |
3 | 4 | import CanList from "./CanList"; |
4 | 5 | import { useGetCanFilterOptionsQuery, useGetCansFundingQuery, useGetCansQuery } from "../../../api/opsAPI"; |
@@ -38,7 +39,16 @@ vi.mock("./CANFilterTags", () => ({ |
38 | 39 | })); |
39 | 40 |
|
40 | 41 | vi.mock("./CANFiscalYearSelect", () => ({ |
41 | | - default: () => <div data-testid="can-fiscal-year-select">FY</div> |
| 42 | + default: ({ fiscalYear, setSelectedFiscalYear }) => ( |
| 43 | + <select |
| 44 | + data-testid="can-fiscal-year-select" |
| 45 | + value={fiscalYear} |
| 46 | + onChange={(e) => setSelectedFiscalYear(e.target.value)} |
| 47 | + > |
| 48 | + <option value={fiscalYear}>{fiscalYear}</option> |
| 49 | + <option value="All">All</option> |
| 50 | + </select> |
| 51 | + ) |
42 | 52 | })); |
43 | 53 |
|
44 | 54 | vi.mock("../../../components/UI/Table/Table.hooks", () => ({ |
@@ -109,4 +119,48 @@ describe("CanList", () => { |
109 | 119 | expect(screen.getByTestId("can-table")).toBeInTheDocument(); |
110 | 120 | expect(screen.getByTestId("can-summary-cards")).toBeInTheDocument(); |
111 | 121 | }); |
| 122 | + |
| 123 | + it("sends empty fiscalYear array when 'All' is selected, resulting in no fiscal_year query param", async () => { |
| 124 | + const user = userEvent.setup(); |
| 125 | + |
| 126 | + render(<CanList />); |
| 127 | + |
| 128 | + // Initially, should call with current fiscal year as array |
| 129 | + expect(useGetCansQuery).toHaveBeenCalledWith( |
| 130 | + expect.objectContaining({ |
| 131 | + fiscalYear: expect.any(Array) |
| 132 | + }) |
| 133 | + ); |
| 134 | + |
| 135 | + // Get the initial call's fiscalYear to verify it's not empty |
| 136 | + const initialCall = useGetCansQuery.mock.calls[0][0]; |
| 137 | + expect(initialCall.fiscalYear).toHaveLength(1); |
| 138 | + |
| 139 | + // Change fiscal year to "All" |
| 140 | + const fiscalYearSelect = screen.getByTestId("can-fiscal-year-select"); |
| 141 | + await user.selectOptions(fiscalYearSelect, "All"); |
| 142 | + |
| 143 | + // Wait for the component to re-render with new params |
| 144 | + await waitFor(() => { |
| 145 | + // Find the most recent call |
| 146 | + const calls = useGetCansQuery.mock.calls; |
| 147 | + const lastCall = calls[calls.length - 1][0]; |
| 148 | + |
| 149 | + // Verify fiscalYear is an empty array (which results in no query param) |
| 150 | + expect(lastCall.fiscalYear).toEqual([]); |
| 151 | + }); |
| 152 | + |
| 153 | + // Also verify the filter options query receives empty array |
| 154 | + await waitFor(() => { |
| 155 | + const calls = useGetCanFilterOptionsQuery.mock.calls; |
| 156 | + const lastCall = calls[calls.length - 1][0]; |
| 157 | + expect(lastCall.fiscalYear).toEqual([]); |
| 158 | + }); |
| 159 | + |
| 160 | + await waitFor(() => { |
| 161 | + const calls = useGetCansFundingQuery.mock.calls; |
| 162 | + const lastCall = calls[calls.length - 1][0]; |
| 163 | + expect(lastCall.fiscalYear).toBeUndefined(); |
| 164 | + }); |
| 165 | + }); |
112 | 166 | }); |
0 commit comments