Files
HealthLog/src/components/medications/sections/__tests__/inventory-section.test.tsx
T
Marc-André Bombeck 6609ce0dc7 fix(medications): evict supply caches so the card reflects stock at once
The /medications card and table render the dose-derived stock
(stockUnitsRemaining / stockDosesRemaining) straight from the
GET /api/medications list payload, which reads through cachedSwr on the
60s-fresh + 10-min-stale medications bucket. Registering, adjusting, or
deleting an inventory container changes that stock, but the inventory
routes never invalidated any cache — so the list served the pre-write
stock for the rest of the stale window. On the client the add / adjust /
delete supply mutations only invalidated the per-medication inventory
query, never the medications-list query that the card reads, so a mounted
list never refetched even once the server caught up.

Hard-evict the per-user medications + compliance buckets from every
supply-affecting write (inventory POST, the per-item PATCH / DELETE, and
the deprecated glp1 POST dose-change + ledger branches) so the next read
reflects the change rather than a stale SWR value. On the client,
centralise the supply mutations on a shared invalidateSupplyQueries helper
that drops both the medications-list key and the per-medication inventory
key. The card's neutral styling is unchanged.
2026-06-19 21:08:35 +02:00

44 lines
1.9 KiB
TypeScript

/**
* Supply-staleness regression — the medications-list card / table read
* the dose-derived stock (`stockUnitsRemaining` / `stockDosesRemaining`)
* straight from the `GET /api/medications` list payload. The add / adjust /
* delete supply mutations used to invalidate only the per-medication
* inventory query, leaving the list query (`queryKeys.medications()`)
* serving the pre-write stock — so the card kept showing the old supply
* until an unrelated refetch landed.
*
* `invalidateSupplyQueries` is the single helper all three mutations call;
* this pins that it drops BOTH the list key (the card / table source) and
* the per-medication inventory key. The repo's component-test convention
* avoids `@testing-library/react` DOM interaction, so the mutation's
* invalidation contract is asserted through the extracted helper against a
* minimal QueryClient stand-in.
*/
import { describe, expect, it, vi } from "vitest";
import { invalidateSupplyQueries } from "@/components/medications/sections/inventory-section";
import { queryKeys } from "@/lib/query-keys";
describe("invalidateSupplyQueries — supply staleness regression", () => {
it("invalidates the medications LIST key AND the per-medication inventory key", async () => {
const invalidateQueries = vi.fn().mockResolvedValue(undefined);
const queryClient = { invalidateQueries } as unknown as Parameters<
typeof invalidateSupplyQueries
>[0];
await invalidateSupplyQueries(queryClient, "med-1");
const invalidatedKeys = invalidateQueries.mock.calls.map((call) =>
JSON.stringify((call[0] as { queryKey: unknown }).queryKey),
);
// The card / table source — the regression was this key being absent.
expect(invalidatedKeys).toContain(JSON.stringify(queryKeys.medications()));
// The per-medication supply tab still refetches too.
expect(invalidatedKeys).toContain(
JSON.stringify(queryKeys.medicationInventory("med-1")),
);
});
});