Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(client): adapt the specs to Preact 11's rendering
Elian Doran3e813df42c64
diff --git a/apps/client/src/test/setup.ts b/apps/client/src/test/setup.tsindex 894b11d1ea..f6d9397cda 100644--- a/apps/client/src/test/setup.ts+++ b/apps/client/src/test/setup.ts@@ -1,3 +1,8 @@+// The client always runs with `preact/compat` loaded, and its `options.vnode` hook changes how+// every element renders (`onChange` on a text input is `input`, `onFocus` is `focusin`). Loaded+// here so a spec renders the same way whether or not its own imports reach compat.+import "preact/compat";+import { Modal } from "bootstrap";import $ from "jquery";import { vi } from "vitest";diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsxindex 7f64a52f9a..4d52a61094 100644--- a/apps/client/src/widgets/collections/board/card.spec.tsx+++ b/apps/client/src/widgets/collections/board/card.spec.tsx@@ -67,9 +67,10 @@ describe("Board card", () => {});afterEach(() => {- if (container) {- render(null, container);- container.remove();+ const mounted = container;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();container = undefined;}});@@ -600,13 +601,8 @@ describe("Board card", () => {target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, ...init }));}- /**- * happy-dom defines no `ondragstart` on elements, so Preact registers the handler under the- * prop's own casing rather than the DOM event name (see drag.spec).- */function fireDrag(target: HTMLElement, type: "dragstart" | "dragend", dataTransfer: unknown) {- const cased = type === "dragstart" ? "DragStart" : "DragEnd";- const event = new Event(`on${type}` in target ? type : cased, { bubbles: true });+ const event = new Event(type, { bubbles: true });Object.defineProperty(event, "dataTransfer", { value: dataTransfer, configurable: true });target.dispatchEvent(event);}@@ -743,7 +739,7 @@ describe("OutsideFilterBadge", () => {expect(badge?.getAttribute("role")).toBe("img");expect(badge?.getAttribute("aria-label")).toBe("board_view.card-outside-filter");} finally {- render(null, container);+ act(() => render(null, container));container.remove();}});diff --git a/apps/client/src/widgets/collections/board/drag.spec.tsx b/apps/client/src/widgets/collections/board/drag.spec.tsxindex 4589b5c2d9..61d9859199 100644--- a/apps/client/src/widgets/collections/board/drag.spec.tsx+++ b/apps/client/src/widgets/collections/board/drag.spec.tsx@@ -25,13 +25,6 @@ vi.mock("../../../services/branches", () => ({}}));-/** What Preact registers each handler as where the DOM knows no property of that name. */-const PREACT_DRAG_EVENTS = {- dragover: "DragOver",- dragleave: "DragLeave",- drop: "Drop"-} as const;-// Hoisted with the mock, which is lifted above everything a test file declares.const layout = vi.hoisted(() => ({ onMobile: false }));@@ -55,9 +48,10 @@ describe("Board drag and drop", () => {afterEach(() => {(document.activeElement as HTMLElement | null)?.blur?.();- if (container) {- render(null, container);- container.remove();+ const mounted = container;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();container = undefined;}});@@ -584,10 +578,7 @@ describe("Board drag and drop", () => {clipboard: { types: string[], data?: Record<string, string> },clientY = 0) {- // happy-dom defines no `ondragover`/`ondrop` on elements, and Preact falls back to the- // prop's own casing where the DOM knows no lowercase one. A `dragover` reaches nothing.- const known = `on${type}` in document.createElement("div");- const event = new Event(known ? type : PREACT_DRAG_EVENTS[type], {+ const event = new Event(type, {bubbles: true,cancelable: true});@@ -632,9 +623,10 @@ describe("Board column reordering", () => {afterEach(() => {(document.activeElement as HTMLElement | null)?.blur?.();- if (container) {- render(null, container);- container.remove();+ const mounted = container;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();container = undefined;}});@@ -957,10 +949,7 @@ describe("Board column reordering", () => {type: "dragstart" | "dragover" | "dragend" | "drop",clientX = 0) {- const cased = {- dragstart: "DragStart", dragover: "DragOver", dragend: "DragEnd", drop: "Drop"- }[type];- const event = new Event(`on${type}` in target ? type : cased, {+ const event = new Event(type, {bubbles: true,cancelable: true});diff --git a/apps/client/src/widgets/collections/board/group_by.spec.tsx b/apps/client/src/widgets/collections/board/group_by.spec.tsxindex 879f4c1479..fb62252c1d 100644--- a/apps/client/src/widgets/collections/board/group_by.spec.tsx+++ b/apps/client/src/widgets/collections/board/group_by.spec.tsx@@ -140,9 +140,10 @@ describe("BoardGroupBy", () => {mocks.setLabel.mockClear();mocks.showError.mockClear();mocks.detail.opts = null;- if (container) {- render(null, container);- container.remove();+ const mounted = container;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();container = undefined;}});@@ -168,12 +169,15 @@ describe("BoardGroupBy", () => {);});- // Bootstrap does not open a menu under happy-dom, and the items are drawn only once it is.+ await openMenu(mountPoint);+ return { mountPoint, onSelect };+ }++ // Bootstrap does not open a menu under happy-dom, and the items are drawn only once it is.+ async function openMenu(mountPoint: HTMLElement) {await act(async () => {$(mountPoint.querySelector(".dropdown") as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");});-- return { mountPoint, onSelect };}const items = (mountPoint: HTMLElement) =>@@ -209,7 +213,11 @@ describe("BoardGroupBy", () => {act(() => { (priority as HTMLElement).click(); });expect(onSelect).not.toHaveBeenCalled();- act(() => { (status as HTMLElement).click(); });+ // Picking closes the menu, so the next pick happens in a menu opened again.+ await openMenu(mountPoint);+ const [ statusAgain ] = items(mountPoint);+ expect(statusAgain?.textContent?.trim()).toBe("Status");+ act(() => { (statusAgain as HTMLElement).click(); });expect(onSelect).toHaveBeenCalledWith("status");});diff --git a/apps/client/src/widgets/react/ColorPicker.spec.tsx b/apps/client/src/widgets/react/ColorPicker.spec.tsxindex ec9db4ba02..f883598077 100644--- a/apps/client/src/widgets/react/ColorPicker.spec.tsx+++ b/apps/client/src/widgets/react/ColorPicker.spec.tsx@@ -193,9 +193,9 @@ describe("ColorPicker", () => {// Two rapid changes within the debounce interval collapse into a single report.await act(async () => {input.value = "#aabbcc";- input.dispatchEvent(new Event("change", { bubbles: true }));+ input.dispatchEvent(new Event("input", { bubbles: true }));input.value = "#ddeeff";- input.dispatchEvent(new Event("change", { bubbles: true }));+ input.dispatchEvent(new Event("input", { bubbles: true }));});expect(onChange).not.toHaveBeenCalled();@@ -217,7 +217,7 @@ describe("ColorPicker", () => {await act(async () => {input.value = "#aabbcc";- input.dispatchEvent(new Event("change", { bubbles: true }));+ input.dispatchEvent(new Event("input", { bubbles: true }));});await act(async () => { vi.advanceTimersByTime(250); });onChange.mockClear();diff --git a/apps/client/src/widgets/react/Modal.spec.tsx b/apps/client/src/widgets/react/Modal.spec.tsxindex c92c2eb696..b5a82d8f6f 100644--- a/apps/client/src/widgets/react/Modal.spec.tsx+++ b/apps/client/src/widgets/react/Modal.spec.tsx@@ -60,7 +60,7 @@ beforeEach(() => {});afterEach(() => {- render(null, container);+ act(() => render(null, container));container.remove();});diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsxindex e5addf59d2..ebabae5ecf 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx@@ -102,7 +102,7 @@ describe("useCanvasPersistence content loading (#10279)", () => {});afterEach(() => {- render(null, container);+ act(() => render(null, container));container.remove();vi.useRealTimers();});@@ -236,7 +236,7 @@ describe("useCanvasDrawingPersistence", () => {});afterEach(() => {- render(null, container);+ act(() => render(null, container));container.remove();});@@ -283,7 +283,7 @@ describe("useCanvasDrawingPersistence", () => {files: { f1: IMAGE }});- render(null, container);+ act(() => render(null, container));const colored = buildAttachment({ elements, appState: { viewBackgroundColor: "#ffc9c9" } });expect((await mount(colored.attachment, buildEditor())).appState).toEqual({ viewBackgroundColor: "#ffc9c9", theme: "light" });@@ -334,7 +334,7 @@ describe("useCanvasDrawingPersistence", () => {change();expect(editor.scheduleSave).toHaveBeenCalledTimes(2);- render(null, container);+ act(() => render(null, container));expect(editor.release).toHaveBeenCalledWith("a1");});diff --git a/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx b/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsxindex 6c6b0a4cbf..6604a0f7d1 100644--- a/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx+++ b/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx@@ -1,4 +1,5 @@import { render } from "preact";+import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";// i18next is never initialized under test, so t() is rendered deterministic instead: the key with@@ -54,7 +55,7 @@ beforeEach(() => {});afterEach(() => {- render(null, container);+ act(() => render(null, container));container.remove();vi.unstubAllGlobals();vi.clearAllMocks();@@ -128,7 +129,7 @@ describe("FontPreview", () => {await waitForSpecimen();expect(registeredFonts.size).toBe(1);- render(null, container);+ act(() => render(null, container));expect(registeredFonts.size).toBe(0);});@@ -168,7 +169,7 @@ describe("FontPreview", () => {// The label alone says the font is offered — the picker names it by the note's title, so// there is no family to keep in step with a rename.isOffered = true;- render(null, container);+ act(() => render(null, container));mount();await waitForSpecimen();diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsxindex 19d81fbd0a..cd8a4c9ef3 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx@@ -111,9 +111,10 @@ describe("ReadOnlyText reacting to content changes (#10575)", () => {let cleanupContainer: HTMLElement | undefined;afterEach(() => {- if (cleanupContainer) {- render(null, cleanupContainer);- cleanupContainer.remove();+ const mounted = cleanupContainer;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();cleanupContainer = undefined;}});@@ -190,9 +191,10 @@ describe("ReadOnlyText ?bookmark= handling", () => {let cleanupContainer: HTMLElement | undefined;afterEach(() => {- if (cleanupContainer) {- render(null, cleanupContainer);- cleanupContainer.remove();+ const mounted = cleanupContainer;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();cleanupContainer = undefined;}});@@ -256,9 +258,10 @@ describe("ReadOnlyText text direction", () => {let cleanupContainer: HTMLElement | undefined;afterEach(() => {- if (cleanupContainer) {- render(null, cleanupContainer);- cleanupContainer.remove();+ const mounted = cleanupContainer;+ if (mounted) {+ act(() => render(null, mounted));+ mounted.remove();cleanupContainer = undefined;}});