mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
fix(medications): refresh the episode and document details after filing a link
This commit is contained in:
1 parent
0cb877e0a2
commit
86113848b5
2 files changed
+63
-1
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
const captured = vi.hoisted(() => ({
|
||||
onSaved: undefined as (() => void) | undefined,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/links/related-records", () => ({
|
||||
RelatedRecordsList: () => null,
|
||||
RelationLinkPickers: (props: { onSaved: () => void }) => {
|
||||
captured.onSaved = props.onSaved;
|
||||
return null;
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/hooks/use-record-capabilities", () => ({
|
||||
useRecordCapabilities: () => ({ inSharedRecord: false }),
|
||||
}));
|
||||
|
||||
import { MedicationRelatedSection } from "@/components/medications/detail/medication-related-section";
|
||||
|
||||
/**
|
||||
* A link filed from the medication is listed on the other end too, so a save
|
||||
* must stale every detail that reads it back: the medication itself, the
|
||||
* episode, the document and the visit.
|
||||
*/
|
||||
describe("MedicationRelatedSection", () => {
|
||||
it("invalidates every end a saved link is read back on", () => {
|
||||
const qc = new QueryClient();
|
||||
const spy = vi.spyOn(qc, "invalidateQueries");
|
||||
renderToStaticMarkup(
|
||||
<QueryClientProvider client={qc}>
|
||||
<I18nProvider initialLocale="en">
|
||||
<MedicationRelatedSection
|
||||
medicationId="med-1"
|
||||
startsOn={null}
|
||||
related={[]}
|
||||
/>
|
||||
</I18nProvider>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
expect(captured.onSaved).toBeTypeOf("function");
|
||||
captured.onSaved?.();
|
||||
const keys = spy.mock.calls.map((call) => call[0]?.queryKey);
|
||||
expect(keys).toEqual(
|
||||
expect.arrayContaining([
|
||||
queryKeys.medicationDetail("med-1"),
|
||||
queryKeys.illness(),
|
||||
queryKeys.documents(),
|
||||
queryKeys.encounters(),
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -46,9 +46,13 @@ export function MedicationRelatedSection({
|
||||
related={related}
|
||||
anchor={startsOn}
|
||||
onSaved={() => {
|
||||
// The other end lists the medication too.
|
||||
// The other end lists the medication too: every kind the
|
||||
// pickers file (episode, document, visit) reads it back on its
|
||||
// own detail, so each one's prefix goes stale.
|
||||
for (const queryKey of [
|
||||
queryKeys.medicationDetail(medicationId),
|
||||
queryKeys.illness(),
|
||||
queryKeys.documents(),
|
||||
queryKeys.encounters(),
|
||||
]) {
|
||||
void qc.invalidateQueries({ queryKey });
|
||||
|
||||
Reference in new issue
Block a user