is-cursor-on-empty-line.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { expect } from 'chai'
  2. import { EditorState, EditorSelection } from '@codemirror/state'
  3. import { isCursorOnEmptyLine } from '../../../../../frontend/js/features/source-editor/utils/is-cursor-on-empty-line'
  4. function cursorState(doc: string, cursorPos: number) {
  5. return EditorState.create({
  6. doc,
  7. selection: EditorSelection.cursor(cursorPos),
  8. })
  9. }
  10. function selectionState(doc: string, from: number, to: number) {
  11. return EditorState.create({
  12. doc,
  13. selection: EditorSelection.range(from, to),
  14. })
  15. }
  16. describe('isCursorOnEmptyLine', function () {
  17. it('returns true when cursor is on an empty line', function () {
  18. const state = cursorState('hello\n\nworld', 6)
  19. expect(isCursorOnEmptyLine(state)).to.be.true
  20. })
  21. it('returns true when cursor is on a whitespace-only line', function () {
  22. const state = cursorState('hello\n \nworld', 8)
  23. expect(isCursorOnEmptyLine(state)).to.be.true
  24. })
  25. it('returns false when cursor is on a line with content', function () {
  26. const state = cursorState('hello world', 3)
  27. expect(isCursorOnEmptyLine(state)).to.be.false
  28. })
  29. it('returns false when there is an active selection', function () {
  30. const state = selectionState('hello\n\nworld', 0, 5)
  31. expect(isCursorOnEmptyLine(state)).to.be.false
  32. })
  33. it('returns false when selection spans an empty line', function () {
  34. const state = selectionState('hello\n\nworld', 4, 8)
  35. expect(isCursorOnEmptyLine(state)).to.be.false
  36. })
  37. })