|
| 1 | +import type { components } from "@/api/prefect"; |
| 2 | +import { QueryClient, useSuspenseQuery } from "@tanstack/react-query"; |
| 3 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import { buildApiUrl, createWrapper, server } from "@tests/utils"; |
| 5 | +import { http, HttpResponse } from "msw"; |
| 6 | +import { describe, expect, it } from "vitest"; |
| 7 | +import { buildFilterLogsQuery, queryKeyFactory } from "."; |
| 8 | + |
| 9 | +type Log = components["schemas"]["Log"]; |
| 10 | + |
| 11 | +describe("logs api", () => { |
| 12 | + const mockFetchLogsAPI = (logs: Array<Log>) => { |
| 13 | + server.use( |
| 14 | + http.post(buildApiUrl("/logs/filter"), () => { |
| 15 | + return HttpResponse.json(logs); |
| 16 | + }), |
| 17 | + ); |
| 18 | + }; |
| 19 | + |
| 20 | + describe("queryKeyFactory", () => { |
| 21 | + it("generates correct query keys", () => { |
| 22 | + expect(queryKeyFactory.all()).toEqual(["logs"]); |
| 23 | + expect(queryKeyFactory.lists()).toEqual(["logs", "list"]); |
| 24 | + |
| 25 | + const filter = { |
| 26 | + logs: { operator: "and_", level: { ge_: 0 } }, |
| 27 | + sort: "TIMESTAMP_ASC", |
| 28 | + offset: 0, |
| 29 | + limit: 100, |
| 30 | + } as const; |
| 31 | + |
| 32 | + expect(queryKeyFactory.list(filter)).toEqual(["logs", "list", filter]); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("buildFilterLogsQuery", () => { |
| 37 | + it("fetches logs with default parameters", async () => { |
| 38 | + const mockLogs = [ |
| 39 | + { id: "1", level: 20, message: "Test log 1" }, |
| 40 | + { id: "2", level: 30, message: "Test log 2" }, |
| 41 | + ] as Log[]; |
| 42 | + |
| 43 | + mockFetchLogsAPI(mockLogs); |
| 44 | + |
| 45 | + const queryClient = new QueryClient(); |
| 46 | + const { result } = renderHook( |
| 47 | + () => |
| 48 | + useSuspenseQuery( |
| 49 | + buildFilterLogsQuery({ |
| 50 | + offset: 0, |
| 51 | + sort: "TIMESTAMP_ASC", |
| 52 | + limit: 100, |
| 53 | + }), |
| 54 | + ), |
| 55 | + { wrapper: createWrapper({ queryClient }) }, |
| 56 | + ); |
| 57 | + |
| 58 | + await waitFor(() => { |
| 59 | + expect(result.current.data).toEqual(mockLogs); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("fetches logs with custom filter parameters", async () => { |
| 64 | + const mockLogs = [{ id: "1", level: 40, message: "Error log" }] as Log[]; |
| 65 | + |
| 66 | + mockFetchLogsAPI(mockLogs); |
| 67 | + |
| 68 | + const filter = { |
| 69 | + logs: { |
| 70 | + operator: "and_", |
| 71 | + level: { ge_: 40 }, |
| 72 | + }, |
| 73 | + sort: "TIMESTAMP_DESC", |
| 74 | + offset: 0, |
| 75 | + limit: 50, |
| 76 | + } as const; |
| 77 | + |
| 78 | + const queryClient = new QueryClient(); |
| 79 | + const { result } = renderHook( |
| 80 | + () => useSuspenseQuery(buildFilterLogsQuery(filter)), |
| 81 | + { wrapper: createWrapper({ queryClient }) }, |
| 82 | + ); |
| 83 | + |
| 84 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 85 | + expect(result.current.data).toEqual(mockLogs); |
| 86 | + }); |
| 87 | + |
| 88 | + it("handles empty response from API", async () => { |
| 89 | + mockFetchLogsAPI([]); |
| 90 | + |
| 91 | + const queryClient = new QueryClient(); |
| 92 | + const { result } = renderHook( |
| 93 | + () => |
| 94 | + useSuspenseQuery( |
| 95 | + buildFilterLogsQuery({ |
| 96 | + offset: 0, |
| 97 | + sort: "TIMESTAMP_ASC", |
| 98 | + limit: 100, |
| 99 | + }), |
| 100 | + ), |
| 101 | + { wrapper: createWrapper({ queryClient }) }, |
| 102 | + ); |
| 103 | + |
| 104 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 105 | + expect(result.current.data).toEqual([]); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments