diff --git a/tests/ai-toolbar.test.ts b/tests/ai-toolbar.test.ts index d963cd1..ec01646 100644 --- a/tests/ai-toolbar.test.ts +++ b/tests/ai-toolbar.test.ts @@ -333,6 +333,49 @@ describe("AI capture redesign", () => { expect(onImagesSelect).toHaveBeenCalledWith([image]); }); + test("textarea-targeted paste bypasses the document listener and is handled only by the textarea path", async () => { + const onImagesSelect = mock(); + const image = new File(["image-bytes"], "clipboard.png", { + type: "image/png", + }); + + const textareaProps = await renderToolbarBoundary({ onImagesSelect }); + const handleCapturePaste = getDocumentListener( + "paste", + (entry) => entry.options?.capture === true, + ); + const handleDocumentPaste = getDocumentListener( + "paste", + (entry) => !entry.options?.capture, + ); + const preventDefault = mock(); + const textareaTarget = { + tagName: "TEXTAREA", + isContentEditable: false, + } as HTMLTextAreaElement; + const pasteEvent = { + target: textareaTarget, + currentTarget: textareaTarget, + clipboardData: createClipboardData([image]), + preventDefault, + }; + + handleCapturePaste(pasteEvent as unknown as Event); + expect(preventDefault).not.toHaveBeenCalled(); + expect(onImagesSelect).not.toHaveBeenCalled(); + + textareaProps.onPaste?.( + pasteEvent as unknown as React.ClipboardEvent, + ); + expect(preventDefault).toHaveBeenCalledTimes(1); + expect(onImagesSelect).toHaveBeenCalledTimes(1); + expect(onImagesSelect).toHaveBeenCalledWith([image]); + + handleDocumentPaste(pasteEvent as unknown as Event); + expect(preventDefault).toHaveBeenCalledTimes(1); + expect(onImagesSelect).toHaveBeenCalledTimes(1); + }); + test("document paste forwards clipboard images to onImagesSelect only for non-editable targets", async () => { const onImagesSelect = mock(); const image = new File(["image-bytes"], "clipboard.png", {