Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor: improve test coverage
Adorian Dorana614b734c410
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.tsindex a709bfede8..bbc48f30cc 100644--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts@@ -2,11 +2,11 @@ import {addListToDropdown, Bold, ButtonView, type ClassicEditor, Collection, createDropdown,type DropdownMenuNestedMenuView, DropdownView, Essentials, Italic,type ListDropdownItemDefinition, ListSeparatorView, type Locale, Paragraph, Plugin,- SplitButtonView, type ToolbarView, UIModel+ SplitButtonView, type ToolbarView, UIModel, View} from "ckeditor5";import { beforeEach, describe, expect, it, vi } from "vitest";-import { createTestEditor } from "../../test/editor-kit.js";+import { createTestEditor, createTestEditorOf } from "../../test/editor-kit.js";import ToolbarGroupMenu from "./toolbar_group_menu.js";const insertedSamples: string[] = [];@@ -20,6 +20,11 @@ beforeEach(() => {now = "noon";});+/** A toolbar item that is neither a button nor a dropdown, focusable as a toolbar wants. */+class PlainItemView extends View {+ focus() {}+}+/*** Stands in for the group's dropdown entries — `mermaid` and `dateTime` are split buttons over a* list, with an action of their own on the button half.@@ -62,6 +67,36 @@ class SampleDropdown extends Plugin {return dropdown;});+ // A list carrying everything a dropdown list can hold besides plain rows.+ this.editor.ui.componentFactory.add("oddList", (locale: Locale) => {+ const dropdown = createDropdown(locale);+ dropdown.buttonView.set({ label: "Odd list", icon: "<svg />" });++ const items = new Collection<ListDropdownItemDefinition>();+ items.add({ type: "separator" });+ items.add({ type: "button", model: new UIModel({ label: "First", withText: true }) });+ items.add({ type: "separator" });+ items.add({ type: "button", model: new UIModel({ withText: true }) });+ items.add({+ type: "group",+ model: new UIModel({ label: "Grouped" }),+ items: new Collection([{+ type: "button" as const,+ model: new UIModel({ label: "Inside", withText: true })+ }])+ });+ addListToDropdown(dropdown, items);++ return dropdown;+ });++ // Neither a button nor a dropdown: a toolbar item the menu has no row to draw for.+ this.editor.ui.componentFactory.add("plainView", (locale: Locale) => {+ const view = new PlainItemView(locale);+ view.setTemplate({ tag: "span", attributes: { class: ["ck"] } });+ return view;+ });+// A dropdown with no list of its own: nothing to open onto, so it stays a plain row.this.editor.ui.componentFactory.add("panelOnly", (locale: Locale) => {const dropdown = createDropdown(locale);@@ -220,6 +255,32 @@ describe("ToolbarGroupMenu", () => {expect(document.activeElement).toBe(mounted.buttonView.element);});+ it("draws a rule where the list has one, never at its head, and names every row", async () => {+ const editor = await createTestEditor(PLUGINS, {+ toolbar: { items: [{ ...MENU_GROUP, items: ["oddList"] }] }+ });++ const dropdown = openDropdown(getToolbar(editor), "Insert");++ // The group of the list holds rows the menu has no place for, so it is left out; the+ // unnamed row is drawn all the same, since its view is what runs.+ const rows = itemLabels(openSubmenu(dropdown).listView.items);+ expect(rows).toStrictEqual(["First", "—", ""]);+ });++ // A rule at the very head is CKEditor's to drop, so the only way one reaches the menu with+ // nothing drawn before it is behind an item the menu passed over.+ it("passes over an item it has no row for, and the rule that follows it", async () => {+ const editor = await createTestEditor(PLUGINS, {+ toolbar: {+ items: [{ ...MENU_GROUP, items: ["plainView", "|", "bold", "-", "italic"] }]+ }+ });++ const menu = openGroup(getToolbar(editor), "Insert");+ expect(rowLabels(menu)).toStrictEqual(["Bold", "Italic"]);+ });+it("converts a group of the block toolbar, filled after the plugin is set up", async () => {const { BlockToolbar } = await import("ckeditor5");const editor = await createTestEditor([BlockToolbar, ...PLUGINS], {@@ -231,13 +292,15 @@ describe("ToolbarGroupMenu", () => {expect(rowLabels(menu)).toStrictEqual(["Bold", "—", "Italic", "Insert sample"]);});+ // A real `BalloonEditor`, whose UI view carries no fixed bar at all: the group is reached+ // through the plugin holding the selection toolbar, and nowhere else.it("converts a group of the selection balloon", async () => {- const { BalloonToolbar } = await import("ckeditor5");- const editor = await createTestEditor([BalloonToolbar, ...PLUGINS], {- toolbar: { items: ["bold"] },- balloonToolbar: [MENU_GROUP]+ const { BalloonEditor, BalloonToolbar } = await import("ckeditor5");+ const editor = await createTestEditorOf(BalloonEditor, PLUGINS, {+ toolbar: [MENU_GROUP]});+ expect((editor.ui.view as { toolbar?: ToolbarView }).toolbar).toBeUndefined();const menu = openGroup(editor.plugins.get(BalloonToolbar).toolbarView, "Insert");expect(rowLabels(menu)).toStrictEqual(["Bold", "—", "Italic", "Insert sample"]);});diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.tsindex 30c8f37f04..8f42bdbbc9 100644--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts@@ -130,6 +130,7 @@ export default class ToolbarGroupMenu extends Plugin {this.plans.set(dropdown, plan);const menuView = dropdown.menuView;+ /* v8 ignore next 3 -- `addMenuToDropdown()` has just set the menu this narrows */if (!menuView) {return;}@@ -152,6 +153,7 @@ export default class ToolbarGroupMenu extends Plugin {for (const menu of menuView.menus) {const opener = plan.openers.get(menu.id);+ /* v8 ignore next 3 -- every submenu the menu holds was described from an opener */if (!opener) {continue;}@@ -200,7 +202,7 @@ export default class ToolbarGroupMenu extends Plugin {plan.rows.set(id, { view: entry, run: () => entry.fire("execute") });opener.rowIds.push(id);- const button = new DropdownMenuListItemButtonView(locale, id, entry.label ?? "");+ const button = new DropdownMenuListItemButtonView(locale, id, labelOf(entry));button.icon = entry.icon;button.bind("isEnabled").to(entry, "isEnabled");items.add(new DropdownMenuListItemView(locale, menu, button));@@ -261,6 +263,7 @@ function discardMenu(dropdown: DropdownView, plan: MenuPlan | undefined) {// The controls mounted into it belong to the group, so they leave before the menu holding them// is destroyed; the next draw mounts them again.+ /* v8 ignore next -- only a redraw discards a menu, and it draws from a plan */for (const { row, source } of plan?.mounted ?? []) {row.children.remove(source);}@@ -358,10 +361,15 @@ function planGroupMenu(items: View[]) {return plan;}+/** What a row is called: the label of the view it stands for, or nothing where it sets none. */+function labelOf(view: { label?: string }) {+ return view.label ?? "";+}+/** Records a row against the view it stands for, and describes it to the menu. */function describeRow(plan: MenuPlan, id: string, view: SourceButton) {plan.rows.set(id, { view, run: () => view.fire("execute") });- return { id, label: view.label ?? "" };+ return { id, label: labelOf(view) };}/**@@ -379,7 +387,7 @@ function planSubmenu(plan: MenuPlan, takeId: () => string, dropdown: DropdownVieif (!listEntriesOf(dropdown).length) {const mountId = takeId();plan.mounts.set(mountId, dropdown);- plan.definition.push({ id: mountId, label: opener.label ?? "" });+ plan.definition.push({ id: mountId, label: labelOf(opener) });return;}@@ -390,7 +398,7 @@ function planSubmenu(plan: MenuPlan, takeId: () => string, dropdown: DropdownVierowIds: [],run: opener instanceof SplitButtonView ? () => opener.fire("execute") : undefined});- plan.definition.push({ id, menu: opener.label ?? "", children: [] });+ plan.definition.push({ id, menu: labelOf(opener), children: [] });}/** The rows a dropdown's own list holds, which it builds the first time it is opened. */diff --git a/packages/ckeditor5/test/editor-kit.ts b/packages/ckeditor5/test/editor-kit.tsindex 524ff9a356..59c4173552 100644--- a/packages/ckeditor5/test/editor-kit.ts+++ b/packages/ckeditor5/test/editor-kit.ts@@ -1,4 +1,4 @@-import { ClassicEditor } from "ckeditor5";+import { ClassicEditor, type Editor } from "ckeditor5";/*** Shared editor lifecycle helpers for the spec suite.@@ -18,10 +18,15 @@ import { ClassicEditor } from "ckeditor5";type EditorCreateConfig = NonNullable<Parameters<typeof ClassicEditor.create>[1]>;interface TrackedEditor {- editor: ClassicEditor;+ editor: Editor;editorElement: HTMLDivElement;}+/** An editor class as the kit reaches for it: `ClassicEditor`, `BalloonEditor`, and the rest. */+interface EditorClass<T extends Editor> {+ create(sourceElement: HTMLElement, config: EditorCreateConfig): Promise<T>;+}+const trackedEditors: TrackedEditor[] = [];/**@@ -34,10 +39,22 @@ export async function createTestEditor(plugins: EditorCreateConfig["plugins"],extraConfig: Omit<EditorCreateConfig, "plugins"> = {}): Promise<ClassicEditor> {+ return createTestEditorOf(ClassicEditor, plugins, extraConfig);+}++/**+ * The same, over the editor class the spec names — for a spec whose subject is what a given+ * editor builds around it, such as the toolbar a `BalloonEditor` has instead of a fixed bar.+ */+export async function createTestEditorOf<T extends Editor>(+ EditorClass: EditorClass<T>,+ plugins: EditorCreateConfig["plugins"],+ extraConfig: Omit<EditorCreateConfig, "plugins"> = {}+): Promise<T> {const editorElement = document.createElement("div");document.body.appendChild(editorElement);- const editor = await ClassicEditor.create(editorElement, {+ const editor = await EditorClass.create(editorElement, {licenseKey: "GPL",plugins,...extraConfig@@ -51,7 +68,7 @@ export async function createTestEditor(* Return the host `<div>` an editor was created over via {@link createTestEditor}. Throws if the* editor was not created through the kit, so callers narrow the element without a non-null `!`.*/-export function getEditorElement(editor: ClassicEditor): HTMLDivElement {+export function getEditorElement(editor: Editor): HTMLDivElement {const tracked = trackedEditors.find((entry) => entry.editor === editor);if (!tracked) {throw new Error("Editor was not created via createTestEditor(); no host element is tracked.");