Files
cannamanage/cannamanage-frontend/src/__tests__/services/distributions.test.tsx
T
Patrick Plate 53931d9d2b
CI — Build, Lint & Security Scan / backend (push) Failing after 1m24s
CI — Build, Lint & Security Scan / frontend (push) Failing after 48s
CI — Build, Lint & Security Scan / image-scan (push) Has been skipped
CI — Build, Lint & Security Scan / secrets-scan (push) Failing after 27s
Deploy to TrueNAS / deploy (push) Successful in 3m0s
fix: resolve CI failures — RetentionService bean, frontend types, artifact upload
- Remove @ConditionalOnProperty from RetentionService class; guard only @Scheduled method
- Fix QuotaStatus property references in frontend tests
- Downgrade upload-artifact to v3 for Gitea compatibility
2026-06-19 16:23:18 +02:00

95 lines
2.8 KiB
TypeScript

import React from "react"
import {
useCreateDistributionMutation,
useDistributionsQuery,
useQuotaQuery,
} from "@/services/distributions"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { renderHook, waitFor } from "@testing-library/react"
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"
import { mockDistributionsPage, mockQuotaStatus } from "../mocks/handlers"
import { server } from "../mocks/server"
beforeAll(() => server.listen({ onUnhandledRequest: "error" }))
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
function createWrapper() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, gcTime: 0 } },
})
return function Wrapper({ children }: { children: React.ReactNode }) {
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
}
describe("useDistributionsQuery", () => {
it("returns paginated distributions data", async () => {
const { result } = renderHook(() => useDistributionsQuery(), {
wrapper: createWrapper(),
})
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data).toEqual(mockDistributionsPage)
expect(result.current.data?.content).toHaveLength(1)
})
it("applies filter params", async () => {
const { result } = renderHook(
() =>
useDistributionsQuery({
memberId: "m1",
from: "2025-06-01",
to: "2025-06-30",
}),
{ wrapper: createWrapper() }
)
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data?.content).toBeDefined()
})
})
describe("useQuotaQuery", () => {
it("returns quota for given member", async () => {
const { result } = renderHook(() => useQuotaQuery("m1"), {
wrapper: createWrapper(),
})
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data).toEqual(mockQuotaStatus)
expect(result.current.data?.monthlyLimitGrams).toBe(50)
expect(result.current.data?.dailyUsedGrams).toBe(15)
})
it("is disabled when memberId is empty", async () => {
const { result } = renderHook(() => useQuotaQuery(""), {
wrapper: createWrapper(),
})
// Should not fetch
expect(result.current.fetchStatus).toBe("idle")
})
})
describe("useCreateDistributionMutation", () => {
it("calls POST and returns new distribution", async () => {
const { result } = renderHook(() => useCreateDistributionMutation(), {
wrapper: createWrapper(),
})
result.current.mutate({
memberId: "m1",
batchId: "b1",
amountGrams: 5.0,
})
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data?.memberId).toBe("m1")
expect(result.current.data?.batchId).toBe("b1")
})
})