Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/main' into bug-fix_8676

Elian Doranb43d2f60eb67
diff --cc apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.ts
index b785e2486d,7a2bd50dbb..eb48527f21
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.ts
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.ts
@@@ -1,121 -1,73 +1,119 @@@
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import $ from 'jquery'
- import utils from '../../../services/utils'
- import dialog from '../../../services/dialog'
++import { describe, it, expect, vi, beforeEach } from "vitest";
+ import $ from "jquery";
-import { describe, expect, it, vi } from "vitest";
++import dialog from "../../../services/dialog";
- vi.mock('../../../services/utils', async (importOriginal) => {
- const actual = await importOriginal() as any
-import { setupAnswerEventHandlers } from "./RelationMap.js";
-
-vi.mock("../../../services/utils.js", async (importOriginal) => {
- const actual = await importOriginal<typeof import("../../../services/utils.js")>();
++vi.mock("../../../services/utils", async (importOriginal) => {
++ const actual = await importOriginal() as any;
return {
...actual,
default: {
...actual.default,
- filterAttributeName: vi.fn((val: string) => val.replace(/[^a-z0-9]/gi, ''))
- filterAttributeName: vi.fn((val: string) => val)
++ filterAttributeName: vi.fn((val: string) => val.replace(/[^a-z0-9]/gi, ""))
}
- }
- })
+ };
+ });
- vi.mock('../../../services/dialog', () => ({
-describe("RelationMap - $answer event synchronization", () => {
++vi.mock("../../../services/dialog", () => ({
+ default: {
+ prompt: vi.fn()
+ }
- }))
-
- vi.mock('../../../services/attribute_autocomplete', () => ({
++}));
++
++vi.mock("../../../services/attribute_autocomplete", () => ({
+ default: {
+ initAttributeNameAutocomplete: vi.fn()
+ }
- }))
-
- vi.mock('../../../services/i18n', () => ({
++}));
++
++vi.mock("../../../services/i18n", () => ({
+ t: (key: string) => key
- }))
++}));
+
+// Call promptForRelationName and extract the $answer input element from the dialog's shown callback
+async function getAnswerFromPrompt(): Promise<JQuery<HTMLInputElement>> {
- const { promptForRelationName } = await import('./utils')
-
- let $answer!: JQuery<HTMLInputElement>
-
++ const { promptForRelationName } = await import("./utils");
++
++ let $answer!: JQuery<HTMLInputElement>;
++
+ vi.mocked(dialog.prompt).mockImplementation(({ shown }) => {
- document.body.innerHTML = '<input type="text" />'
- const input = document.querySelector('input') as HTMLInputElement
- $answer = $(input) as JQuery<HTMLInputElement>
- shown?.({ $answer })
- return Promise.resolve(null)
- })
-
- promptForRelationName()
- return $answer
++ document.body.innerHTML = '<input type="text" />';
++ const input = document.querySelector("input") as HTMLInputElement;
++ $answer = $(input) as JQuery<HTMLInputElement>;
++ shown?.({ $answer });
++ return Promise.resolve(null);
++ });
++
++ promptForRelationName();
++ return $answer;
+}
- describe('IME composition handling - Chinese input (promptForRelationName)', () => {
- let input: HTMLInputElement
- let $answer: JQuery<HTMLInputElement>
- it("dispatches input event with bubbles:true when Enter is pressed", () => {
- const $answer = $("<input type='text' />");
- const dispatchSpy = vi.spyOn($answer[0], "dispatchEvent");
++describe("IME composition handling - Chinese input (promptForRelationName)", () => {
++ let input: HTMLInputElement;
++ let $answer: JQuery<HTMLInputElement>;
- setupAnswerEventHandlers($answer);
- $answer.trigger($.Event("keydown", { key: "Enter" }));
+ beforeEach(async () => {
- vi.clearAllMocks()
- $answer = await getAnswerFromPrompt()
- input = $answer[0] as HTMLInputElement
- })
++ vi.clearAllMocks();
++ $answer = await getAnswerFromPrompt();
++ input = $answer[0] as HTMLInputElement;
++ });
- it('does not filter intermediate Chinese characters during composition', () => {
- expect(dispatchSpy).toHaveBeenCalledWith(
- expect.objectContaining({ type: "input", bubbles: true })
- );
++ it("does not filter intermediate Chinese characters during composition", () => {
+ // user starts typing in Chinese IME
- input.dispatchEvent(new Event('compositionstart'))
++ input.dispatchEvent(new Event("compositionstart"));
+
+ // intermediate IME states — these are pinyin keystrokes shown before final char
- input.value = 'n'
- input.dispatchEvent(new Event('input'))
- input.value = 'ni'
- input.dispatchEvent(new Event('input'))
- input.value = 'nin'
- input.dispatchEvent(new Event('input'))
- input.value = 'ning'
- input.dispatchEvent(new Event('input'))
++ input.value = "n";
++ input.dispatchEvent(new Event("input"));
++ input.value = "ni";
++ input.dispatchEvent(new Event("input"));
++ input.value = "nin";
++ input.dispatchEvent(new Event("input"));
++ input.value = "ning";
++ input.dispatchEvent(new Event("input"));
+
- expect(input.value).toBe('ning')
- })
++ expect(input.value).toBe("ning");
+ });
- it('filters invalid characters from input after IME composition ends', () => {
- input.dispatchEvent(new Event('compositionstart'))
- it("dispatches input event with bubbles:true when input loses focus (blur)", () => {
- const $answer = $("<input type='text' />");
- const dispatchSpy = vi.spyOn($answer[0], "dispatchEvent");
++ it("filters invalid characters from input after IME composition ends", () => {
++ input.dispatchEvent(new Event("compositionstart"));
- setupAnswerEventHandlers($answer);
- $answer.trigger("blur");
+ // intermediate pinyin
- input.value = 'n'
- input.dispatchEvent(new Event('input'))
- input.value = 'ni'
- input.dispatchEvent(new Event('input'))
++ input.value = "n";
++ input.dispatchEvent(new Event("input"));
++ input.value = "ni";
++ input.dispatchEvent(new Event("input"));
- expect(dispatchSpy).toHaveBeenCalledWith(
- expect.objectContaining({ type: "input", bubbles: true })
- );
+ // user selects the Chinese character 你 from the IME picker
- input.value = '你'
- input.dispatchEvent(new Event('compositionend'))
++ input.value = "你";
++ input.dispatchEvent(new Event("compositionend"));
+
- expect(input.value).toBe('')
- })
++ expect(input.value).toBe("");
+ });
- it('allows normal latin input after Chinese composition ends', () => {
- it("does not dispatch input event for non-Enter keys", () => {
- const $answer = $("<input type='text' />");
- const dispatchSpy = vi.spyOn($answer[0], "dispatchEvent");
++ it("allows normal latin input after Chinese composition ends", () => {
+ // first do a Chinese composition
- input.dispatchEvent(new Event('compositionstart'))
- input.value = '你'
- input.dispatchEvent(new Event('compositionend'))
++ input.dispatchEvent(new Event("compositionstart"));
++ input.value = "你";
++ input.dispatchEvent(new Event("compositionend"));
- setupAnswerEventHandlers($answer);
- $answer.trigger($.Event("keydown", { key: "a" }));
- $answer.trigger($.Event("keydown", { key: "Tab" }));
- $answer.trigger($.Event("keydown", { key: "Escape" }));
+ // then type normally in latin
- input.value = 'hello'
- input.dispatchEvent(new Event('input'))
++ input.value = "hello";
++ input.dispatchEvent(new Event("input"));
- expect(input.value).toBe('hello')
- })
- const inputEvents = dispatchSpy.mock.calls
- .map(([e]) => e as Event)
- .filter((e) => e.type === "input" && e.bubbles === true);
- expect(inputEvents).toHaveLength(0);
++ expect(input.value).toBe("hello");
+ });
- it('handles multiple Chinese characters in sequence', () => {
- it("input event bubbles up to parent element", () => {
- const $answer = $("<input type='text' />");
- const parent = document.createElement("div");
- parent.appendChild($answer[0]);
-
- const parentListener = vi.fn();
- parent.addEventListener("input", parentListener);
++ it("handles multiple Chinese characters in sequence", () => {
+ // first character
- input.dispatchEvent(new Event('compositionstart'))
- input.value = '你'
- input.dispatchEvent(new Event('compositionend'))
++ input.dispatchEvent(new Event("compositionstart"));
++ input.value = "你";
++ input.dispatchEvent(new Event("compositionend"));
- setupAnswerEventHandlers($answer);
- $answer.trigger($.Event("keydown", { key: "Enter" }));
+ // second character
- input.dispatchEvent(new Event('compositionstart'))
- input.value = '好'
- input.dispatchEvent(new Event('compositionend'))
++ input.dispatchEvent(new Event("compositionstart"));
++ input.value = "好";
++ input.dispatchEvent(new Event("compositionend"));
- expect(input.value).toBe('')
- })
- expect(parentListener).toHaveBeenCalledOnce();
- expect(parentListener.mock.calls[0][0]).toMatchObject({ type: "input", bubbles: true });
++ expect(input.value).toBe("");
+ });
-
});
-