Skip to content

Build your personal knowledge base with Trilium Notes

CK editor: improve test coverage

Adorian Doran3c2c2bd4e2b0
diff --git a/packages/ckeditor5/src/i18n.spec.ts b/packages/ckeditor5/src/i18n.spec.ts
index 1b75eb6559..9bd89386ef 100644
--- a/packages/ckeditor5/src/i18n.spec.ts
+++ b/packages/ckeditor5/src/i18n.spec.ts
@@ -270,6 +270,23 @@ describe("registerCkTranslations", () => {
expect(boldButton.label).toBe("Fett");
});
+ it("hands the plural form on as a number, whichever way the catalog answers", async () => {
+ // A catalog with two forms answers with a boolean, one with more answers with an index.
+ // CKEditor reads the result through `Number()` when it takes a plural form itself, so what
+ // is laid here has to answer the same way rather than with what the catalog happened to say.
+ await registerCkTranslations("de");
+ await registerCkTranslations("ro");
+
+ for (const [ languageCode, counts ] of [ [ "de", [ 1, 2 ] ], [ "ro", [ 1, 2, 20 ] ] ] as const) {
+ const pluralForm = window.CKEDITOR_TRANSLATIONS?.[languageCode]?.getPluralForm;
+ if (!pluralForm) throw new Error(`expected a plural form for '${languageCode}'`);
+
+ for (const count of counts) {
+ expect(typeof pluralForm(count)).toBe("number");
+ }
+ }
+ });
+
it("fetches a locale once, however many fields ask for it", async () => {
// Every field on the page asks as it is built, and a catalog is a chunk to go and get.
expect(registerCkTranslations("fr")).toBe(registerCkTranslations("fr"));
diff --git a/packages/ckeditor5/src/plugins/math/utils.spec.ts b/packages/ckeditor5/src/plugins/math/utils.spec.ts
index 508a84859a..33bdeb4065 100644
--- a/packages/ckeditor5/src/plugins/math/utils.spec.ts
+++ b/packages/ckeditor5/src/plugins/math/utils.spec.ts
@@ -1,13 +1,15 @@
// Side-effect import: declares the `math` key on EditorConfig used by the editor configs below.
import './math.js';
-import { describe, expect, it } from 'vitest';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import type { MathJax2 } from './typings_external.js';
import {
extractDelimiters,
hasDelimiters,
isMathJaxVersion2,
- isMathJaxVersion3
+ isMathJaxVersion3,
+ renderEquation
} from './utils.js';
describe( 'utils: delimiter helpers', () => {
@@ -72,3 +74,42 @@ describe( 'utils: MathJax version detection', () => {
expect( isMathJaxVersion2( {} ) ).toBe( false );
} );
} );
+
+describe( 'utils: rendering through MathJax 2', () => {
+ /** What MathJax 2 is, as far as the renderer is concerned: a Hub with a queue. */
+ function installMathJax2() {
+ const queue = vi.fn();
+ globalThis.MathJax = { Hub: { Queue: queue } } as unknown as MathJax2;
+ return queue;
+ }
+
+ afterEach( () => {
+ globalThis.MathJax = undefined;
+ } );
+
+ it( 'hands the equation to the Hub as delimited text, display and inline alike', async () => {
+ const queue = installMathJax2();
+ const element = document.createElement( 'span' );
+
+ // Text rather than markup: MathJax 2 typesets by scanning text nodes for the delimiters.
+ await renderEquation( 'x^2', element, 'mathjax', undefined, true );
+ await vi.waitFor( () => expect( element.textContent ).toBe( '\\[x^2\\]' ) );
+
+ await renderEquation( 'x^2', element, 'mathjax', undefined, false );
+ await vi.waitFor( () => expect( element.textContent ).toBe( '\\(x^2\\)' ) );
+
+ expect( queue ).toHaveBeenCalledWith( [ 'Typeset', expect.anything(), element ] );
+ } );
+
+ it( 'leaves the element alone where MathJax never arrived', async () => {
+ const element = document.createElement( 'span' );
+ element.textContent = 'untouched';
+
+ // The renderer falls through to MathJax 2 whenever MathJax is not version 3, this build
+ // included, so it has to answer for finding nothing there at all.
+ await renderEquation( 'x^2', element, 'mathjax' );
+ await new Promise( resolve => window.setTimeout( resolve ) );
+
+ expect( element.textContent ).toBe( 'untouched' );
+ } );
+} );
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 7596e549d3..0b45d4048e 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
@@ -182,6 +182,19 @@ describe("TodoListMultistateEditing", () => {
expect(getBlock(editor, 0).hasAttribute(TASK_STATE_ATTRIBUTE)).toBe(false);
});
+ it("leaves a checkbox that belongs to no todo item alone", () => {
+ // The converter listens for every `<input>` in the incoming data, so it meets
+ // checkboxes that upstream never marked as a todo item: one in a plain list, and one
+ // loose in a paragraph, whose block is not even an element by the time it is asked.
+ editor.setData('<ul><li data-trilium-task-state="doing">Plain<input type="checkbox"></li></ul>'
+ + '<p><input type="checkbox" data-trilium-task-state="doing">Loose</p>');
+
+ for (let index = 0; index < (editor.model.document.getRoot()?.childCount ?? 0); index++) {
+ expect(getBlock(editor, index).hasAttribute(TASK_STATE_ATTRIBUTE)).toBe(false);
+ }
+ expect(editor.getData()).not.toContain("data-trilium-task-state");
+ });
+
it("upcasts a state onto its own item only, never onto nested or sibling items", () => {
// The list model is flat, so a nested item is a sibling block covered by the
// parent <li>'s converted range. The state must not spread across it.
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 be39fa0c40..8a32af2942 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
@@ -494,6 +494,8 @@ function upcastTaskState(
return;
}
const listItem = findListItemAncestor(data.viewItem);
+ /* v8 ignore next 3 -- the block above is a todo item only because upstream made one out of the
+ `<li>` this very checkbox sits in, so by here there is always one to find */
if (!listItem) {
return;
}
@@ -515,6 +517,7 @@ function findListItemAncestor(viewElement: ViewElement): ViewElement | null {
}
ancestor = ancestor.parent;
}
+ /* v8 ignore next -- only ever called from a checkbox inside a todo item's own `<li>` */
return null;
}