File size: 1,792 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import { act } from "react";
import { hydrateRoot, type Root } from "react-dom/client";
import { renderToString } from "react-dom/server";
import { afterEach, describe, expect, it, vi } from "vitest";
const auth = vi.hoisted(() => ({
session: {
data: null as null | { user: { id: string } },
isPending: true,
error: null,
refetch: vi.fn(),
},
}));
vi.mock("better-auth/react", () => ({
createAuthClient: () => ({
signIn: vi.fn(), signOut: vi.fn(), signUp: vi.fn(),
useSession: () => auth.session,
}),
}));
import { useSession } from "./auth-client";
function SessionView() {
const session = useSession();
return <div>{session.isPending ? "Loading" : session.data ? "Signed in" : "Sign in required"}</div>;
}
let root: Root | undefined;
afterEach(async () => {
if (root) await act(async () => root?.unmount());
root = undefined;
document.body.innerHTML = "";
});
describe("session hydration", () => {
it.each([null, { user: { id: "user" } }])("hydrates when a sibling has already resolved the session: %j", async (data) => {
auth.session = { ...auth.session, data: null, isPending: true };
const container = document.createElement("div");
container.innerHTML = renderToString(<SessionView />);
document.body.append(container);
expect(container.textContent).toBe("Loading");
// Better Auth's shared store resolves before this server-rendered tree loads.
auth.session = { ...auth.session, data, isPending: false };
const onRecoverableError = vi.fn();
await act(async () => {
root = hydrateRoot(container, <SessionView />, { onRecoverableError });
});
expect(onRecoverableError).not.toHaveBeenCalled();
expect(container.textContent).toBe(data ? "Signed in" : "Sign in required");
});
});
|