42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
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 <output>{value}</output>;
|
|
};
|
|
|
|
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(<DraftFixture initial="in-progress" />);
|
|
|
|
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(<DraftFixture initial="empty" />);
|
|
|
|
expect(restored.getByText("in-progress")).toBeInTheDocument();
|
|
expect(sessionStorage.getItem("tjwater-session-recovery:fixture")).toBeNull();
|
|
});
|
|
});
|