line-wrapping-indentation.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { expect } from 'chai'
  2. import { EditorView, DecorationSet } from '@codemirror/view'
  3. import { EditorState } from '@codemirror/state'
  4. import { buildDecorations } from '../../../../../frontend/js/features/source-editor/extensions/line-wrapping-indentation'
  5. const basicDoc = `
  6. \\begin{document}
  7. Test
  8. \\end{document}
  9. `
  10. const docLongLineNoIndentation = `
  11. \\begin{document}
  12. Test
  13. Hello one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four
  14. \\end{document}
  15. `
  16. const docLongLineWithIndentation = `
  17. \\begin{document}
  18. Test
  19. Hello one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four
  20. \\end{document}
  21. `
  22. const docLongLineWithLotsOfIndentation = `
  23. \\begin{document}
  24. Test
  25. Hello one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four
  26. Hello one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four
  27. Hello one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four
  28. Hello
  29. Hello one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four one two three four
  30. Hello
  31. \\end{document}
  32. `
  33. describe('line-wrapping-indentation', function () {
  34. describe('buildDecorations', function () {
  35. const _buildView = (doc: string) => {
  36. return new EditorView({
  37. state: EditorState.create({
  38. doc,
  39. }),
  40. })
  41. }
  42. const _toArray = (decorations: DecorationSet) => {
  43. const result = []
  44. const cursor = decorations.iter()
  45. while (cursor.value) {
  46. result.push({ from: cursor.from, to: cursor.to, value: cursor.value })
  47. cursor.next()
  48. }
  49. return result
  50. }
  51. describe('basic document', function () {
  52. it('should have no decorations', function () {
  53. const view = _buildView(basicDoc)
  54. const decorations = buildDecorations(view, 24)
  55. expect(decorations).to.exist
  56. expect(decorations.size).to.equal(0)
  57. })
  58. })
  59. describe('document with long lines, no indentation', function () {
  60. it('should have no decorations', function () {
  61. const view = _buildView(docLongLineNoIndentation)
  62. const decorations = buildDecorations(view, 24)
  63. expect(decorations).to.exist
  64. expect(decorations.size).to.equal(0)
  65. })
  66. })
  67. describe('document with long lines, with indentation', function () {
  68. it('should have a decoration', function () {
  69. const view = _buildView(docLongLineWithIndentation)
  70. const decorations = buildDecorations(view, 24)
  71. expect(decorations).to.exist
  72. expect(decorations.size).to.equal(1)
  73. const decorationItem = _toArray(decorations)[0]
  74. expect(decorationItem.from).to.equal(23)
  75. expect(decorationItem.to).to.equal(23)
  76. })
  77. })
  78. describe('document with long lines, with lots of indentation', function () {
  79. it('should have a decoration', function () {
  80. const view = _buildView(docLongLineWithLotsOfIndentation)
  81. const decorations = buildDecorations(view, 24)
  82. expect(decorations).to.exist
  83. expect(decorations.size).to.equal(4)
  84. const decorationsArray = _toArray(decorations)
  85. const expectedPositions = [23, 265, 507, 758]
  86. decorationsArray.forEach((item, index) => {
  87. expect(item.from).to.equal(expectedPositions[index])
  88. expect(item.to).to.equal(expectedPositions[index])
  89. })
  90. })
  91. })
  92. })
  93. })