import { act, render } from "@testing-library/react";
import { useState } from "react";
import { useSessionRecoveryDraft } from "./sessionRecoveryDraft";
import { useAuthStore } from "@/store/authStore";
const DraftFixture = ({ initial }: { initial: string }) => {
const [value, setValue] = useState(initial);
useSessionRecoveryDraft("fixture", { value }, (draft) => setValue(draft.value));
return ;
};
describe("useSessionRecoveryDraft", () => {
beforeEach(() => {
sessionStorage.clear();
useAuthStore.setState({
accessToken: null,
sessionExpired: false,
sessionExpiryReason: null,
});
});
it("saves an in-progress value when authentication expires and restores it once", () => {
const first = render();
act(() => {
useAuthStore.getState().markSessionExpired("unauthorized");
});
expect(sessionStorage.getItem("tjwater-session-recovery:fixture")).toBe(
JSON.stringify({ value: "in-progress" }),
);
first.unmount();
useAuthStore.getState().clearSessionExpired();
const restored = render();
expect(restored.getByText("in-progress")).toBeInTheDocument();
expect(sessionStorage.getItem("tjwater-session-recovery:fixture")).toBeNull();
});
});