refactor: align frontend ownership boundaries
Close audit gaps across API drift, query policy, UI, and tests.
This commit is contained in:
@@ -1,36 +1,21 @@
|
||||
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 { FC, PropsWithChildren } from "react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
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", () => {
|
||||
afterEach(() => configureAuthClient(null));
|
||||
|
||||
it("uses the account endpoint with a 30 second cache and no retry", async () => {
|
||||
configureAuthClient({
|
||||
accessToken: () => "token",
|
||||
refresh: async () => true,
|
||||
clear: vi.fn(),
|
||||
});
|
||||
const client = new QueryClient();
|
||||
const wrapper: FC<PropsWithChildren> = (props) => {
|
||||
const { children } = props;
|
||||
|
||||
return (
|
||||
<MantineProvider>
|
||||
<QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
</MantineProvider>
|
||||
);
|
||||
};
|
||||
|
||||
const { result } = renderHook(() => usePortfolio("account-1"), {
|
||||
wrapper,
|
||||
wrapper: createQueryWrapper().wrapper,
|
||||
});
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
@@ -58,16 +43,9 @@ describe("usePortfolio", () => {
|
||||
: HttpResponse.json({ code: "TBANK_UNAVAILABLE" }, { status: 503 });
|
||||
}),
|
||||
);
|
||||
const client = new QueryClient();
|
||||
const wrapper: FC<PropsWithChildren> = (props) => {
|
||||
const { children } = props;
|
||||
return (
|
||||
<MantineProvider>
|
||||
<QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
</MantineProvider>
|
||||
);
|
||||
};
|
||||
const { result } = renderHook(() => usePortfolio("account-1"), { wrapper });
|
||||
const { result } = renderHook(() => usePortfolio("account-1"), {
|
||||
wrapper: createQueryWrapper().wrapper,
|
||||
});
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
void result.current.refetch();
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
|
||||
import type { paths } from "@/shared/api/generated/schema";
|
||||
import {
|
||||
apiQueryClient,
|
||||
type paths,
|
||||
useAuthenticatedQuery,
|
||||
} from "@/shared/api";
|
||||
|
||||
const portfolioQueryPolicy = {
|
||||
staleTime: 30_000,
|
||||
retry: false,
|
||||
} as const;
|
||||
|
||||
type Portfolio =
|
||||
paths["/api/v1/brokerage-accounts/{accountId}/portfolio"]["get"]["responses"][200]["content"]["application/json"];
|
||||
|
||||
function portfolioQueryOptions(accountId: string | undefined) {
|
||||
return apiQueryClient.queryOptions(
|
||||
"get",
|
||||
"/api/v1/brokerage-accounts/{accountId}/portfolio",
|
||||
{ params: { path: { accountId: accountId ?? "" } } },
|
||||
);
|
||||
}
|
||||
|
||||
function usePortfolio(accountId: string | undefined) {
|
||||
return useAuthenticatedQuery({
|
||||
...apiQueryClient.queryOptions(
|
||||
"get",
|
||||
"/api/v1/brokerage-accounts/{accountId}/portfolio",
|
||||
{ params: { path: { accountId: accountId ?? "" } } },
|
||||
),
|
||||
...portfolioQueryOptions(accountId),
|
||||
enabled: Boolean(accountId),
|
||||
...portfolioQueryPolicy,
|
||||
});
|
||||
}
|
||||
|
||||
export { type Portfolio, usePortfolio };
|
||||
export { type Portfolio, portfolioQueryOptions, usePortfolio };
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
export { type Portfolio, usePortfolio } from "./api/usePortfolio";
|
||||
export {
|
||||
formatMoney,
|
||||
formatShare,
|
||||
formatSnapshotTime,
|
||||
instrumentLogoUrl,
|
||||
portfolioErrorCode,
|
||||
} from "./model/display";
|
||||
type Portfolio,
|
||||
portfolioQueryOptions,
|
||||
usePortfolio,
|
||||
} from "./api/usePortfolio";
|
||||
export { formatShare } from "./model/display";
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { formatMoney, formatShare, instrumentLogoUrl } from "./display";
|
||||
import { formatShare } from "./display";
|
||||
|
||||
describe("portfolio display helpers", () => {
|
||||
it("formats decimal strings without financial arithmetic", () => {
|
||||
expect(formatMoney({ value: "-1234567.89", currency: "RUB" })).toBe(
|
||||
"-1 234 567,89 ₽",
|
||||
);
|
||||
expect(formatMoney({ value: "10", currency: "RUB" })).toBe("10 ₽");
|
||||
expect(formatMoney({ value: "0.5", currency: "USD" })).toBe("0,5 USD");
|
||||
expect(formatMoney(null)).toBe("—");
|
||||
});
|
||||
|
||||
it("formats a share as a display-only percentage", () => {
|
||||
expect(
|
||||
formatShare(
|
||||
@@ -33,15 +24,4 @@ describe("portfolio display helpers", () => {
|
||||
),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("builds the brand logo url from an upstream file name", () => {
|
||||
expect(instrumentLogoUrl("sber3.png")).toBe(
|
||||
"https://invest-brands.cdn-tinkoff.ru/sber3x160.png",
|
||||
);
|
||||
expect(instrumentLogoUrl(" RU000A0JPNM1.png ")).toBe(
|
||||
"https://invest-brands.cdn-tinkoff.ru/RU000A0JPNM1x160.png",
|
||||
);
|
||||
expect(instrumentLogoUrl(null)).toBeNull();
|
||||
expect(instrumentLogoUrl(" ")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,21 +2,6 @@ import type { Portfolio } from "../api/usePortfolio";
|
||||
|
||||
type Money = Portfolio["totalsByCurrency"][number];
|
||||
|
||||
function formatDecimal(value: string): string {
|
||||
const [integer, fraction] = value.split(".");
|
||||
const sign = integer.startsWith("-") ? "-" : "";
|
||||
const grouped = (sign ? integer.slice(1) : integer).replace(
|
||||
/\B(?=(\d{3})+(?!\d))/g,
|
||||
" ",
|
||||
);
|
||||
return `${sign}${grouped}${fraction ? `,${fraction}` : ""}`;
|
||||
}
|
||||
|
||||
function formatMoney(money: Money | null): string {
|
||||
if (!money) return "—";
|
||||
return `${formatDecimal(money.value)} ${money.currency === "RUB" ? "₽" : money.currency}`;
|
||||
}
|
||||
|
||||
function formatShare(
|
||||
numerator: Money | null,
|
||||
denominator: Money | null,
|
||||
@@ -32,33 +17,4 @@ function formatShare(
|
||||
return `${rounded} %`;
|
||||
}
|
||||
|
||||
function formatSnapshotTime(value: string): string {
|
||||
return new Intl.DateTimeFormat("ru-RU", {
|
||||
dateStyle: "medium",
|
||||
timeStyle: "short",
|
||||
timeZone: "UTC",
|
||||
}).format(new Date(value));
|
||||
}
|
||||
|
||||
function portfolioErrorCode(error: unknown): string | undefined {
|
||||
return typeof error === "object" && error !== null && "code" in error
|
||||
? String(error.code)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
const logoSize = 160;
|
||||
|
||||
function instrumentLogoUrl(logoName: string | null | undefined): string | null {
|
||||
const stem = logoName?.trim().replace(/\.png$/i, "");
|
||||
return stem
|
||||
? `https://invest-brands.cdn-tinkoff.ru/${stem}x${logoSize}.png`
|
||||
: null;
|
||||
}
|
||||
|
||||
export {
|
||||
formatMoney,
|
||||
formatShare,
|
||||
formatSnapshotTime,
|
||||
instrumentLogoUrl,
|
||||
portfolioErrorCode,
|
||||
};
|
||||
export { formatShare };
|
||||
|
||||
Reference in New Issue
Block a user