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 Doran23a0f021346a
diff --git a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.tsindex 5bec498ade..26c0ec2358 100644--- a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts@@ -132,10 +132,8 @@ describe("FileUploadUI", () => {expect([ dropdown.isEnabled, actionView.isEnabled ]).toEqual([ false, false ]);});- it("lists no embeds in an editor without includes", async () => {- const withoutIncludes = await createTestEditor([Essentials, Paragraph, FileUploadUI]);- withoutIncludes.commands.add("fileUpload", new Command(withoutIncludes));- editor = withoutIncludes;+ it("builds the button in an editor without the commands, listing no embeds", async () => {+ editor = await createTestEditor([Essentials, Paragraph, FileUploadUI]);const labels = createDropdown().items.map((item) => item.label);expect(labels).toEqual([ "Attach file as a link" ]);diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.tsindex e2675467fb..f711e3bf1b 100644--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.ts+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.ts@@ -134,17 +134,7 @@ export default class FileUploadEditing extends Plugin {writer.setAttribute( 'uploadStatus', 'reading', fileElement );} );- let finish = () => {};- const done = new Promise<void>( resolve => {- finish = () => resolve();- } );- this.fire<FileUploadEvent>( 'upload', {- fileName: String( fileElement.getAttribute( 'uploadFileName' ) ?? '' ),- loader,- done- } );-- return loader.read()+ const upload = loader.read().then( () => {const promise = loader.upload();@@ -187,8 +177,15 @@ export default class FileUploadEditing extends Plugin {model.enqueueChange( { isUndoable: false }, writer => {writer.remove( fileElement );} );- } )- .finally( finish );+ } );++ this.fire<FileUploadEvent>( 'upload', {+ fileName: String( fileElement.getAttribute( 'uploadFileName' ) ?? '' ),+ loader,+ done: Promise.allSettled( [ upload ] ).then( () => undefined )+ } );++ return upload;function clean() {model.enqueueChange( { isUndoable: false }, writer => {diff --git a/packages/ckeditor5/src/plugins/includenote.spec.ts b/packages/ckeditor5/src/plugins/includenote.spec.tsindex 389da2119d..eea7e2589e 100644--- a/packages/ckeditor5/src/plugins/includenote.spec.ts+++ b/packages/ckeditor5/src/plugins/includenote.spec.ts@@ -483,13 +483,16 @@ describe("IncludeNote", () => {// Exercise the falsy `if (command)` branch in IncludeNoteUI: when the command lookup// returns undefined, the button must still be created (just without the binding).const realGet = editor.commands.get.bind(editor.commands);+ const absent: string[] = [ COMMAND_NAME, CONVERT_EMBED_TO_LINK_COMMAND ];const getSpy = vi.spyOn(editor.commands, "get")- .mockImplementation((name) => (name === COMMAND_NAME ? undefined : realGet(name)));+ .mockImplementation((name) => (absent.includes(name) ? undefined : realGet(name)));try {const view = editor.ui.componentFactory.create("includeNote") as unknown as { label: string };expect(view.label).toBe("Include note");+ const convert = editor.ui.componentFactory.create(CONVERT_EMBED_TO_LINK_COMMAND);+ expect((convert as unknown as { label: string }).label).toBe("Convert to link");} finally {getSpy.mockRestore();}@@ -603,18 +606,35 @@ describe("IncludeNote with attachments", () => {expect(getModelData(editor.model, { withoutSelection: true })).toBe(`<paragraph>Before <reference href="${LINK_HREF}"></reference> after</paragraph>`);++ const relinked = editor.editing.view.getDomRoot()?.querySelector("a.reference-link");+ editor.execute(EMBED_ATTACHMENT_LINK_COMMAND, { domElement: relinked });+ expect(findIncludeNote(editor)?.getAttribute("boxSize")).toBe("medium");});- it("leaves a link to a note alone", () => {+ it("leaves a link to a note, and anything other than a link, alone", () => {editor.setData(`<p>Before <a class="reference-link" href="#root/noteAbc">x</a></p>`);const before = getModelData(editor.model, { withoutSelection: true });- const link = editor.editing.view.getDomRoot()?.querySelector("a.reference-link");+ const domRoot = editor.editing.view.getDomRoot();+ const link = domRoot?.querySelector("a.reference-link");+ const paragraph = domRoot?.querySelector("p");+ const detached = document.createElement("a");- editor.execute(EMBED_ATTACHMENT_LINK_COMMAND, { domElement: link });+ for (const domElement of [ link, paragraph, detached ]) {+ editor.execute(EMBED_ATTACHMENT_LINK_COMMAND, { domElement });+ }expect(getModelData(editor.model, { withoutSelection: true })).toBe(before);});+ it("loads an include that names nothing, as one saved mid-upload, as an empty box", () => {+ editor.setData("<section class=\"include-note\" data-box-size=\"small\"></section>");++ expect(renderEmbeds()).toHaveLength(1);+ expect(loadIncludedNote).not.toHaveBeenCalled();+ expect(loadIncludedAttachment).not.toHaveBeenCalled();+ });+it("turns an attachment embed back into a link, and offers it only for one", async () => {const command = editor.commands.get(CONVERT_EMBED_TO_LINK_COMMAND);const button = editor.ui.componentFactory.create(CONVERT_EMBED_TO_LINK_COMMAND);@@ -632,11 +652,29 @@ describe("IncludeNote with attachments", () => {expect(command?.isEnabled).toBe(true);expect(button.isVisible).toBe(true);- await editor.execute(CONVERT_EMBED_TO_LINK_COMMAND);+ button.fire("execute");expect(getAttachmentHref).toHaveBeenCalledWith("att1");- expect(getModelData(editor.model, { withoutSelection: true }))- .toBe(`<paragraph><reference href="${LINK_HREF}"></reference></paragraph>`);+ await vi.waitFor(() => expect(getModelData(editor.model, { withoutSelection: true }))+ .toBe(`<paragraph><reference href="${LINK_HREF}"></reference></paragraph>`));+ });++ it("converts nothing when no attachment embed is selected", async () => {+ editor.setData(embedHtml() + "<p>after</p>");+ setModelData(editor.model, "<paragraph>foo[]bar</paragraph>");+ const before = editor.getData();++ // The decorated `Command#execute()` skips a disabled command, so the command is forced+ // enabled to run `execute()` with no embed selected.+ const command = editor.commands.get(CONVERT_EMBED_TO_LINK_COMMAND) as {+ isEnabled: boolean;+ execute(): Promise<void>;+ };+ command.isEnabled = true;+ await command.execute();++ expect(getAttachmentHref).not.toHaveBeenCalled();+ expect(editor.getData()).toBe(before);});it("keeps an embed whose attachment has no link to go back to", async () => {diff --git a/packages/ckeditor5/src/plugins/referencelink.spec.ts b/packages/ckeditor5/src/plugins/referencelink.spec.tsindex bc14dd10a8..7ffb8b3db9 100644--- a/packages/ckeditor5/src/plugins/referencelink.spec.ts+++ b/packages/ckeditor5/src/plugins/referencelink.spec.ts@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";import { installGlobMock } from "../../test/globals-test-kit.js";-import ReferenceLink from "./referencelink.js";+import ReferenceLink, { getAttachmentId } from "./referencelink.js";describe("ReferenceLink", () => {let editor: ClassicEditor;@@ -240,6 +240,27 @@ describe("ReferenceLink", () => {expect(editor.commands.get("undo")?.isEnabled).toBe(false);});+ it("redraws the links to a renamed attachment without changing the document", () => {+ const href = "#root/owner?viewMode=attachments&attachmentId=renamed";+ const htmlHref = href.replace("&", "&");+ editor.setData(`<p><a class="reference-link" href="${htmlHref}">a</a></p>`);+ const before = getModelData(editor.model);+ loadReferenceLinkTitle.mockClear();++ editor.plugins.get("ReferenceLinkEditing").updateAttachmentLinks([+ { attachmentId: "renamed", isDeleted: false }+ ]);++ expect(loadReferenceLinkTitle).toHaveBeenCalledWith(expect.anything(), href);+ expect(getModelData(editor.model)).toBe(before);+ });++ it("reads the attachment a link points to, and none from a note link or a missing href", () => {+ expect(getAttachmentId("#root/owner?viewMode=attachments&attachmentId=att1")).toBe("att1");+ expect(getAttachmentId("#root/noteAbc")).toBeNull();+ expect(getAttachmentId(undefined)).toBeNull();+ });+it("dataDowncasts a reference back to an anchor, resolving the title synchronously", () => {editor.setData('<p><a class="reference-link" href="#root/noteAbc">old</a></p>');