|
| 1 | + |
| 2 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 3 | +import { EditorState } from "prosemirror-state"; |
| 4 | +import { Fragment, Slice } from "prosemirror-model"; |
| 5 | +import { type ReplaceStep, replaceStep } from "prosemirror-transform"; |
| 6 | +import { assert, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +import { type TaggedNode, testBuilders } from "../testing/testBuilders.js"; |
| 9 | +import { suggestChanges } from "../plugin.js"; |
| 10 | +import { transformToSuggestionTransaction } from "../withSuggestChanges.js"; |
| 11 | + |
| 12 | +describe("withSuggestChanges", () => { |
| 13 | + it("should wrap an insertion in a mark", () => { |
| 14 | + // Create document with all tags at once |
| 15 | + const doc = testBuilders.doc( |
| 16 | + testBuilders.paragraph({ id: null }, "This is a <a>test<b> paragraph."), |
| 17 | + testBuilders.paragraph( |
| 18 | + { id: null }, |
| 19 | + "This is a <c>test<d> <e>test<f> paragraph.", |
| 20 | + ), |
| 21 | + ) as TaggedNode; |
| 22 | + |
| 23 | + const editorState = EditorState.create({ |
| 24 | + doc, |
| 25 | + plugins: [suggestChanges()], |
| 26 | + }); |
| 27 | + |
| 28 | + const originalTransaction = editorState.tr; |
| 29 | + |
| 30 | + // Step 1: Replace "test" with "WORK" in first paragraph |
| 31 | + const step1 = replaceStep( |
| 32 | + doc, |
| 33 | + doc.tag["a"]!, |
| 34 | + doc.tag["b"], |
| 35 | + new Slice(Fragment.from(testBuilders.schema.text("WORK")), 0, 0), |
| 36 | + ) as ReplaceStep | null; |
| 37 | + assert(step1, "Could not create step1"); |
| 38 | + originalTransaction.step(step1); |
| 39 | + |
| 40 | + // Step 2: Replace first "test" with "WORK" in second paragraph |
| 41 | + // Use the updated document from the transaction |
| 42 | + const step2 = replaceStep( |
| 43 | + originalTransaction.doc, |
| 44 | + doc.tag["c"]!, |
| 45 | + doc.tag["d"], |
| 46 | + new Slice(Fragment.from(testBuilders.schema.text("WORK")), 0, 0), |
| 47 | + ) as ReplaceStep | null; |
| 48 | + assert(step2, "Could not create step2"); |
| 49 | + originalTransaction.step(step2); |
| 50 | + |
| 51 | + // Step 3: Replace second "test" with "WORK" in second paragraph |
| 52 | + // Use the updated document from the transaction |
| 53 | + const step3 = replaceStep( |
| 54 | + originalTransaction.doc, |
| 55 | + doc.tag["e"]!, |
| 56 | + doc.tag["f"], |
| 57 | + new Slice(Fragment.from(testBuilders.schema.text("WORK")), 0, 0), |
| 58 | + ) as ReplaceStep | null; |
| 59 | + assert(step3, "Could not create step3"); |
| 60 | + originalTransaction.step(step3); |
| 61 | + |
| 62 | + // Verify the transaction produces expected result |
| 63 | + const originalTrState = editorState.apply(originalTransaction); |
| 64 | + expect(originalTrState.doc.textContent).toEqual( |
| 65 | + "This is a WORK paragraph.This is a WORK WORK paragraph.", |
| 66 | + ); |
| 67 | + |
| 68 | + // Transform to suggestion transaction |
| 69 | + const suggestedTr = transformToSuggestionTransaction( |
| 70 | + originalTransaction, |
| 71 | + editorState, |
| 72 | + ); |
| 73 | + const newState = editorState.apply(suggestedTr); |
| 74 | + |
| 75 | + // Should pass without error - document should be modified |
| 76 | + expect(newState.doc.toJSON()).not.toEqual(doc.toJSON()); |
| 77 | + }); |
| 78 | +}); |
0 commit comments