Skip to content

Build your personal knowledge base with Trilium Notes

Merge branch 'main' into fix/set-boolean-await

Elian Doran2a270db9dbc6
diff --cc apps/client/src/widgets/react/NotePropertyMenu.spec.tsx
index 9a24774c98,cd23b00ded..8483f10c40
--- a/apps/client/src/widgets/react/NotePropertyMenu.spec.tsx
+++ b/apps/client/src/widgets/react/NotePropertyMenu.spec.tsx
@@@ -71,3 -72,107 +72,107 @@@ describe("A collection property's expla
expect(element.querySelector(".help-tooltip-button")).not.toBeNull();
});
});
+
+ describe("ViewPropertySection", () => {
+ let container: HTMLElement | undefined;
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ vi.restoreAllMocks();
+ });
+
+ function draw(note: FNote, properties: BookProperty[]) {
+ const element = document.createElement("div");
+ container = element;
+ document.body.appendChild(element);
+ act(() => {
+ render(<>{properties.map((property, index) => (
+ <ViewPropertySection key={index} note={note} property={property} />
+ ))}</>, element);
+ });
+ return element;
+ }
+
+ const titles = (element: HTMLElement) => [ ...element.querySelectorAll(".tn-card-option-title") ]
+ .map((title) => title.textContent);
+
+ it("draws a switch, an option group and a submenu as card rows, and nothing for a button", () => {
+ const note = buildNote({ title: "Calendar", "#calendar:weekNumbers": "true" }) as unknown as FNote;
+ const element = draw(note, [
+ { type: "checkbox", label: "Week numbers", bindToLabel: "calendar:weekNumbers" },
+ { type: "button", label: "Collapse", onClick() {} },
+ {
+ type: "submenu", label: "Time slots", children: [
+ {
+ type: "option-group", label: "Duration", bindToLabel: "calendar:slotDuration",
+ defaultValue: "00:30:00",
+ options: [ { value: "00:15:00", label: "15 min" }, { value: "00:30:00", label: "30 min" } ]
+ },
+ { type: "separator" }
+ ]
+ }
+ ]);
+
+ expect(titles(element)).toEqual([ "Week numbers", "Time slots", "Duration" ]);
+ expect(element.querySelector(".switch-button")?.classList.contains("on")).toBe(true);
+ // The submenu's options stand nested under its own row, showing the default while unset.
+ const select = element.querySelector<HTMLSelectElement>(".tn-card-section-nested select");
+ expect(select?.value).toBe("00:30:00");
+ });
+
+ it("writes the label it is bound to", () => {
+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
+ const setBoolean = vi.spyOn(attributes, "setBooleanWithInheritance").mockResolvedValue(undefined);
+ const note = buildNote({ title: "Calendar" }) as unknown as FNote;
+ const element = draw(note, [
+ { type: "checkbox", label: "Hide weekends", bindToLabel: "calendar:hideWeekends" },
+ {
+ type: "option-group", label: "Duration", bindToLabel: "calendar:slotDuration",
+ options: [ { value: "00:15:00", label: "15 min" }, { value: "00:30:00", label: "30 min" } ]
+ }
+ ]);
+
+ act(() => element.querySelector<HTMLElement>(".switch-button")?.click());
+ expect(setBoolean).toHaveBeenCalledWith(note, "calendar:hideWeekends", true);
+
+ const select = element.querySelector<HTMLSelectElement>("select");
+ if (!select) throw new Error("The option group has no select.");
+ select.value = "00:30:00";
+ act(() => { select.dispatchEvent(new Event("change", { bubbles: true })); });
+ expect(setLabel).toHaveBeenCalledWith(note.noteId, "calendar:slotDuration", "00:30:00");
+ });
+
+ it("writes a number once the box commits, and removes the label when the box is emptied", () => {
+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
- const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);
++ const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);
+ const note = buildNote({ title: "Map", "#maxNestingDepth": "2" }) as unknown as FNote;
+ const element = draw(note, [
+ { type: "number", label: "Maximum nesting depth", bindToLabel: "maxNestingDepth" }
+ ]);
+ const input = element.querySelector<HTMLInputElement>("input");
+ if (!input) throw new Error("The number property has no box.");
+ expect(input.value).toBe("2");
+
+ const type = (value: string) => act(() => {
+ input.value = value;
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ const commit = () => act(() => { input.dispatchEvent(new Event("change")); });
+
+ type("1");
+ type("12");
+ expect(setLabel).not.toHaveBeenCalled();
+ commit();
+ expect(setLabel).toHaveBeenCalledExactlyOnceWith(note.noteId, "maxNestingDepth", "12");
+
+ type("");
+ commit();
+ expect(input.value).toBe("");
+ expect(removeLabel).toHaveBeenCalledExactlyOnceWith(note, "maxNestingDepth");
+ expect(setLabel).toHaveBeenCalledTimes(1);
+ });
+ });