Skip to content

Commit 53ee871

Browse files
test(hooks): add unit tests for useIncomingStreams
1 parent 65d1429 commit 53ee871

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { renderHook, waitFor, act } from "@testing-library/react";
2+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
3+
import { vi, describe, it, expect, beforeEach, afterEach } from "vitest";
4+
import React from "react";
5+
import {
6+
useIncomingStreams,
7+
useWithdrawIncomingStream,
8+
incomingStreamsQueryKey,
9+
} from "./useIncomingStreams";
10+
import { fetchIncomingStreams } from "@/lib/api/streams";
11+
import { withdrawFromStream } from "@/lib/soroban";
12+
13+
vi.mock("@/lib/api/streams", () => ({
14+
fetchIncomingStreams: vi.fn(),
15+
}));
16+
17+
vi.mock("@/lib/soroban", () => ({
18+
withdrawFromStream: vi.fn(),
19+
}));
20+
21+
describe("useIncomingStreams hooks", () => {
22+
let queryClient: QueryClient;
23+
24+
beforeEach(() => {
25+
queryClient = new QueryClient({
26+
defaultOptions: {
27+
queries: { retry: false },
28+
},
29+
});
30+
vi.clearAllMocks();
31+
});
32+
33+
afterEach(() => {
34+
queryClient.clear();
35+
});
36+
37+
const wrapper = ({ children }: { children: React.ReactNode }) => (
38+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
39+
);
40+
41+
describe("incomingStreamsQueryKey", () => {
42+
it("returns correct shape", () => {
43+
expect(incomingStreamsQueryKey("pubkey")).toEqual([
44+
"incoming-streams",
45+
"pubkey",
46+
]);
47+
expect(incomingStreamsQueryKey(null)).toEqual(["incoming-streams", null]);
48+
});
49+
});
50+
51+
describe("useIncomingStreams", () => {
52+
it("stays disabled when publicKey is null/undefined", () => {
53+
const { result, rerender } = renderHook(
54+
(props: { publicKey: string | null | undefined }) =>
55+
useIncomingStreams(props.publicKey),
56+
{ wrapper, initialProps: { publicKey: null } }
57+
);
58+
59+
expect(result.current.isPending).toBe(true);
60+
expect(result.current.fetchStatus).toBe("idle");
61+
expect(fetchIncomingStreams).not.toHaveBeenCalled();
62+
63+
rerender({ publicKey: undefined });
64+
expect(result.current.fetchStatus).toBe("idle");
65+
expect(fetchIncomingStreams).not.toHaveBeenCalled();
66+
});
67+
});
68+
69+
describe("useWithdrawIncomingStream", () => {
70+
it("rejects when session is null", async () => {
71+
const { result } = renderHook(
72+
() => useWithdrawIncomingStream(null, "pubkey"),
73+
{ wrapper }
74+
);
75+
76+
await expect(
77+
result.current.mutateAsync({} as any)
78+
).rejects.toThrow("Please connect your wallet first");
79+
expect(withdrawFromStream).not.toHaveBeenCalled();
80+
});
81+
82+
it("invalidates incomingStreamsQueryKey(publicKey) on success", async () => {
83+
(withdrawFromStream as any).mockResolvedValue({ status: "success" });
84+
(fetchIncomingStreams as any).mockResolvedValue([]);
85+
86+
const { result } = renderHook(
87+
() => useWithdrawIncomingStream({} as any, "pubkey"),
88+
{ wrapper }
89+
);
90+
91+
const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
92+
93+
await act(async () => {
94+
await result.current.mutateAsync({
95+
id: 1,
96+
streamId: 1,
97+
withdrawn: 0,
98+
deposited: 100,
99+
ratePerSecond: 1,
100+
isPaused: false,
101+
lastUpdateTime: Date.now() / 1000,
102+
} as any);
103+
});
104+
105+
// Wait for pollIndexerForWithdraw to complete and call invalidateQueries
106+
await waitFor(() => {
107+
expect(invalidateSpy).toHaveBeenCalledWith({
108+
queryKey: incomingStreamsQueryKey("pubkey"),
109+
});
110+
}, { timeout: 10000 });
111+
});
112+
});
113+
});

0 commit comments

Comments
 (0)