mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(forms): keep one Idempotency-Key for the remaining create forms
A screening, an illness episode, a life event, a symptom, a custom metric or an address-book entry created on a weak signal could be stored twice: the POST committed, the answer missed the timeout, and the second tap went out under a new key. Their routes already honour the key; the forms now post through useIdempotentPost like the other create forms, so the retry replays the first answer. The screening's save moves into a small hook beside MentalWellbeing, which sat at the function-size cap and now drops below it; the custom metric form's create-or-update call moves into one as well.
This commit is contained in:
1 parent
4485dfb3a9
commit
e68041a3af
8 files changed
+102
-38
No files matched your search
@@ -1,9 +1,11 @@
|
||||
/**
|
||||
* An episode save settles once the server stored it.
|
||||
* An episode save settles once the server stored it, and stores it once.
|
||||
*
|
||||
* Every episode hook's `onSuccess` used to return the refresh of the illness
|
||||
* reads and the dashboard. A mutation waits for what `onSuccess` returns, so
|
||||
* the episode sheet kept its spinner until each of those reads had refetched.
|
||||
* And a create retried after a timeout went out under a new key, so the
|
||||
* server stored the episode twice.
|
||||
*/
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
@@ -14,6 +16,11 @@ const client = vi.hoisted(() => ({
|
||||
getQueryCache: () => ({ findAll: () => [] }),
|
||||
}));
|
||||
|
||||
vi.mock("react", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("react")>()),
|
||||
useRef: <T>(initial: T) => ({ current: initial }),
|
||||
}));
|
||||
|
||||
vi.mock("@tanstack/react-query", () => ({
|
||||
// The hook's options are what is under test; return them as they are.
|
||||
useQuery: (options: unknown) => options,
|
||||
@@ -29,6 +36,7 @@ vi.mock("@/lib/api/api-fetch", () => ({
|
||||
apiDelete: vi.fn(),
|
||||
}));
|
||||
|
||||
import { apiPost } from "@/lib/api/api-fetch";
|
||||
import {
|
||||
useCreateEpisode,
|
||||
useDeleteEpisode,
|
||||
@@ -54,3 +62,26 @@ describe("episode saves", () => {
|
||||
expect(client.invalidateQueries).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("creating an episode", () => {
|
||||
it("retries under the same Idempotency-Key and starts the next one fresh", async () => {
|
||||
const create = useCreateEpisode() as unknown as {
|
||||
mutationFn: (input: unknown) => Promise<unknown>;
|
||||
};
|
||||
const post = vi.mocked(apiPost);
|
||||
post.mockRejectedValueOnce(new TypeError("timed out"));
|
||||
post.mockResolvedValue({ id: "e1" });
|
||||
const keyOf = (call: number) =>
|
||||
new Headers((post.mock.calls[call]![2] as RequestInit).headers).get(
|
||||
"Idempotency-Key",
|
||||
);
|
||||
|
||||
await expect(create.mutationFn({ name: "Flu" })).rejects.toThrow();
|
||||
await create.mutationFn({ name: "Flu" });
|
||||
await create.mutationFn({ name: "Cold" });
|
||||
|
||||
expect(keyOf(0)).toBeTruthy();
|
||||
expect(keyOf(1)).toBe(keyOf(0));
|
||||
expect(keyOf(2)).not.toBe(keyOf(0));
|
||||
});
|
||||
});
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
apiPatch,
|
||||
apiPost,
|
||||
} from "@/lib/api/api-fetch";
|
||||
import { useIdempotentPost } from "@/lib/api/use-idempotent-post";
|
||||
import {
|
||||
illnessDependentKeys,
|
||||
invalidateKeys,
|
||||
@@ -164,9 +165,12 @@ function useInvalidateIllness() {
|
||||
/** Create an episode. */
|
||||
export function useCreateEpisode() {
|
||||
const invalidate = useInvalidateIllness();
|
||||
// One key until a save lands, so a retry after a timeout is not a second
|
||||
// episode.
|
||||
const post = useIdempotentPost();
|
||||
return useMutation({
|
||||
mutationFn: (input: IllnessEpisodeCreateInput) =>
|
||||
apiPost<IllnessEpisodeDTO>("/api/illness/episodes", input),
|
||||
post<IllnessEpisodeDTO>("/api/illness/episodes", input),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user