Skip to content

Build your personal knowledge base with Trilium Notes

test(text): cover the mention UI's guards and the deferred icon picker

Elian Doran481d03bca42d
diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts
index d6d3a5f498..0c1a81ea11 100644
--- a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts
+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts
@@ -770,18 +770,21 @@ describe("buildTriliumSlashCommands", () => {
// The balloon needs the view and mapper settled, which they are not until the slash command has
// finished its own DOM and selection cleanup.
- it("defers the anchor form to the next tick", async () => {
+ it.each([
+ [ "anchor", BookmarkUI, "_showFormView" ],
+ [ "icon", InlineIconUI, "showPicker" ]
+ ] as const)("defers the %s entry's balloon to the next tick", (id, plugin, method) => {
vi.useFakeTimers();
try {
const { fake, pluginInstances } = makeFakeEditor();
- const showFormView = vi.fn();
- pluginInstances.set(BookmarkUI, { _showFormView: showFormView });
+ const show = vi.fn();
+ pluginInstances.set(plugin, { [method]: show });
- definition("anchor").execute?.(fake);
- expect(showFormView).not.toHaveBeenCalled();
+ definition(id).execute?.(fake);
+ expect(show).not.toHaveBeenCalled();
vi.runAllTimers();
- expect(showFormView).toHaveBeenCalledOnce();
+ expect(show).toHaveBeenCalledOnce();
} finally {
vi.useRealTimers();
}
diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts
index ded793ef2b..47dd466b81 100644
--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts
@@ -283,6 +283,22 @@ describe("TriliumMentionUI", () => {
expect(text()).toBe("<paragraph>x @ga</paragraph>");
});
+ it("leaves the text as it is for a pick arriving after the list closed, or a commit that fails", async () => {
+ setModelData(editor.model, "<paragraph>x []</paragraph>");
+ type("#al");
+ const commit = labels.state?.commit;
+ moveCaretTo(0);
+ commit?.({ id: "#alpha", text: "#alpha" });
+ expect(text()).toBe("<paragraph>x #al</paragraph>");
+
+ setModelData(editor.model, "<paragraph>x []</paragraph>");
+ type("#be");
+ labels.state?.commit(Promise.reject(new Error("canceled")));
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(text()).toBe("<paragraph>x #be</paragraph>");
+ });
+
it("hands the item to the feed's commit with the trigger text gone, unless it went stale", async () => {
const commit = vi.fn();
const canCommit = vi.fn(() => true);
@@ -323,6 +339,19 @@ describe("TriliumMentionUI", () => {
});
});
+ it("places the list at the caret once the marked text was undone away", () => {
+ // The undos are invisible to the text watcher, so the list stays open on a marker whose range
+ // has meanwhile been moved to the graveyard.
+ editor.execute("enter");
+ type("#al");
+ const caretRect = labels.state?.caretRect;
+ editor.execute("undo");
+ editor.execute("undo");
+
+ expect(editor.model.markers.get("mention")?.getRange().root.rootName).toBe("$graveyard");
+ expect(caretRect?.().height).toBeGreaterThan(0);
+ });
+
it("tolerates being initialised before MentionEditing registers the mention command", async () => {
await createEditor({}, [ Essentials, Paragraph, TriliumMentionUI, MentionEditing ]);
diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts
index 774f5eec92..aa22fc42f5 100644
--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts
+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts
@@ -83,6 +83,7 @@ export default class TriliumMentionUI extends Plugin {
clickOutsideHandler({
emitter: this._domEmitter,
activator: () => !!this._open?.hostedList.element,
+ /* v8 ignore next -- `clickOutsideHandler` only calls `contextElements()` once the activator reported the list showing, so its element is never null */
contextElements: () => {
const element = this._open?.hostedList.element;
return element ? [ element ] : [];
@@ -240,6 +241,7 @@ export default class TriliumMentionUI extends Plugin {
const { left, top, width, height } = this._caretRect(marker);
return new DOMRect(left, top, width, height);
},
+ /* v8 ignore next -- a live editor's editing view always has its DOM root, so the `?? null` arm never runs */
editable: this.editor.editing.view.getDomRoot() ?? null,
commit: (item) => this._commit(feed, item)
});