mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 06:39:28 +08:00
Merge pull request #1880 from dajiaohuang/fix/handoff-effect-replay
fix(web): start handoff exchange after strict-mode timer replay
This commit is contained in:
@@ -40,8 +40,10 @@ function HandoffPageContent() {
|
||||
const code = searchParams.get("code")?.trim() ?? "";
|
||||
window.history.replaceState(null, "", window.location.pathname);
|
||||
if (!code || started.current) return;
|
||||
started.current = true;
|
||||
const timer = window.setTimeout(() => {
|
||||
// Effect cleanup may cancel this deferred start before it runs.
|
||||
// Mark the exchange as started only when it actually begins.
|
||||
started.current = true;
|
||||
void finish(code);
|
||||
}, 0);
|
||||
return () => window.clearTimeout(timer);
|
||||
|
||||
38
web/tests/handoff-effect-replay.spec.tsx
Normal file
38
web/tests/handoff-effect-replay.spec.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { render, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import HandoffPage from "@/app/handoff/page";
|
||||
|
||||
const runtime = vi.hoisted(() => ({ fetch: vi.fn(), replace: vi.fn() }));
|
||||
vi.mock("@/lib/api", () => ({ apiFetch: runtime.fetch, apiUrl: (url: string) => url }));
|
||||
vi.mock("next/navigation", () => ({
|
||||
useRouter: () => ({ replace: runtime.replace }),
|
||||
useSearchParams: () => new URLSearchParams("code=example-code&next=%2Fprofile"),
|
||||
}));
|
||||
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (value: string) => value }) }));
|
||||
|
||||
describe("automatic handoff sign-in", () => {
|
||||
it.each([false, true])("completes a code sign-in with strict mode %s", async strict => {
|
||||
runtime.replace.mockReset();
|
||||
runtime.fetch.mockReset().mockImplementation((url: string) => Promise.resolve(
|
||||
new Response(JSON.stringify(url.endsWith("/exchange") ? { ticket: "example-ticket" } : {})),
|
||||
));
|
||||
render(<HandoffPage />, { reactStrictMode: strict });
|
||||
await waitFor(() => expect(runtime.replace).toHaveBeenCalledWith("/profile"));
|
||||
expect(runtime.fetch).toHaveBeenCalledTimes(2);
|
||||
expect(JSON.parse(runtime.fetch.mock.calls[0][1].body)).toEqual({ code: "example-code" });
|
||||
expect(JSON.parse(runtime.fetch.mock.calls[1][1].body)).toEqual({ ticket: "example-ticket" });
|
||||
});
|
||||
|
||||
it("does not exchange the code after unmount before the deferred start", async () => {
|
||||
runtime.fetch.mockReset();
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const { unmount } = render(<HandoffPage />);
|
||||
unmount();
|
||||
await vi.runAllTimersAsync();
|
||||
expect(runtime.fetch).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user