refactor: centralize frontend auth query gating

This commit is contained in:
2026-08-25 14:29:41 +03:00
parent fff36f252a
commit 268acdf2e1
24 changed files with 352 additions and 82 deletions
@@ -0,0 +1,56 @@
import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import { HttpResponse, http } from "msw";
import type { ReactNode } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import { server } from "@/test/server";
import { useBrokerageAccounts } from "./useBrokerageAccounts";
function createWrapper() {
const client = new QueryClient();
return ({ children }: { children: ReactNode }) => (
<MantineProvider>
<QueryClientProvider client={client}>{children}</QueryClientProvider>
</MantineProvider>
);
}
describe("useBrokerageAccounts", () => {
afterEach(() => configureAuthClient(null));
it("loads accounts through the authenticated query seam", async () => {
configureAuthClient({
accessToken: () => "token",
refresh: async () => true,
clear: vi.fn(),
});
server.use(
http.get("*/api/v1/brokerage-accounts", () =>
HttpResponse.json([
{
id: "account-1",
name: "Основной",
type: "BROKERAGE",
openedOn: "2024-08-13",
},
]),
),
);
const { result } = renderHook(() => useBrokerageAccounts(), {
wrapper: createWrapper(),
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data).toEqual([
{
id: "account-1",
name: "Основной",
type: "BROKERAGE",
openedOn: "2024-08-13",
},
]);
});
});
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { apiQueryClient } from "@/shared/api";
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
import type { paths } from "@/shared/api/generated/schema";
const brokerageAccountsQueryOptions = () =>
@@ -11,10 +10,9 @@ export const brokerageAccountsQueryKey =
export type BrokerageAccount =
paths["/api/v1/brokerage-accounts"]["get"]["responses"][200]["content"]["application/json"][number];
export function useBrokerageAccounts(accessToken: string | null) {
return useQuery({
export function useBrokerageAccounts() {
return useAuthenticatedQuery({
...brokerageAccountsQueryOptions(),
enabled: Boolean(accessToken),
staleTime: 60_000,
retry: false,
});