Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/custom task states: improve test coverage

Adorian Doran1bf943fce842
diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts
index 271faa90f9..f85fea90a2 100644
--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts
+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts
@@ -676,6 +676,104 @@ describe("TodoListMultistateEditing", () => {
});
});
+ describe("hover-driven tooltip visibility", () => {
+ // Two todos so we can hover one while the caret sits in a plain
+ // paragraph — the caret and hover flows must be independently
+ // exercised, since the `ownedByCaret` check yields different results
+ // in each case.
+ const HOVER_FIXTURE = '<paragraph>plain[]</paragraph>' +
+ '<paragraph listIndent="0" listItemId="t-a" listType="todo">A</paragraph>' +
+ '<paragraph listIndent="0" listItemId="t-b" listType="todo">B</paragraph>';
+
+ function moveCaretTo(blockIndex: number): void {
+ editor.model.change((writer) => {
+ const block = getBlock(editor, blockIndex);
+ writer.setSelection(writer.createPositionAt(block, 0));
+ });
+ }
+
+ function checkboxOfItem(itemId: string): HTMLInputElement {
+ const domRoot = editor.editing.view.getDomRoot();
+ const input = domRoot?.querySelector<HTMLInputElement>(
+ `li[data-list-item-id="${itemId}"] .todo-list__label input[type="checkbox"]`
+ );
+ if (!input) {
+ throw new Error(`No checkbox for item ${itemId}.`);
+ }
+ return input;
+ }
+
+ beforeEach(async () => {
+ editor = await createEditor({ taskStates: CUSTOM_STATES });
+ setModelData(editor.model, HOVER_FIXTURE);
+ vi.useFakeTimers({ shouldAdvanceTime: false });
+ });
+
+ function endFakeTimers(): void {
+ vi.runOnlyPendingTimers();
+ vi.useRealTimers();
+ }
+
+ it("mouseenter on a checkbox not owned by the caret schedules a delayed show; mouseleave cancels it", () => {
+ // Caret in the plain paragraph — no todo owns the caret, so the
+ // hover flow is fully in charge of the hovered checkbox's tooltip.
+ moveCaretTo(0);
+ const inputA = checkboxOfItem("t-a");
+ expect(livePopup()).toBeNull();
+
+ inputA.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
+ expect(livePopup()).toBeNull(); // dwell not yet elapsed
+
+ vi.advanceTimersByTime(TOOLTIP_DWELL_MS);
+ expect(livePopup()).not.toBeNull();
+ const source = document.querySelector<HTMLElement>(
+ `[aria-describedby="${livePopup()?.id}"]`
+ );
+ expect(source?.closest("li")?.getAttribute("data-list-item-id")).toBe("t-a");
+
+ // Leaving the checkbox pops the hover handle → stack empties → popup gone.
+ inputA.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true }));
+ expect(livePopup()).toBeNull();
+
+ endFakeTimers();
+ });
+
+ it("mouseenter is a no-op when the caret is inside the hovered checkbox's item", () => {
+ // Caret is inside todo A → the caret handle already owns A's
+ // tooltip visibility. Hovering the same checkbox must not run the
+ // dwell-and-push cycle (the manager would flicker if it did).
+ moveCaretTo(1);
+ vi.advanceTimersByTime(TOOLTIP_DWELL_MS);
+ const beforeHover = livePopup();
+ expect(beforeHover).not.toBeNull(); // caret drove this popup
+
+ const inputA = checkboxOfItem("t-a");
+ inputA.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
+ // Advance well past the dwell — no NEW render, and no manager churn.
+ vi.advanceTimersByTime(TOOLTIP_DWELL_MS * 2);
+ // Same popup element — the caret handle's tooltip was never disturbed.
+ expect(livePopup()).toBe(beforeHover);
+
+ endFakeTimers();
+ });
+
+ it("mouseleave is a no-op when the caret is inside the hovered checkbox's item", () => {
+ // The caret owns A's popup. Even if a mouseleave arrives (mouse
+ // sweeping across the checkbox during selection), the hover branch
+ // must not tear the popup down — the caret handle still wants it.
+ moveCaretTo(1);
+ vi.advanceTimersByTime(TOOLTIP_DWELL_MS);
+ const beforeLeave = livePopup();
+ expect(beforeLeave).not.toBeNull();
+
+ const inputA = checkboxOfItem("t-a");
+ inputA.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true }));
+ expect(livePopup()).toBe(beforeLeave);
+
+ endFakeTimers();
+ });
+ });
+
// Pure-function tests for the tooltip HTML builder. Assert against the
// returned string directly rather than introspecting Bootstrap Tooltip
// instances — no `_config` peeking, no editor scaffolding needed.
diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts
index 3e4611ca00..9c52b638c3 100644
--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts
+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts
@@ -312,6 +312,10 @@ export default class TodoListMultistateEditing extends Plugin {
this._hoverHandles.set(input, handle);
// Cache the enclosing item's id — stable for this input's life, and we
// need it on every hover event to decide whether to yield to the caret.
+ /* v8 ignore next -- CKEditor's list plugin guarantees every
+ `<li>` inside a rendered todo list carries `data-list-item-id`
+ and the checkbox is nested inside that `<li>`. The `?.` /
+ `?? null` fallbacks are defensive. */
const ownItemId = input.closest<HTMLElement>("li")?.getAttribute("data-list-item-id") ?? null;
// Hover on the checkbox whose item already has the caret is a no-op:
// the caret handle already owns that item's tooltip visibility, and
@@ -374,11 +378,18 @@ export default class TodoListMultistateEditing extends Plugin {
this._caretHandle?.dispose();
this._caretHandle = null;
this._caretInput = target;
+ /* v8 ignore next -- inside the "same item, input changed"
+ branch, `target` cannot be null: `!itemChanged` means
+ `targetItemId === this._caretItemId`, and if `target` were
+ null then `targetItemId` would be null too, forcing the
+ `_caretItemId` we're comparing against to also be null —
+ but then `_caretInput` was null too, so `inputChanged`
+ would be false and we wouldn't be in this branch. */
if (target) {
this._caretHandle = this._tooltipManager.createHandle(target, this._computeContent(target));
this._caretHandle.show();
}
- /* v8 ignore next 4 -- reachable only when `forceShowIfSameTarget`
+ /* v8 ignore start -- reachable only when `forceShowIfSameTarget`
is true, which the render listener only sets when
`_refreshHoverHandles` observes a state change on the caret's
input WITHOUT the input being recreated — see the paired v8
@@ -387,6 +398,7 @@ export default class TodoListMultistateEditing extends Plugin {
this._caretHandle.setContent(this._computeContent(target));
this._caretHandle.show();
}
+ /* v8 ignore stop */
return;
}
diff --git a/packages/ckeditor5/src/utils/editor_tooltip_manager.spec.ts b/packages/ckeditor5/src/utils/editor_tooltip_manager.spec.ts
index 0f3ad7b1c4..d156774bea 100644
--- a/packages/ckeditor5/src/utils/editor_tooltip_manager.spec.ts
+++ b/packages/ckeditor5/src/utils/editor_tooltip_manager.spec.ts
@@ -154,6 +154,17 @@ describe("EditorTooltipManager", () => {
vi.advanceTimersByTime(2000);
expect(livePopup()).toBeNull();
});
+
+ it("no-ops when the element is detached by the time the timer fires", () => {
+ // Element is still connected when we schedule; disconnected by the
+ // time the setTimeout callback runs. The callback's `isConnected`
+ // guard must swallow the push so no popup ever materializes.
+ const handle = manager.createHandle(a, "hello");
+ handle.showAfter(1000);
+ host.remove(); // detaches `a` mid-dwell
+ vi.advanceTimersByTime(1000);
+ expect(livePopup()).toBeNull();
+ });
});
describe("setContent", () => {
diff --git a/packages/ckeditor5/src/utils/editor_tooltip_manager.ts b/packages/ckeditor5/src/utils/editor_tooltip_manager.ts
index f1b9f65083..def2e906fc 100644
--- a/packages/ckeditor5/src/utils/editor_tooltip_manager.ts
+++ b/packages/ckeditor5/src/utils/editor_tooltip_manager.ts
@@ -21,6 +21,10 @@ import { Tooltip } from "bootstrap";
dispose(): void;
__editorTooltipManagerDisposePatched?: true;
};
+ /* v8 ignore next -- the "already-patched" branch is unreachable in a
+ single-process test run: the module loads exactly once and the flag is
+ unset. The guard exists so the client's own copy of the patch (see
+ `apps/client/src/widgets/react/hooks.tsx`) can co-exist with this one. */
if (!proto.__editorTooltipManagerDisposePatched) {
const originalDispose = proto.dispose;
const disposedPlaceholder = {
@@ -256,9 +260,17 @@ export class EditorTooltipManager {
}
private _render(): void {
+ /* v8 ignore start -- defensively unreachable. Every caller
+ (`_pushOrMoveTop`, `_removeEntry`, `setContent`, the auto-hide
+ timer) either checks `_destroyed` themselves or is disarmed by
+ `destroy`: `destroy` clears `_stack` (so `_removeEntry`/`setContent`
+ find no entry and skip the `_render` call) and cancels the auto-hide
+ timer (so its callback never fires). Kept as a belt-and-braces guard
+ in case a new call site is added later. */
if (this._destroyed) {
return;
}
+ /* v8 ignore stop */
// A push or setContent interrupts any fade currently in progress —
// the user has done something, so we shouldn't tear the popup down.
const wasFading = this._cancelPendingHide();
@@ -274,6 +286,11 @@ export class EditorTooltipManager {
// Bootstrap's `setContent` avoids a dispose+recreate that would fade
// the popup out and back in for a mere content change.
if (this._currentElement === targetElement) {
+ /* v8 ignore next -- if we're in this branch, either both are the
+ same non-null element (then `_currentTooltip` is truthy and `top`
+ is defined) or both are null (then the outer `_render` was called
+ on an already-empty state, which no live code path does — pushes
+ always precede a same-element render). */
if (this._currentTooltip && top) {
this._currentTooltip.setContent({ ".tooltip-inner": top.content });
if (wasFading) {
@@ -321,9 +338,18 @@ export class EditorTooltipManager {
}
this._autoHideTimer = setTimeout(() => {
this._autoHideTimer = null;
+ /* v8 ignore start -- defensively unreachable. The timer is only
+ scheduled by `_resetAutoHide` when `_currentTooltip` is truthy,
+ i.e. the stack is non-empty. Every path that could empty the
+ stack (`hide`, `dispose`, `destroy`) runs synchronously and
+ either cancels this timer (`_cancelAutoHide`) or falls through
+ to `_render`, which re-runs `_resetAutoHide` after the mutation
+ and would clear us before we fire. Kept as a belt-and-braces
+ guard in case a new mutation path is added later. */
if (this._stack.length === 0) {
return;
}
+ /* v8 ignore stop */
this._stack.pop();
if (this._stack.length === 0) {
// Nothing left — fade the popup out with Bootstrap's transition.
@@ -352,14 +378,25 @@ export class EditorTooltipManager {
private _hideWithTransition(): void {
const tooltip = this._currentTooltip;
const element = this._currentElement;
+ /* v8 ignore start -- defensively unreachable. `_hideWithTransition`
+ is only called from the auto-hide timer, which is only scheduled
+ when `_currentTooltip` is truthy; `_currentElement` is set alongside
+ `_currentTooltip` in every `_render` code path. Kept as a
+ belt-and-braces guard in case a new call site is added later. */
if (!tooltip || !element) {
return;
}
+ /* v8 ignore stop */
const onHidden = () => {
element.removeEventListener("hidden.bs.tooltip", onHidden);
this._pendingHideCleanup = null;
// If we're still the current tooltip (no interrupting push landed)
// then dispose ourselves and reset state.
+ /* v8 ignore next -- the false branch (currentTooltip changed while
+ the fade was in flight) is defensively unreachable: every
+ interruption path (`_render` on a push, `destroy`) first calls
+ `_cancelPendingHide`, which removes this listener before it can
+ fire. Kept for safety if a new interruption path forgets to. */
if (this._currentTooltip === tooltip) {
tooltip.dispose();
this._currentTooltip = null;