import { TextSelection } from 'prosemirror-state'; import createTestEditor from '../testing/createTestEditor'; import extractSelectedLinesTo from './extractSelectedLinesTo'; import schema from '../schema'; describe('extractSelectedLinesTo', () => { test.each([ { label: 'should extract a single line containing the cursor to a heading', initial: { docHtml: '

Line 1
Line 2
Line 3

', // Put the cursor in the middle of the second line cursorPosition: 'Line 1

Line 3

', cursorPosition: '<'.length, }, convertTo: { type: schema.nodes.heading }, expected: { docJson: [ { type: 'paragraph', content: [{ type: 'text', text: 'Line 1' }], }, { type: 'heading', }, { type: 'paragraph', content: [{ type: 'text', text: 'Line 3' }], }, ], cursorPosition: '<'.length, }, }, { label: 'should convert the last line in a paragraph to a heading', initial: { docHtml: '

Line 1

End

', cursorPosition: '<'.length, }, }, { label: 'should convert the first line in a paragraph to a heading', initial: { docHtml: '


Line 1

End

', cursorPosition: '<'.length, }, convertTo: { type: schema.nodes.heading }, expected: { docJson: [ { type: 'heading', }, { type: 'paragraph', content: [{ type: 'text', text: 'Line 1' }], }, { type: 'paragraph', content: [{ type: 'text', text: 'End' }], }, ], cursorPosition: '<'.length, }, }, ])('$label', ({ initial, expected, convertTo }) => { const view = createTestEditor({ html: initial.docHtml, }); view.dispatch(view.state.tr.setSelection( TextSelection.create( view.state.doc, initial.cursorPosition, ), )); const { transaction } = extractSelectedLinesTo( { type: convertTo.type, attrs: convertTo.attrs ?? { } }, view.state.tr, view.state.selection, ); view.dispatch(transaction); expect(view.state.doc.toJSON()).toMatchObject({ content: expected.docJson, }); // All of the tests in this group expect a single cursor expect(view.state.selection.empty).toBe(true); expect(view.state.selection.from).toBe(expected.cursorPosition); }); test('should extract multiple lines in the same paragraph to a new paragraph', () => { const view = createTestEditor({ html: '

Line 1
Line 2
Line 3
Line 4

', }); view.dispatch(view.state.tr.setSelection( TextSelection.create( view.state.doc, // ...from the middle of the second line... '