82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
import { http, HttpResponse } from "msw";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { useBrokerageAccounts } from "@/entities/brokerage-account";
|
|
import { configureAuthClient } from "@/shared/api";
|
|
import { createQueryWrapper } from "@/test/query";
|
|
import { resource } from "@/test/resource";
|
|
import { server } from "@/test/server";
|
|
|
|
import { useScreenSync } from "../index";
|
|
|
|
function auth() {
|
|
configureAuthClient({
|
|
accessToken: () => "synthetic",
|
|
clear: vi.fn(),
|
|
refresh: () => Promise.resolve(true),
|
|
});
|
|
}
|
|
|
|
describe("read-only screen synchronization", () => {
|
|
it("retries the resource with GET without requesting source observation", async () => {
|
|
auth();
|
|
let gets = 0;
|
|
const syncModes: (null | string)[] = [];
|
|
server.use(
|
|
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
|
|
gets += 1;
|
|
syncModes.push(new URL(request.url).searchParams.get("sync"));
|
|
|
|
return gets === 1
|
|
? HttpResponse.json({ code: "TBANK_UNAVAILABLE" }, { status: 503 })
|
|
: HttpResponse.json(resource([]));
|
|
}),
|
|
);
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
const accounts = useBrokerageAccounts();
|
|
|
|
return {
|
|
accounts,
|
|
sync: useScreenSync({ screen: "accounts" }, [accounts]),
|
|
};
|
|
},
|
|
{ wrapper: createQueryWrapper().wrapper },
|
|
);
|
|
|
|
await waitFor(() => expect(gets).toBe(1));
|
|
await act(async () => result.current.sync.retry());
|
|
await waitFor(() => expect(result.current.accounts.data).toEqual([]));
|
|
|
|
expect(gets).toBe(2);
|
|
expect(syncModes).toEqual([null, null]);
|
|
});
|
|
|
|
it("refreshes the currently open resources together", async () => {
|
|
auth();
|
|
let gets = 0;
|
|
server.use(
|
|
http.get("*/api/v1/brokerage-accounts", () => {
|
|
gets += 1;
|
|
|
|
return HttpResponse.json(resource([]));
|
|
}),
|
|
);
|
|
|
|
const { result } = renderHook(
|
|
() => {
|
|
const accounts = useBrokerageAccounts();
|
|
|
|
return useScreenSync({ screen: "accounts" }, [accounts]);
|
|
},
|
|
{ wrapper: createQueryWrapper().wrapper },
|
|
);
|
|
|
|
await waitFor(() => expect(gets).toBe(1));
|
|
await act(async () => result.current.refresh());
|
|
await waitFor(() => expect(gets).toBe(2));
|
|
});
|
|
});
|