Files
cannamanage/cannamanage-frontend/src/__tests__/services/distributions.test.tsx
T
Patrick Plate 599514c0db
Deploy to Production / test (push) Has been cancelled
Deploy to Production / deploy (push) Has been cancelled
feat(sprint-6): Phase 6 — Notifications (WebSocket) + PWA
- WebSocket: Spring STOMP + SockJS, NotificationService, persistent notifications table
- NotificationController: GET/PUT endpoints for notification management
- Frontend: notification bell with unread badge, dropdown panel, real-time via STOMP
- PWA: manifest.json, service worker (manual sw.js), offline page, install prompt
- PWA icons (192+512), dark theme colors, standalone display
- Full i18n (de/en) for notifications and PWA
- Flyway V10 migration for notifications table
- spring-boot-starter-websocket dependency added
2026-06-12 23:02:44 +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?.usedGrams).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")
})
})