|
| 1 | +import React from "react"; |
| 2 | +import { describe, it, expect } from "vitest"; |
| 3 | +import { render, screen } from "@testing-library/react"; |
| 4 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 5 | +import { useLogFilterLogic } from "../../src/components/view_logs/log_filter_logic"; |
| 6 | + |
| 7 | +// Minimal mocks to avoid real network during hook init |
| 8 | +vi.mock("../../src/components/key_team_helpers/filter_helpers", () => ({ |
| 9 | + fetchAllKeyAliases: vi.fn().mockResolvedValue([]), |
| 10 | + fetchAllTeams: vi.fn().mockResolvedValue([]), |
| 11 | +})); |
| 12 | + |
| 13 | +const createQueryClient = () => new QueryClient({ |
| 14 | + defaultOptions: { queries: { retry: false, gcTime: 0 } }, |
| 15 | +}); |
| 16 | + |
| 17 | +function Harness({ logs }: { logs: any }) { |
| 18 | + const { filteredLogs } = useLogFilterLogic({ |
| 19 | + logs, |
| 20 | + accessToken: "token", |
| 21 | + startTime: "2025-01-01 00:00:00", |
| 22 | + endTime: "2025-01-02 00:00:00", |
| 23 | + pageSize: 50, |
| 24 | + isCustomDate: true, |
| 25 | + setCurrentPage: () => {}, |
| 26 | + userID: "user-1", |
| 27 | + userRole: "admin", |
| 28 | + }); |
| 29 | + |
| 30 | + return <div data-testid="count">{filteredLogs.data.length}</div>; |
| 31 | +} |
| 32 | + |
| 33 | +describe("useLogFilterLogic (minimal)", () => { |
| 34 | + it("useLogFilterLogic minimal: updates filteredLogs when logs change", async () => { |
| 35 | + const qc = createQueryClient(); |
| 36 | + const logsA = { data: [{ request_id: "a" }], total: 1, page: 1, page_size: 50, total_pages: 1 }; |
| 37 | + const logsB = { data: [{ request_id: "a" }, { request_id: "b" }], total: 2, page: 1, page_size: 50, total_pages: 1 }; |
| 38 | + |
| 39 | + const { rerender } = render( |
| 40 | + <QueryClientProvider client={qc}> |
| 41 | + <Harness logs={logsA} /> |
| 42 | + </QueryClientProvider> |
| 43 | + ); |
| 44 | + |
| 45 | + expect(await screen.findByTestId("count")).toHaveTextContent("1"); |
| 46 | + |
| 47 | + rerender( |
| 48 | + <QueryClientProvider client={qc}> |
| 49 | + <Harness logs={logsB} /> |
| 50 | + </QueryClientProvider> |
| 51 | + ); |
| 52 | + |
| 53 | + expect(await screen.findByTestId("count")).toHaveTextContent("2"); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | + |
0 commit comments