fix(web): VisibilityBadge typed to the union (#38); normalize localized_text to default language on save (#41)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 15:46:48 +02:00
parent 0a29127f7e
commit b4d71b0f80
4 changed files with 83 additions and 27 deletions
+38
View File
@@ -3,6 +3,7 @@ import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { ObjectForm } from "./object-form";
import { pruneFields } from "./prune-fields";
test("create mode: shows visibility (draft/internal only) and submits assembled values", async () => {
const onSubmit = vi.fn();
@@ -47,3 +48,40 @@ test("edit mode: no visibility control, save button, prefilled values", async ()
expect(screen.queryByLabelText(/visibility/i)).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();
});
test("pruneFields: localized_text keeps only the default-language key, other object fields unaffected", () => {
const localizedTextKeys = new Set(["title_ml"]);
const result = pruneFields(
{ title_ml: { en: "Old", sv: "Ny" }, other: "x" },
localizedTextKeys,
"sv",
);
expect(result).toEqual({ title_ml: { sv: "Ny" }, other: "x" });
expect(Object.keys(result.title_ml as Record<string, unknown>)).not.toContain("en");
});
test("pruneFields: localized_text with only non-default lang produces empty object (key omitted)", () => {
const localizedTextKeys = new Set(["title_ml"]);
const result = pruneFields(
{ title_ml: { en: "English only" } },
localizedTextKeys,
"sv",
);
expect(result).toEqual({});
});
test("pruneFields: non-localized_text object fields are preserved as-is", () => {
const localizedTextKeys = new Set(["title_ml"]);
const result = pruneFields(
{ nested_obj: { a: "1", b: "2" } },
localizedTextKeys,
"sv",
);
expect(result).toEqual({ nested_obj: { a: "1", b: "2" } });
});