|
| 1 | +import { EditorState } from '@codemirror/state' |
| 2 | +import { EditorView } from '@codemirror/view' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import { createClipboardOutput } from '../clipboardOutput' |
| 5 | + |
| 6 | +function filterClipboardOutput(text: string, isWindows: boolean) { |
| 7 | + const state = EditorState.create({ |
| 8 | + extensions: [createClipboardOutput(isWindows)], |
| 9 | + }) |
| 10 | + |
| 11 | + return state |
| 12 | + .facet(EditorView.clipboardOutputFilter) |
| 13 | + .reduce((output, filter) => filter(output, state), text) |
| 14 | +} |
| 15 | + |
| 16 | +describe('clipboard output', () => { |
| 17 | + it('converts LF line endings to CRLF on Windows', () => { |
| 18 | + expect(filterClipboardOutput('first\nsecond\nthird', true)).toBe( |
| 19 | + 'first\r\nsecond\r\nthird', |
| 20 | + ) |
| 21 | + }) |
| 22 | + |
| 23 | + it('does not duplicate CR in existing CRLF line endings on Windows', () => { |
| 24 | + expect(filterClipboardOutput('first\r\nsecond', true)).toBe( |
| 25 | + 'first\r\nsecond', |
| 26 | + ) |
| 27 | + }) |
| 28 | + |
| 29 | + it('leaves line endings unchanged on macOS and Linux', () => { |
| 30 | + expect(filterClipboardOutput('first\nsecond', false)).toBe('first\nsecond') |
| 31 | + }) |
| 32 | + |
| 33 | + it('leaves text without line endings unchanged', () => { |
| 34 | + expect(filterClipboardOutput('single line', true)).toBe('single line') |
| 35 | + }) |
| 36 | +}) |
0 commit comments