fix: stabilize frontend query cache contracts

This commit is contained in:
2026-08-23 15:20:08 +03:00
parent b285719c1b
commit 1250581ee3
12 changed files with 151 additions and 35 deletions
@@ -0,0 +1,99 @@
import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react";
import { HttpResponse, http } from "msw";
import { afterEach, describe, expect, it, vi } from "vitest";
import { apiQueryClient } from "@/shared/api";
import { server } from "@/test/server";
import { TbankConnectionSection } from "./TbankConnectionSection";
vi.mock("@/features/auth", () => ({
useAuth: () => ({ accessToken: "test-token" }),
}));
afterEach(cleanup);
const brokerageAccountsQueryKey = apiQueryClient.queryOptions(
"get",
"/api/v1/brokerage-accounts",
).queryKey;
function renderSection(client: QueryClient) {
return render(
<MantineProvider>
<QueryClientProvider client={client}>
<TbankConnectionSection />
</QueryClientProvider>
</MantineProvider>,
);
}
function seededClient() {
const client = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
client.setQueryData(brokerageAccountsQueryKey, [
{
id: "account-1",
name: "Основной счет",
type: "BROKERAGE",
openedOn: null,
},
]);
return client;
}
describe("TbankConnectionSection", () => {
it("updates the generated status cache and invalidates accounts after connecting", async () => {
const client = seededClient();
renderSection(client);
await screen.findByText("Не подключено");
fireEvent.change(screen.getByLabelText("Token T-Bank"), {
target: { value: "replacement-token" },
});
fireEvent.click(screen.getByRole("button", { name: "Подключить" }));
await waitFor(() =>
expect(
screen.getByRole("button", { name: "Заменить token" }),
).toBeInTheDocument(),
);
expect(client.getQueryState(brokerageAccountsQueryKey)?.isInvalidated).toBe(
true,
);
});
it("updates the generated status cache and invalidates accounts after deleting", async () => {
server.use(
http.get("*/api/v1/tbank-connection", () =>
HttpResponse.json({
connected: true,
connectedAt: "2026-08-14T00:00:00Z",
}),
),
);
const client = seededClient();
renderSection(client);
fireEvent.click(
await screen.findByRole("button", { name: "Удалить подключение" }),
);
fireEvent.click(await screen.findByRole("button", { name: /^Удалить$/ }));
await waitFor(() =>
expect(
screen.getByRole("button", { name: "Подключить" }),
).toBeInTheDocument(),
);
expect(client.getQueryState(brokerageAccountsQueryKey)?.isInvalidated).toBe(
true,
);
});
});