refactor: centralize frontend auth query gating
This commit is contained in:
@@ -5,6 +5,7 @@ import { HttpResponse, http } from "msw";
|
||||
import { MemoryRouter, Route, Routes } from "react-router";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Portfolio } from "@/entities/portfolio";
|
||||
import { configureAuthClient } from "@/shared/api";
|
||||
import { server } from "@/test/server";
|
||||
import {
|
||||
AccountPage,
|
||||
@@ -19,10 +20,6 @@ import type {
|
||||
} from "./AccountPage.vm";
|
||||
import { toAccountPagePositionSections } from "./AccountPage.vm";
|
||||
|
||||
vi.mock("@/features/auth", () => ({
|
||||
useAuth: () => ({ accessToken: "test-token" }),
|
||||
}));
|
||||
|
||||
function renderPage(ui: React.ReactNode) {
|
||||
return render(
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
@@ -33,7 +30,16 @@ function renderPage(ui: React.ReactNode) {
|
||||
);
|
||||
}
|
||||
|
||||
function configureTestAuth() {
|
||||
configureAuthClient({
|
||||
accessToken: () => "test-token",
|
||||
refresh: async () => true,
|
||||
clear: vi.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
function renderAccountRoute(queryClient: QueryClient) {
|
||||
configureTestAuth();
|
||||
return render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MantineProvider>
|
||||
@@ -105,7 +111,10 @@ function emptyProps() {
|
||||
}
|
||||
|
||||
describe("AccountPage states", () => {
|
||||
afterEach(cleanup);
|
||||
afterEach(() => {
|
||||
configureAuthClient(null);
|
||||
cleanup();
|
||||
});
|
||||
it("renders loading and empty states", () => {
|
||||
renderPage(<AccountPageLoading />);
|
||||
expect(screen.getByRole("status")).toBeInTheDocument();
|
||||
|
||||
@@ -4,15 +4,12 @@ import { renderHook, waitFor } from "@testing-library/react";
|
||||
import { HttpResponse, http } from "msw";
|
||||
import type { ReactNode } from "react";
|
||||
import { MemoryRouter, Route, Routes } from "react-router";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Portfolio } from "@/entities/portfolio";
|
||||
import { configureAuthClient } from "@/shared/api";
|
||||
import { server } from "@/test/server";
|
||||
import { useAccountPageViewModel } from "./AccountPage.vm";
|
||||
|
||||
vi.mock("@/features/auth", () => ({
|
||||
useAuth: () => ({ accessToken: "test-token" }),
|
||||
}));
|
||||
|
||||
const portfolio: Portfolio = {
|
||||
lastSuccessfulAt: "2026-08-13T10:00:00Z",
|
||||
isStale: true,
|
||||
@@ -55,6 +52,14 @@ const portfolio: Portfolio = {
|
||||
cashBalances: [{ currency: "USD", value: "1" }],
|
||||
};
|
||||
|
||||
function configureTestAuth() {
|
||||
configureAuthClient({
|
||||
accessToken: () => "test-token",
|
||||
refresh: async () => true,
|
||||
clear: vi.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
function wrapper({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
@@ -121,7 +126,10 @@ function mockLoadedAccountPage() {
|
||||
}
|
||||
|
||||
describe("useAccountPageViewModel", () => {
|
||||
afterEach(() => configureAuthClient(null));
|
||||
|
||||
it("prepares grouped position sections with derived display data", async () => {
|
||||
configureTestAuth();
|
||||
mockLoadedAccountPage();
|
||||
|
||||
const { result } = renderHook(() => useAccountPageViewModel(), { wrapper });
|
||||
@@ -175,6 +183,7 @@ describe("useAccountPageViewModel", () => {
|
||||
});
|
||||
|
||||
it("prepares recent operations and events state for presentation views", async () => {
|
||||
configureTestAuth();
|
||||
mockLoadedAccountPage();
|
||||
|
||||
const { result } = renderHook(() => useAccountPageViewModel(), { wrapper });
|
||||
@@ -207,6 +216,7 @@ describe("useAccountPageViewModel", () => {
|
||||
});
|
||||
|
||||
it("keeps recent errors isolated in the ViewModel presentation state", async () => {
|
||||
configureTestAuth();
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts/:accountId/portfolio", () =>
|
||||
HttpResponse.json(portfolio),
|
||||
|
||||
@@ -18,7 +18,6 @@ import {
|
||||
portfolioErrorCode,
|
||||
usePortfolio,
|
||||
} from "@/entities/portfolio";
|
||||
import { useAuth } from "@/features/auth";
|
||||
|
||||
type PositionSectionKind = "stocks" | "bonds" | "other";
|
||||
type AccountPageReturnTone = "positive" | "negative";
|
||||
@@ -86,12 +85,11 @@ export type AccountPageViewModel =
|
||||
|
||||
export function useAccountPageViewModel(): AccountPageViewModel {
|
||||
const { accountId } = useParams();
|
||||
const { accessToken } = useAuth();
|
||||
const portfolio = usePortfolio(accountId, accessToken);
|
||||
const operations = useBrokerageOperations(accountId, accessToken, {
|
||||
const portfolio = usePortfolio(accountId);
|
||||
const operations = useBrokerageOperations(accountId, {
|
||||
limit: 5,
|
||||
});
|
||||
const events = useBrokerageEvents(accountId, accessToken);
|
||||
const events = useBrokerageEvents(accountId);
|
||||
const account = useQueryClient()
|
||||
.getQueryData<BrokerageAccount[]>(brokerageAccountsQueryKey)
|
||||
?.find((item) => item.id === accountId);
|
||||
|
||||
Reference in New Issue
Block a user