Skip to content

Build your personal knowledge base with Trilium Notes

fix: surface saved search errors

Wu Shuwen57235be7c10a
diff --git a/apps/client/src/widgets/search_result.spec.tsx b/apps/client/src/widgets/search_result.spec.tsx
new file mode 100644
index 0000000000..b58823be98
--- /dev/null
+++ b/apps/client/src/widgets/search_result.spec.tsx
@@ -0,0 +1,110 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const shownContext = vi.hoisted(() => ({
+ current: null as import("../entities/fnote").default | null,
+ parentComponent: null as import("../components/component").default | null,
+}));
+
+vi.mock("./react/hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("./react/hooks")>()),
+ useNoteContext: () => ({
+ note: shownContext.current,
+ notePath: shownContext.current
+ ? `root/${shownContext.current.noteId}`
+ : "root",
+ ntxId: "ntx1",
+ parentComponent: shownContext.parentComponent,
+ }),
+}));
+
+vi.mock("./collections/NoteList", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("./collections/NoteList")>()),
+ SearchNoteList: () => null,
+}));
+
+import type Component from "../components/component";
+import type FNote from "../entities/fnote";
+import froca from "../services/froca";
+import toast from "../services/toast";
+import { buildNote } from "../test/easy-froca";
+import SearchResult from "./search_result";
+import { ParentComponent } from "./react/react_utils";
+
+describe("SearchResult", () => {
+ const handlers = new Map<string, (data: unknown) => void>();
+ const triggerEvent = vi.fn((name: string, data: unknown) => {
+ handlers.get(name)?.(data);
+ });
+ const parent = {
+ componentId: "cid",
+ registerHandler: (name: string, callback: (data: unknown) => void) =>
+ handlers.set(name, callback),
+ removeHandler: () => {},
+ triggerEvent,
+ } as unknown as Component;
+ const loadSearchNote = vi.fn(async (noteId: string) => {
+ const note =
+ (froca.notes[noteId] as FNote | undefined) ?? shownContext.current;
+ if (note) {
+ note.searchResultsLoaded = true;
+ }
+ return undefined as Awaited<ReturnType<typeof froca.loadSearchNote>>;
+ });
+ const showError = vi.spyOn(toast, "showError");
+ let container: HTMLDivElement | null = null;
+
+ beforeEach(() => {
+ handlers.clear();
+ triggerEvent.mockClear();
+ loadSearchNote.mockClear();
+ showError.mockClear();
+ shownContext.current = null;
+ shownContext.parentComponent = parent;
+ froca.loadSearchNote =
+ loadSearchNote as unknown as typeof froca.loadSearchNote;
+ });
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = null;
+ }
+ });
+
+ it("surfaces errors returned by a saved search", async () => {
+ const savedSearch = buildNote({
+ title: "My search",
+ type: "search",
+ "#searchString": "#tcfindme",
+ });
+ shownContext.current = savedSearch;
+ container = document.createElement("div");
+ document.body.appendChild(container);
+
+ act(() =>
+ render(
+ <ParentComponent.Provider value={parent}>
+ <SearchResult />
+ </ParentComponent.Provider>,
+ container,
+ ),
+ );
+
+ const button = container.querySelector("button");
+ expect(button).toBeTruthy();
+ loadSearchNote.mockResolvedValue({ error: "Invalid saved search" });
+
+ await act(async () => {
+ button?.click();
+ });
+
+ expect(loadSearchNote).toHaveBeenCalledWith(savedSearch.noteId);
+ expect(showError).toHaveBeenCalledWith("Invalid saved search");
+ expect(triggerEvent).toHaveBeenCalledWith("searchRefreshed", {
+ ntxId: "ntx1",
+ });
+ });
+});
diff --git a/apps/client/src/widgets/search_result.tsx b/apps/client/src/widgets/search_result.tsx
index 7dee73f670..901bc90bb0 100644
--- a/apps/client/src/widgets/search_result.tsx
+++ b/apps/client/src/widgets/search_result.tsx
@@ -44,7 +44,10 @@ export default function SearchResult() {
return;
}
try {
- await froca.loadSearchNote(note.noteId);
+ const result = await froca.loadSearchNote(note.noteId);
+ if (result?.error) {
+ toast.showError(result.error);
+ }
} catch (e: unknown) {
toast.showError(getErrorMessage(e));
}