Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2import {
3 $createParagraphNode,
4 $createTextNode,
5 $getRoot,
6 $getSelection,
7 $isRangeSelection,
8 COMMAND_PRIORITY_EDITOR,
9 createEditor,
10 PASTE_COMMAND,
11} from "lexical";
12
13import { registerComposerInlineTokenPaste } from "./composerInlineTokenPaste";
14
15class TestClipboardEvent extends Event {
16 readonly clipboardData: DataTransfer;
17
18 constructor(text: string) {
19 super("paste", { cancelable: true });
20 this.clipboardData = {
21 files: [],
22 getData: (type: string) => (type === "text/plain" ? text : ""),
23 } as unknown as DataTransfer;
24 }
25}
26
27describe("registerComposerInlineTokenPaste", () => {
28 afterEach(() => {
29 vi.unstubAllGlobals();
30 });
31
32 it("handles a copied mention without also running the plain-text paste fallback", () => {
33 vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
34 const editor = createEditor();
35 const mention = "[improve-deploy-error-logging.md](.changeset/improve-deploy-error-logging.md)";
36 const plainTextFallback = vi.fn(() => {
37 const selection = $getSelection();
38 if (!$isRangeSelection(selection)) return false;
39 selection.insertText(mention);
40 return true;
41 });
42
43 editor.update(
44 () => {
45 const paragraph = $createParagraphNode();
46 $getRoot().append(paragraph);
47 paragraph.selectEnd();
48 },
49 { discrete: true },
50 );
51 registerComposerInlineTokenPaste(editor, {
52 createMentionNode: (path) => $createTextNode(`<mention:${path}>`),
53 getExpandedAbsoluteOffsetForPoint: () => 0,
54 });
55 editor.registerCommand(PASTE_COMMAND, plainTextFallback, COMMAND_PRIORITY_EDITOR);
56
57 const event = new TestClipboardEvent(mention);
58 let handled = false;
59 editor.update(
60 () => {
61 handled = editor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
62 },
63 { discrete: true },
64 );
65
66 expect(handled).toBe(true);
67 expect(plainTextFallback).not.toHaveBeenCalled();
68 expect(event.defaultPrevented).toBe(true);
69 expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe(
70 "<mention:.changeset/improve-deploy-error-logging.md> ",
71 );
72 });
73});