|
| 1 | +import { EditorState, TextSelection } from "prosemirror-state"; |
| 2 | +import { Step } from "prosemirror-transform"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | + |
| 5 | +import { testBuilders } from "../testing/testBuilders.js"; |
| 6 | +import { suggestChanges } from "../plugin.js"; |
| 7 | +import { transformToSuggestionTransaction } from "../withSuggestChanges.js"; |
| 8 | + |
| 9 | +const steps = [ |
| 10 | + { |
| 11 | + stepType: "replace", |
| 12 | + from: 11, |
| 13 | + to: 15, |
| 14 | + slice: { |
| 15 | + content: [ |
| 16 | + { |
| 17 | + type: "text", |
| 18 | + text: "WORK", |
| 19 | + }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + }, |
| 23 | + { |
| 24 | + stepType: "replace", |
| 25 | + from: 38, |
| 26 | + to: 42, |
| 27 | + slice: { |
| 28 | + content: [ |
| 29 | + { |
| 30 | + type: "text", |
| 31 | + text: "WORK", |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + stepType: "replace", |
| 38 | + from: 43, |
| 39 | + to: 47, |
| 40 | + slice: { |
| 41 | + content: [ |
| 42 | + { |
| 43 | + type: "text", |
| 44 | + text: "WORK", |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + }, |
| 49 | +]; |
| 50 | + |
| 51 | +describe("withSuggestChanges", () => { |
| 52 | + it("should wrap an insertion in a mark", () => { |
| 53 | + // should match initialJson |
| 54 | + const doc = testBuilders.doc( |
| 55 | + testBuilders.paragraph({ id: null }, "This is a test paragraph."), |
| 56 | + testBuilders.paragraph({ id: null }, "This is a test test paragraph."), |
| 57 | + ); |
| 58 | + |
| 59 | + const end = TextSelection.atEnd(doc); |
| 60 | + const trSteps = steps.map((step) => Step.fromJSON(doc.type.schema, step)); |
| 61 | + const editorState = EditorState.create({ |
| 62 | + doc, |
| 63 | + selection: end, |
| 64 | + plugins: [suggestChanges()], |
| 65 | + }); |
| 66 | + |
| 67 | + const originalTransaction = editorState.tr; |
| 68 | + trSteps.forEach((s) => { |
| 69 | + originalTransaction.step(s); |
| 70 | + }); |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-return |
| 72 | + expect(originalTransaction.steps.map((s) => s.toJSON())).toEqual(steps); |
| 73 | + const originalTrState = editorState.apply(originalTransaction); |
| 74 | + expect(originalTrState.doc.textContent).toEqual( |
| 75 | + "This is a WORK paragraph.This is a WORK WORK paragraph.", |
| 76 | + ); |
| 77 | + const suggestedTr = transformToSuggestionTransaction( |
| 78 | + originalTransaction, |
| 79 | + editorState, |
| 80 | + ); |
| 81 | + const newState = editorState.apply(suggestedTr); |
| 82 | + // Should pass without error |
| 83 | + expect(newState.doc.toJSON()).not.toEqual(doc.toJSON()); |
| 84 | + }); |
| 85 | +}); |
0 commit comments