Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/main' into feat/search-overhaul

Elian Doranae12f610f950
diff --cc apps/client/src/widgets/react/hooks.spec.tsx
index e6442b5d78,0e715258fb..f1b8914d42
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@@ -471,53 -436,70 +472,88 @@@ describe("useImperativeSearchHighlighli
});
it("does not touch an element that was never highlighted", async () => {
- await mount(null);
- const target = content("<p>a needle here</p>");
+ await remount(null);
+ const el = content("<p>a needle here</p>");
markSpies.unmark.mockClear();
- highlight?.(target);
+ liveHighlight?.(el);
expect(markSpies.unmark).not.toHaveBeenCalled();
- expect(target.innerHTML).toBe("<p>a needle here</p>");
- target.remove();
+ expect(el.innerHTML).toBe("<p>a needle here</p>");
+ el.remove();
});
- it("leaves a collapsed block closed when it holds no match", async () => {
- await mount([ "needle" ]);
- const target = content("<details><summary>t</summary><p>nothing relevant</p></details>");
+ it("wraps every match of a regex-typed token (#5332)", () => {
+ const el = highlight([{ token: "ba.", type: "regex" }], "<p>foo bar baz qux</p>");
+ expect(markedTexts(el)).toEqual(["bar", "baz"]);
+ });
- highlight?.(target);
+ it("skips an invalid regex token without throwing", () => {
+ expect(() => highlight([{ token: "(unterminated", type: "regex" }], "<p>foo bar</p>")).not.toThrow();
+ expect(markedTexts(target)).toEqual([]);
+ });
- expect(target.querySelector("details")?.open).toBe(false);
- target.remove();
+ it("still highlights when given legacy string[] input", () => {
+ const el = highlight(["bar"], "<p>foo bar baz</p>");
+ expect(markedTexts(el)).toEqual(["bar"]);
});
- it("does nothing without tokens", async () => {
- await mount([]);
- const target = content("<details><summary>t</summary><p>needle</p></details>");
+ it("is a no-op for null or undefined tokens", () => {
+ expect(() => highlight(null, "<p>foo bar</p>")).not.toThrow();
+ expect(markedTexts(target)).toEqual([]);
- highlight?.(target);
+ expect(() => highlight(undefined, "<p>foo bar</p>")).not.toThrow();
+ expect(markedTexts(target)).toEqual([]);
- expect(target.querySelectorAll(".ck-find-result").length).toBe(0);
+ highlight([], "<details><summary>t</summary><p>needle</p></details>");
+ expect(markedTexts(target)).toEqual([]);
expect(target.querySelector("details")?.open).toBe(false);
- target.remove();
+ });
+
+ it("highlights matches and opens the collapsed <details> that contains them", () => {
+ const el = highlight(["needle"], "<details><summary>t</summary><p>a needle here</p></details>");
+ expect(markedTexts(el)).toEqual(["needle"]);
+ expect(el.querySelector("details")?.open).toBe(true);
+ });
+
+ it("leaves a collapsed block closed when it holds no match", () => {
+ const el = highlight(["needle"], "<details><summary>t</summary><p>nothing relevant</p></details>");
+ expect(markedTexts(el)).toEqual([]);
+ expect(el.querySelector("details")?.open).toBe(false);
});
});
+
+ describe("useNoteLabelBoolean", () => {
+ let container: HTMLElement | undefined;
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ /**
+ * The render that mounts a consumer has already read the label, so forcing another one after it
+ * draws everything twice for a value that has not changed. A board draws this once a card.
+ */
+ it("draws a consumer once for a label that has not changed", async () => {
+ const note = buildNote({ title: "Card", "#archived": "true" });
+ const draws: boolean[] = [];
+
+ function Consumer() {
+ const [ archived ] = useNoteLabelBoolean(note, "archived");
+ draws.push(archived);
+ return <span>{String(archived)}</span>;
+ }
+
+ container = document.body.appendChild(document.createElement("div"));
+ await act(async () => {
+ render(<Consumer />, container as HTMLElement);
+ await new Promise((resolve) => setTimeout(resolve));
+ });
+
+ expect(draws).toEqual([ true ]);
+ });
+ });