test-simplify.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import ist from "ist"
  2. import {doc, p, img} from "prosemirror-test-builder"
  3. import {Node} from "prosemirror-model"
  4. import {simplifyChanges, Change, Span} from "prosemirror-changeset"
  5. describe("simplifyChanges", () => {
  6. it("doesn't change insertion-only changes", () => test(
  7. [[1, 1, 1, 2], [2, 2, 3, 4]], doc(p("hello")), [[1, 1, 1, 2], [2, 2, 3, 4]]))
  8. it("doesn't change deletion-only changes", () => test(
  9. [[1, 2, 1, 1], [3, 4, 2, 2]], doc(p("hello")), [[1, 2, 1, 1], [3, 4, 2, 2]]))
  10. it("doesn't change single-letter-replacements", () => test(
  11. [[1, 2, 1, 2]], doc(p("hello")), [[1, 2, 1, 2]]))
  12. it("does expand multiple-letter replacements", () => test(
  13. [[2, 4, 2, 4]], doc(p("hello")), [[1, 6, 1, 6]]))
  14. it("does combine changes within the same word", () => test(
  15. [[1, 3, 1, 1], [5, 5, 3, 4]], doc(p("hello")), [[1, 7, 1, 6]]))
  16. it("expands changes to cover full words", () => test(
  17. [[7, 10]], doc(p("one two three four")), [[5, 14]]))
  18. it("doesn't expand across non-word text", () => test(
  19. [[7, 10]], doc(p("one two ----- four")), [[5, 10]]))
  20. it("treats leaf nodes as non-words", () => test(
  21. [[2, 3], [6, 7]], doc(p("one", img(), "two")), [[2, 3], [6, 7]]))
  22. it("treats node boundaries as non-words", () => test(
  23. [[2, 3], [7, 8]], doc(p("one"), p("two")), [[2, 3], [7, 8]]))
  24. it("can merge stretches of changes", () => test(
  25. [[2, 3], [4, 6], [8, 10], [15, 16]], doc(p("foo bar baz bug ugh")), [[1, 12], [15, 16]]))
  26. it("handles realistic word updates", () => test(
  27. [[8, 8, 8, 11], [10, 15, 13, 17]], doc(p("chonic condition")), [[8, 15, 8, 17]]))
  28. it("works when after significant content", () => test(
  29. [[63, 80, 63, 83]], doc(p("one long paragraph -----"), p("two long paragraphs ------"), p("a vote against the government")),
  30. [[62, 81, 62, 84]]))
  31. it("joins changes that grow together when simplifying", () => test(
  32. [[1, 5, 1, 5], [7, 13, 7, 9], [20, 21, 16, 16]], doc(p('and his co-star')),
  33. [[1, 13, 1, 9], [20, 21, 16, 16]]))
  34. it("properly fills in metadata", () => {
  35. let simple = simplifyChanges([range([2, 3], 0), range([4, 6], 1), range([8, 9, 8, 8], 2)],
  36. doc(p("1234567890")))
  37. ist(simple.length, 1)
  38. ist(JSON.stringify(simple[0].deleted.map(s => [s.length, s.data])),
  39. JSON.stringify([[3, 0], [4, 1], [4, 2]]))
  40. ist(JSON.stringify(simple[0].inserted.map(s => [s.length, s.data])),
  41. JSON.stringify([[3, 0], [4, 1], [3, 2]]))
  42. })
  43. })
  44. function range(array: number[], author = 0) {
  45. let [fromA, toA] = array
  46. let [fromB, toB] = array.length > 2 ? array.slice(2) : array
  47. return new Change(fromA, toA, fromB, toB, [new Span(toA - fromA, author)], [new Span(toB - fromB, author)])
  48. }
  49. function test(changes: number[][], doc: Node, result: number[][]) {
  50. let ranges = changes.map(range)
  51. ist(JSON.stringify(simplifyChanges(ranges, doc).map((r, i) => {
  52. if (result[i] && result[i].length > 2) return [r.fromA, r.toA, r.fromB, r.toB]
  53. else return [r.fromB, r.toB]
  54. })), JSON.stringify(result))
  55. }