|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + getClipboardTextForContentEditablePaste, |
| 4 | + hasNonEmptyPlainTextOnClipboard, |
| 5 | +} from './use-chat-attachments'; |
| 6 | + |
| 7 | +function mockClipboard(getData: (type: string) => string) { |
| 8 | + return { getData } as unknown as ClipboardEvent['clipboardData']; |
| 9 | +} |
| 10 | + |
| 11 | +describe('getClipboardTextForContentEditablePaste', () => { |
| 12 | + it('returns null when clipboardData is null', () => { |
| 13 | + expect(getClipboardTextForContentEditablePaste(null)).toBe(null); |
| 14 | + }); |
| 15 | + |
| 16 | + it('returns plain text when text/plain is non-empty', () => { |
| 17 | + const body = "const x = 1\n"; |
| 18 | + expect( |
| 19 | + getClipboardTextForContentEditablePaste(mockClipboard((t) => (t === 'text/plain' ? body : ''))) |
| 20 | + ).toBe(body); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns null when text/plain is only whitespace and html is empty', () => { |
| 24 | + expect( |
| 25 | + getClipboardTextForContentEditablePaste( |
| 26 | + mockClipboard((t) => (t === 'text/plain' ? ' \n' : t === 'text/html' ? '' : '')) |
| 27 | + ) |
| 28 | + ).toBe(null); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns innerText when text/plain is empty but html has a pre block', () => { |
| 32 | + const html = '<html><body><pre>line1\nline2</pre></body></html>'; |
| 33 | + expect( |
| 34 | + getClipboardTextForContentEditablePaste( |
| 35 | + mockClipboard((t) => (t === 'text/plain' ? '' : t === 'text/html' ? html : '')) |
| 36 | + ) |
| 37 | + ).toBe('line1\nline2'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns null when html is only an image', () => { |
| 41 | + const html = '<html><body><img src="data:image/png;base64,xx" /></body></html>'; |
| 42 | + expect( |
| 43 | + getClipboardTextForContentEditablePaste( |
| 44 | + mockClipboard((t) => (t === 'text/plain' ? '' : t === 'text/html' ? html : '')) |
| 45 | + ) |
| 46 | + ).toBe(null); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('hasNonEmptyPlainTextOnClipboard', () => { |
| 51 | + it('returns false when clipboardData is null', () => { |
| 52 | + expect(hasNonEmptyPlainTextOnClipboard(null)).toBe(false); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns true when text/plain has code from an IDE', () => { |
| 56 | + const body = "function x() {\n return 1;\n}\n"; |
| 57 | + expect(hasNonEmptyPlainTextOnClipboard(mockClipboard((t) => (t === 'text/plain' ? body : '')))).toBe( |
| 58 | + true |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns true when only html provides text', () => { |
| 63 | + const html = '<pre>abc</pre>'; |
| 64 | + expect( |
| 65 | + hasNonEmptyPlainTextOnClipboard( |
| 66 | + mockClipboard((t) => (t === 'text/plain' ? '' : t === 'text/html' ? html : '')) |
| 67 | + ) |
| 68 | + ).toBe(true); |
| 69 | + }); |
| 70 | +}); |
0 commit comments