refactor: centralize frontend auth query gating
This commit is contained in:
@@ -2,14 +2,22 @@ import { MantineProvider } from "@mantine/core";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { renderHook, waitFor } from "@testing-library/react";
|
||||
import type { ReactNode } from "react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { configureAuthClient } from "@/shared/api";
|
||||
import {
|
||||
brokerageOperationsQueryParams,
|
||||
useBrokerageOperations,
|
||||
} from "./useBrokerageOperations";
|
||||
|
||||
describe("useBrokerageOperations", () => {
|
||||
afterEach(() => configureAuthClient(null));
|
||||
|
||||
it("normalizes query parameters and loads a page without retries", async () => {
|
||||
configureAuthClient({
|
||||
accessToken: () => "token",
|
||||
refresh: async () => true,
|
||||
clear: vi.fn(),
|
||||
});
|
||||
expect(
|
||||
brokerageOperationsQueryParams({
|
||||
categories: ["tax", "income"],
|
||||
@@ -24,7 +32,7 @@ describe("useBrokerageOperations", () => {
|
||||
);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useBrokerageOperations("account-1", "token", { limit: 5 }),
|
||||
() => useBrokerageOperations("account-1", { limit: 5 }),
|
||||
{ wrapper },
|
||||
);
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
type OperationsEndpoint =
|
||||
@@ -37,17 +36,16 @@ export function brokerageOperationsQueryParams(
|
||||
|
||||
export function useBrokerageOperations(
|
||||
accountId: string | undefined,
|
||||
accessToken: string | null,
|
||||
query: BrokerageOperationsQuery = {},
|
||||
) {
|
||||
const params = brokerageOperationsQueryParams(query);
|
||||
return useQuery({
|
||||
return useAuthenticatedQuery({
|
||||
...apiQueryClient.queryOptions(
|
||||
"get",
|
||||
"/api/v1/brokerage-accounts/{accountId}/operations",
|
||||
{ params: { path: { accountId: accountId ?? "" }, query: params } },
|
||||
),
|
||||
enabled: Boolean(accountId && accessToken),
|
||||
enabled: Boolean(accountId),
|
||||
staleTime: 30_000,
|
||||
retry: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user