import { positionToCursorOffset, cursorOffsetToPosition, formatCode, formatAndInsert, } from './formatting'; describe('cursor offset to position', () => { it('should work for one line', () => { const code = `

Title

`; const position = 4; // Before the capital T expect(cursorOffsetToPosition(code, position)).toEqual({ line: 0, ch: 4 }); }); it('should work across multiple lines', () => { const code = `
\n

Title

\n
`; const position = 10; // Before the capital T expect(cursorOffsetToPosition(code, position)).toEqual({ line: 1, ch: 4 }); }); }); describe('position to cursor offset', () => { it('should work for one line', () => { const code = `

Title

`; const offset = { line: 0, ch: 4, }; // Before the capital T expect(positionToCursorOffset(code, offset)).toEqual(4); }); it('should work across multiple lines', () => { const code = `
\n

Title

\n
`; const offset = { line: 1, ch: 4, }; expect(positionToCursorOffset(code, offset)).toEqual(10); }); }); describe('formatting code', () => { it('should handle one line', () => { const code = `

Title

`; expect(formatCode({ code, cursor: { line: 0, ch: 9 } })).toEqual({ cursor: { line: 1, ch: 6 }, code: `
\n

Title

\n
\n`, }); }); it('should handle multiple lines', () => { const code = `
\n

Title

\n
`; expect(formatCode({ code, cursor: { line: 1, ch: 4 } })).toEqual({ cursor: { line: 1, ch: 6 }, code: `
\n

Title

\n
\n`, }); }); it('should handle multiple lines with cursor at start of line', () => { const code = `
\n

Title

\n
`; expect(formatCode({ code, cursor: { line: 1, ch: 0 } })).toEqual({ cursor: { line: 1, ch: 0 }, code: `
\n

Title

\n
\n`, }); }); it('should handle multiple root level jsx elements', () => { const code = `

Title

Title Two

`; expect(formatCode({ code, cursor: { line: 0, ch: 34 } })).toEqual({ cursor: { line: 4, ch: 6 }, code: `
\n

Title

\n
\n
\n

Title Two

\n
\n`, }); }); }); describe('format and insert', () => { it('should handle inserting one line into one line', () => { const snippet = 'added'; const code = `

Title

`; expect( formatAndInsert({ code, cursor: { line: 0, ch: 9 }, snippet }) ).toEqual({ cursor: { line: 2, ch: 22 }, code: `
\n

\n addedTitle\n

\n
\n`, }); }); it('should handle inserting multiple lines into multiple lines', () => { const snippet = '\n second\n'; const code = `
\n

\n addedTitle\n

\n
\n`; expect( formatAndInsert({ code, cursor: { line: 2, ch: 15 }, snippet }) ).toEqual({ cursor: { line: 6, ch: 13 }, code: `
\n

\n \n added\n \n second\n \n \n Title\n

\n
\n`, }); }); it('should handle inserting at the start', () => { const snippet = '\n second\n'; const code = `
\n

\n addedTitle\n

\n
\n`; expect( formatAndInsert({ code, cursor: { line: 0, ch: 0 }, snippet }) ).toEqual({ cursor: { line: 2, ch: 7 }, code: `\n second\n\n
\n

\n addedTitle\n

\n
\n`, }); }); it('should handle inserting at the end', () => { const snippet = '\n second\n'; const code = `
\n

\n addedTitle\n

\n
\n`; expect( formatAndInsert({ code, cursor: { line: 5, ch: 0 }, snippet }) ).toEqual({ cursor: { line: 7, ch: 7 }, code: `
\n

\n addedTitle\n

\n
\n\n second\n\n`, }); }); it('should handle inserting at the end after multiple new lines', () => { const snippet = '\n second\n\n'; const code = `
\n

\n addedTitle\n

\n
\n\n\n\n`; expect( formatAndInsert({ code, cursor: { line: 8, ch: 0 }, snippet }) ).toEqual({ cursor: { line: 9, ch: 0 }, code: `
\n

\n addedTitle\n

\n
\n\n\n second\n\n`, }); }); });