import "jest"; import * as operations from "../operations"; import { EditorState } from "prosemirror-state"; import { compose } from "../../compose"; import "../../__specs__/expect.toMatchEditorState"; import { buildEditorState } from "../../__specs__/buildEditorState"; const { at } = compose; describe(operations.insertAttachment.name, () => { const atToInsert = at({ id: "new" }).node; // Some selection bugs were originally missed by not having realistic // documents in the test cases, specifically having nodes *after* the // insertion point. This causes ProseMirror to choose the inserted node by // default, when in real world documents with other content, some other node // would be selected. const atExisting = at({ id: "old" }).node; function test(initial: EditorState, expected: EditorState) { const tr = initial.tr; operations.insertAttachment(tr, tr.selection.$anchor.pos, atToInsert); expect(initial.apply(tr)).toMatchEditorState(expected); } it("empty paragraph", () => { test( buildEditorState(({ doc, p }) => doc(p("{^}"), atExisting)), buildEditorState(({ doc }) => doc("{node}", atToInsert, atExisting)) ); }); it("start of non-empty paragraph", () => { test( buildEditorState(({ doc, p }) => doc(p("{^}hello"), atExisting)), buildEditorState(({ doc, p }) => doc("{node}", atToInsert, p("hello"), atExisting)) ); }); it("end of non-empty paragraph", () => { test( buildEditorState(({ doc, p }) => doc(p("hello{^}"), atExisting)), buildEditorState(({ doc, p }) => doc(p("hello"), "{node}", atToInsert, atExisting)) ); }); it("text before and after in paragraph", () => { test( buildEditorState(({ doc, p }) => doc(p("hello{^}world"), atExisting)), buildEditorState(({ doc, p }) => doc(p("hello"), "{node}", atToInsert, p("world"), atExisting)) ); }); it("in list item", () => { test( buildEditorState(({ doc, ul, li, p }) => doc(ul(li(p("hello{^}world"))), atExisting)), buildEditorState(({ doc, ul, li, p }) => doc(ul(li(p("hello"))), "{node}", atToInsert, ul(li(p("world"))), atExisting)) ); }); it("wedge selection", () => { test( buildEditorState(({ doc, p }) => doc(p("hello"), "{wedge}", atExisting)), buildEditorState(({ doc, p }) => doc(p("hello"), "{node}", atToInsert, atExisting)) ); }); });