refactor: enforce frontend view model boundaries
This commit is contained in:
@@ -19,11 +19,14 @@ function auth() {
|
||||
refresh: () => Promise.resolve(true),
|
||||
});
|
||||
}
|
||||
|
||||
describe("page-owned screen synchronization", () => {
|
||||
it("ignores a late sync acceptance after the account scope changes", async () => {
|
||||
auth();
|
||||
|
||||
const { promise: blocked, resolve: release } = Promise.withResolvers<void>();
|
||||
const { promise: blocked, resolve: release }
|
||||
= Promise.withResolvers<void>();
|
||||
|
||||
const posted: string[] = [];
|
||||
const polled: string[] = [];
|
||||
server.use(
|
||||
@@ -32,8 +35,10 @@ describe("page-owned screen synchronization", () => {
|
||||
accountId: string;
|
||||
screen: "account";
|
||||
};
|
||||
|
||||
posted.push(scope.accountId);
|
||||
await blocked;
|
||||
|
||||
return HttpResponse.json(
|
||||
{ id: scope.accountId, now: "2026-09-25T00:00:00Z", scope },
|
||||
{ status: 202 },
|
||||
@@ -41,6 +46,7 @@ describe("page-owned screen synchronization", () => {
|
||||
}),
|
||||
http.get("*/api/v1/tbank-sync/:id", ({ params }) => {
|
||||
polled.push(String(params.id));
|
||||
|
||||
return HttpResponse.json({
|
||||
id: params.id,
|
||||
resources: [],
|
||||
@@ -48,6 +54,7 @@ describe("page-owned screen synchronization", () => {
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const { rerender, result } = renderHook(
|
||||
({ accountId }) => useScreenSync({ accountId, screen: "account" }, []),
|
||||
{
|
||||
@@ -55,6 +62,7 @@ describe("page-owned screen synchronization", () => {
|
||||
wrapper: createQueryWrapper().wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
let pending!: Promise<void>;
|
||||
act(() => {
|
||||
pending = result.current.refresh();
|
||||
@@ -79,6 +87,7 @@ describe("page-owned screen synchronization", () => {
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
|
||||
modes.push(new URL(request.url).searchParams.get("sync"));
|
||||
|
||||
return isUnavailable
|
||||
? HttpResponse.json({ code: "UNAVAILABLE" }, { status: 503 })
|
||||
: HttpResponse.json(
|
||||
@@ -87,14 +96,17 @@ describe("page-owned screen synchronization", () => {
|
||||
}),
|
||||
http.post("*/api/v1/tbank-sync", () => {
|
||||
posts += 1;
|
||||
|
||||
return new HttpResponse(null, { status: 500 });
|
||||
}),
|
||||
);
|
||||
const { queryClient, wrapper } = createQueryWrapper();
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
const disabled = useStockQuote("");
|
||||
|
||||
return {
|
||||
accounts,
|
||||
disabled,
|
||||
@@ -103,6 +115,7 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper },
|
||||
);
|
||||
|
||||
if (cached) {
|
||||
await waitFor(() =>
|
||||
// eslint-disable-next-line vitest/no-conditional-expect -- The fixed table case selects the expected outcome; every case asserts its own branch.
|
||||
@@ -111,6 +124,7 @@ describe("page-owned screen synchronization", () => {
|
||||
isUnavailable = true;
|
||||
await act(() => result.current.accounts.refetch());
|
||||
}
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.isError).toBe(true));
|
||||
isUnavailable = false;
|
||||
await act(() => result.current.sync.retry());
|
||||
@@ -119,7 +133,14 @@ describe("page-owned screen synchronization", () => {
|
||||
cached ? "3" : "2",
|
||||
);
|
||||
expect(modes).toEqual(
|
||||
cached ? [null, /* eslint-disable-line unicorn/no-null -- Exercise the explicit null returned by the production API/DOM contract. */ null, "observe"] : [null, "observe"],
|
||||
cached
|
||||
? [
|
||||
null, // eslint-disable-line unicorn/no-null -- Preserve explicit null in the API fixture.
|
||||
// eslint-disable-next-line unicorn/no-null -- Exercise the explicit null returned by the production API/DOM contract.
|
||||
null,
|
||||
"observe",
|
||||
]
|
||||
: [null, "observe"], // eslint-disable-line unicorn/no-null -- Preserve explicit null in the API fixture.
|
||||
);
|
||||
expect(posts).toBe(0);
|
||||
expect(result.current.disabled.data).toBeUndefined();
|
||||
@@ -141,7 +162,9 @@ describe("page-owned screen synchronization", () => {
|
||||
async (boundary, pending) => {
|
||||
auth();
|
||||
|
||||
const { promise: blocked, resolve: release } = Promise.withResolvers<void>();
|
||||
const { promise: blocked, resolve: release }
|
||||
= Promise.withResolvers<void>();
|
||||
|
||||
let isRecovering = false;
|
||||
let isChanged = false;
|
||||
server.use(
|
||||
@@ -154,18 +177,22 @@ describe("page-owned screen synchronization", () => {
|
||||
: HttpResponse.json({ code: "UNAVAILABLE" }, { status: 503 });
|
||||
isRecovering = true;
|
||||
await blocked;
|
||||
|
||||
return HttpResponse.json(resource([], { stateVersion: "old" }));
|
||||
}),
|
||||
);
|
||||
const { queryClient, wrapper } = createQueryWrapper();
|
||||
const published: unknown[] = [];
|
||||
|
||||
const unsubscribe = queryClient.getQueryCache().subscribe((event) => {
|
||||
if (event.type === "updated" && event.action.type === "success")
|
||||
published.push(event.query.state.data);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
|
||||
return {
|
||||
accounts,
|
||||
sync: useScreenSync({ screen: "accounts" }, [accounts]),
|
||||
@@ -173,6 +200,7 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.isError).toBe(true));
|
||||
let recovery!: Promise<void>;
|
||||
act(() => {
|
||||
@@ -182,6 +210,7 @@ describe("page-owned screen synchronization", () => {
|
||||
if (pending) await waitFor(() => expect(isRecovering).toBe(true));
|
||||
await act(async () => {
|
||||
isChanged = true;
|
||||
|
||||
switch (boundary) {
|
||||
case "access": {
|
||||
invalidateResourceAccess(queryClient, "brokerage", {
|
||||
@@ -190,24 +219,29 @@ describe("page-owned screen synchronization", () => {
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "logout": {
|
||||
configureAuthClient();
|
||||
queryClient.clear();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "session": {
|
||||
auth();
|
||||
queryClient.clear();
|
||||
|
||||
break;
|
||||
}
|
||||
case "connection": { commitTbankConnectionBoundary(queryClient, {
|
||||
connected: true,
|
||||
connectedAt: "2026-09-27T00:00:00Z",
|
||||
});
|
||||
|
||||
case "connection": {
|
||||
commitTbankConnectionBoundary(queryClient, {
|
||||
connected: true,
|
||||
connectedAt: "2026-09-27T00:00:00Z",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
release();
|
||||
await recovery;
|
||||
});
|
||||
@@ -233,6 +267,7 @@ describe("page-owned screen synchronization", () => {
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
|
||||
modes.push(new URL(request.url).searchParams.get("sync"));
|
||||
|
||||
return modes.length === 1
|
||||
? HttpResponse.json({ code: "UNAVAILABLE" }, { status: 503 })
|
||||
: HttpResponse.json(resource([]));
|
||||
@@ -242,6 +277,7 @@ describe("page-owned screen synchronization", () => {
|
||||
),
|
||||
http.get("*/api/v1/tbank-sync/:id", () => {
|
||||
polls += 1;
|
||||
|
||||
return retryStatus && polls === 1
|
||||
? HttpResponse.json({ code: "UNAVAILABLE" }, { status: 503 })
|
||||
: HttpResponse.json({
|
||||
@@ -252,10 +288,12 @@ describe("page-owned screen synchronization", () => {
|
||||
}),
|
||||
);
|
||||
const { queryClient, wrapper } = createQueryWrapper();
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
const disabled = useStockQuote("");
|
||||
|
||||
return {
|
||||
accounts,
|
||||
sync: useScreenSync({ screen: "accounts" }, [accounts, disabled]),
|
||||
@@ -263,9 +301,11 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.isError).toBe(true));
|
||||
expect(result.current.accounts.meta).toBeUndefined();
|
||||
await act(() => result.current.sync.refresh());
|
||||
|
||||
if (retryStatus) {
|
||||
await waitFor(() =>
|
||||
// eslint-disable-next-line vitest/no-conditional-expect -- The fixed table case selects the expected outcome; every case asserts its own branch.
|
||||
@@ -273,6 +313,7 @@ describe("page-owned screen synchronization", () => {
|
||||
);
|
||||
await act(() => result.current.sync.retry());
|
||||
}
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.isSuccess).toBe(true));
|
||||
expect(result.current.accounts.data).toEqual([]);
|
||||
// eslint-disable-next-line unicorn/no-null -- Preserve the API/DOM null sentinel in the expected response.
|
||||
@@ -290,16 +331,20 @@ describe("page-owned screen synchronization", () => {
|
||||
const bodies: unknown[] = [];
|
||||
let isCompleted = false;
|
||||
let polls = 0;
|
||||
|
||||
const meta = () =>
|
||||
resource([], { stateVersion: isCompleted ? "2" : "1" }).meta;
|
||||
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
|
||||
modes.push(new URL(request.url).searchParams.get("sync"));
|
||||
|
||||
return HttpResponse.json(resource([], meta()));
|
||||
}),
|
||||
http.post("*/api/v1/tbank-sync", async ({ request }) => {
|
||||
bodies.push(await request.json());
|
||||
isCompleted = true;
|
||||
|
||||
return HttpResponse.json(
|
||||
{
|
||||
id: "screen",
|
||||
@@ -311,7 +356,10 @@ describe("page-owned screen synchronization", () => {
|
||||
}),
|
||||
http.get("*/api/v1/tbank-sync/:id", () => {
|
||||
polls++;
|
||||
return HttpResponse.json((/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
|
||||
/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
|
||||
return HttpResponse.json(
|
||||
{
|
||||
errorCode: null,
|
||||
id: "screen",
|
||||
@@ -320,13 +368,16 @@ describe("page-owned screen synchronization", () => {
|
||||
resources: [meta()],
|
||||
scope: { screen: "accounts" },
|
||||
state: "succeeded",
|
||||
}
|
||||
/* eslint-enable unicorn/no-null */));
|
||||
},
|
||||
/* eslint-enable unicorn/no-null */
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const { result, unmount } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
|
||||
return {
|
||||
accounts,
|
||||
sync: useScreenSync({ screen: "accounts" }, [accounts]),
|
||||
@@ -334,6 +385,7 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper: createQueryWrapper().wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.data).toEqual([]));
|
||||
await act(async () => {
|
||||
await Promise.all([
|
||||
@@ -348,6 +400,7 @@ describe("page-owned screen synchronization", () => {
|
||||
// eslint-disable-next-line unicorn/no-null -- Preserve the API/DOM null sentinel in the expected response.
|
||||
expect(modes).toEqual([null, "observe"]);
|
||||
vi.useFakeTimers();
|
||||
|
||||
try {
|
||||
await act(() => vi.advanceTimersByTimeAsync(120_000));
|
||||
expect(polls).toBe(1);
|
||||
@@ -365,26 +418,36 @@ describe("page-owned screen synchronization", () => {
|
||||
"publishes completed empty coverage while history runs, then stops on %s",
|
||||
async (boundary) => {
|
||||
vi.useFakeTimers({ toFake: ["Date"] });
|
||||
|
||||
try {
|
||||
auth();
|
||||
let isCompleted = false;
|
||||
let polls = 0;
|
||||
const modes: (null | string)[] = [];
|
||||
|
||||
const snapshot = () =>
|
||||
resource(isCompleted ? [] : /* eslint-disable-line unicorn/no-null -- Exercise the explicit null returned by the production API/DOM contract. */ null, (/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
resource(
|
||||
isCompleted
|
||||
? []
|
||||
: null, // eslint-disable-line unicorn/no-null -- Preserve the nullable API fixture.
|
||||
{
|
||||
stateVersion: isCompleted ? "covered" : "initial",
|
||||
sync: { errorCode: null, id: "history", state: "running" },
|
||||
}
|
||||
/* eslint-enable unicorn/no-null */));
|
||||
sync: { errorCode: null, id: "history", state: "running" }, // eslint-disable-line unicorn/no-null -- Preserve explicit null in the API fixture.
|
||||
},
|
||||
);
|
||||
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
|
||||
modes.push(new URL(request.url).searchParams.get("sync"));
|
||||
|
||||
return HttpResponse.json(snapshot());
|
||||
}),
|
||||
http.get("*/api/v1/tbank-sync/:id", () => {
|
||||
polls++;
|
||||
return HttpResponse.json((/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
|
||||
/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
|
||||
return HttpResponse.json(
|
||||
{
|
||||
errorCode: null,
|
||||
id: "history",
|
||||
@@ -397,13 +460,16 @@ describe("page-owned screen synchronization", () => {
|
||||
resources: [snapshot().meta],
|
||||
scope: null,
|
||||
state: "running",
|
||||
}
|
||||
/* eslint-enable unicorn/no-null */));
|
||||
},
|
||||
/* eslint-enable unicorn/no-null */
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const { result, unmount } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
|
||||
return {
|
||||
accounts,
|
||||
sync: useScreenSync({ screen: "accounts" }, [accounts]),
|
||||
@@ -411,6 +477,7 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper: createQueryWrapper().wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(result.current.accounts.meta?.sync.id).toBe("history"),
|
||||
);
|
||||
@@ -447,20 +514,25 @@ describe("page-owned screen synchronization", () => {
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", () =>
|
||||
HttpResponse.json(
|
||||
resource([], (/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
resource(
|
||||
/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
[],
|
||||
{
|
||||
sync: { errorCode: null, id: "revoked", state: "running" },
|
||||
}
|
||||
/* eslint-enable unicorn/no-null */)),
|
||||
},
|
||||
/* eslint-enable unicorn/no-null */
|
||||
),
|
||||
),
|
||||
),
|
||||
http.get("*/api/v1/tbank-sync/:id", () =>
|
||||
HttpResponse.json({ code: "INVALID_TBANK_TOKEN" }, { status: 400 }),
|
||||
),
|
||||
);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
|
||||
return {
|
||||
accounts,
|
||||
sync: useScreenSync({ screen: "accounts" }, [accounts]),
|
||||
@@ -468,6 +540,7 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper: createQueryWrapper().wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.isError).toBe(true));
|
||||
expect(result.current.accounts.data).toBeUndefined();
|
||||
});
|
||||
@@ -475,7 +548,10 @@ describe("page-owned screen synchronization", () => {
|
||||
auth();
|
||||
let isObserved = false;
|
||||
const modes: (null | string)[] = [];
|
||||
const dependency = resource([], (/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
|
||||
const dependency = resource(
|
||||
/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
[],
|
||||
{
|
||||
resourceKey: "operations",
|
||||
stateVersion: isObserved ? "covered" : "initial",
|
||||
@@ -484,24 +560,36 @@ describe("page-owned screen synchronization", () => {
|
||||
id: "dependency",
|
||||
state: isObserved ? "idle" : "running",
|
||||
},
|
||||
}
|
||||
/* eslint-enable unicorn/no-null */)).meta;
|
||||
},
|
||||
/* eslint-enable unicorn/no-null */
|
||||
).meta;
|
||||
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
|
||||
const mode = new URL(request.url).searchParams.get("sync");
|
||||
modes.push(mode);
|
||||
if (mode) isObserved = true;
|
||||
|
||||
return HttpResponse.json(
|
||||
resource(isObserved ? [] : /* eslint-disable-line unicorn/no-null -- Exercise the explicit null returned by the production API/DOM contract. */ null, {
|
||||
dependencies: [
|
||||
{ ...dependency, stateVersion: isObserved ? "covered" : "initial" },
|
||||
],
|
||||
resourceKey: "projection",
|
||||
}),
|
||||
resource(
|
||||
isObserved
|
||||
? []
|
||||
: null, // eslint-disable-line unicorn/no-null -- Exercise the explicit null returned by the production API/DOM contract.
|
||||
{
|
||||
dependencies: [
|
||||
{
|
||||
...dependency,
|
||||
stateVersion: isObserved ? "covered" : "initial",
|
||||
},
|
||||
],
|
||||
resourceKey: "projection",
|
||||
},
|
||||
),
|
||||
);
|
||||
}),
|
||||
http.get("*/api/v1/tbank-sync/:id", () =>
|
||||
HttpResponse.json((/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
HttpResponse.json(
|
||||
{
|
||||
errorCode: null,
|
||||
id: "dependency",
|
||||
@@ -510,18 +598,22 @@ describe("page-owned screen synchronization", () => {
|
||||
resources: [{ ...dependency, stateVersion: "covered" }],
|
||||
scope: null,
|
||||
state: "succeeded",
|
||||
}
|
||||
/* eslint-enable unicorn/no-null */)),
|
||||
},
|
||||
/* eslint-enable unicorn/no-null */
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
useScreenSync({ screen: "accounts" }, [accounts]);
|
||||
|
||||
return accounts;
|
||||
},
|
||||
{ wrapper: createQueryWrapper().wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.data).toEqual([]));
|
||||
// eslint-disable-next-line unicorn/no-null -- Preserve the API/DOM null sentinel in the expected response.
|
||||
expect(modes).toEqual([null, "observe"]);
|
||||
@@ -536,9 +628,11 @@ describe("page-owned screen synchronization", () => {
|
||||
HttpResponse.json({ code: "TBANK_UNAVAILABLE" }, { status: 503 }),
|
||||
),
|
||||
);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const accounts = useBrokerageAccounts();
|
||||
|
||||
return {
|
||||
accounts,
|
||||
sync: useScreenSync({ screen: "accounts" }, [accounts]),
|
||||
@@ -546,6 +640,7 @@ describe("page-owned screen synchronization", () => {
|
||||
},
|
||||
{ wrapper: createQueryWrapper().wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.accounts.data).toEqual([]));
|
||||
await act(() => result.current.sync.refresh());
|
||||
await waitFor(() => expect(result.current.sync.transportError).toBe(true));
|
||||
|
||||
Reference in New Issue
Block a user