59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { renderHook, waitFor } from "@testing-library/react";
|
|
import { HttpResponse, http } from "msw";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { configureAuthClient } from "@/shared/api";
|
|
import { createQueryWrapper } from "@/test/query";
|
|
import { server } from "@/test/server";
|
|
import { usePortfolio } from "./usePortfolio";
|
|
|
|
describe("usePortfolio", () => {
|
|
it("uses the account endpoint with a 30 second cache and no retry", async () => {
|
|
configureAuthClient({
|
|
accessToken: () => "token",
|
|
refresh: async () => true,
|
|
clear: vi.fn(),
|
|
});
|
|
const { result } = renderHook(() => usePortfolio("account-1"), {
|
|
wrapper: createQueryWrapper().wrapper,
|
|
});
|
|
|
|
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
expect(result.current.data).toMatchObject({ positions: [] });
|
|
});
|
|
|
|
it("keeps the current connection snapshot after a background refetch error", async () => {
|
|
configureAuthClient({
|
|
accessToken: () => "token",
|
|
refresh: async () => true,
|
|
clear: vi.fn(),
|
|
});
|
|
let calls = 0;
|
|
server.use(
|
|
http.get("*/api/v1/brokerage-accounts/:accountId/portfolio", () => {
|
|
calls += 1;
|
|
return calls === 1
|
|
? HttpResponse.json({
|
|
lastSuccessfulAt: "2026-08-14T00:00:00Z",
|
|
isStale: false,
|
|
totalsByCurrency: [],
|
|
positions: [],
|
|
cashBalances: [],
|
|
})
|
|
: HttpResponse.json({ code: "TBANK_UNAVAILABLE" }, { status: 503 });
|
|
}),
|
|
);
|
|
const { result } = renderHook(() => usePortfolio("account-1"), {
|
|
wrapper: createQueryWrapper().wrapper,
|
|
});
|
|
|
|
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
void result.current.refetch();
|
|
await waitFor(() => expect(result.current.isError).toBe(true));
|
|
|
|
expect(result.current.data).toMatchObject({
|
|
lastSuccessfulAt: "2026-08-14T00:00:00Z",
|
|
});
|
|
});
|
|
});
|