Files
HealthLog/src/components/settings/access/__tests__/managed-profile-edit-patch.test.ts
T
Marc-André Bombeck c673255355 fix(settings): stop a timezone edit writing the actor's language into a record
A managed record with no recorded language opens the edit form's language
control on the actor's, which is the same starting point the creation form
uses. The diff then compared that control against the record's NULL column, so
every save of such a record carried `locale` — a guardian correcting only the
timezone silently recorded their own language as the record's, and the audit
row said the language changed.

The comparison is against the value the control opened on, frozen at mount.
Nothing is sent unless somebody moves it.
2026-09-10 01:59:08 +02:00

69 lines
2.0 KiB
TypeScript

/**
* What the managed-record edit form sends, and what it must not.
*
* The route reads an absent key as "leave it", so the diff this function
* computes is the whole of the promise that a save touches only what somebody
* moved. The case worth pinning is the language: a record with none opens its
* control on the ACTOR's, so comparing the control against the record's column
* made every save of such a record carry `locale` — and a guardian who only
* corrected the timezone wrote their own language into somebody else's record.
*/
import { describe, expect, it } from "vitest";
import { managedProfileEditPatch } from "@/components/settings/access/managed-profile-edit-form";
import type { ManagedProfileView } from "@/lib/queries/use-managed-profiles";
const record: ManagedProfileView = {
id: "rec_1",
displayName: "Managed record",
dateOfBirth: null,
gender: null,
locale: null,
timezone: "Europe/Berlin",
recordKind: "managed",
};
const draft = {
displayName: "Managed record",
dateOfBirth: "",
locale: "en" as const,
gender: null,
timezone: "Europe/Berlin",
};
describe("managedProfileEditPatch", () => {
it("leaves the language out when a record with none only moves its timezone", () => {
const patch = managedProfileEditPatch(
record,
{ ...draft, timezone: "Europe/Madrid" },
"en",
);
expect(patch).toEqual({ timezone: "Europe/Madrid" });
});
it("answers null when a record with no language is not touched at all", () => {
expect(managedProfileEditPatch(record, draft, "en")).toBeNull();
});
it("sends the language once somebody picks a different one", () => {
const patch = managedProfileEditPatch(
record,
{ ...draft, locale: "de" },
"en",
);
expect(patch).toEqual({ locale: "de" });
});
it("sends the language when a record that has one is moved off it", () => {
const patch = managedProfileEditPatch(
{ ...record, locale: "de" },
{ ...draft, locale: "en" },
"de",
);
expect(patch).toEqual({ locale: "en" });
});
});