File
Blob: tests/client/components/editor/lib/block-drag-state.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { getSchema } from "@tiptap/core"; |
| 3 | import { StarterKit } from "@tiptap/starter-kit"; |
| 4 | import { EditorState, NodeSelection, type Transaction } from "@tiptap/pm/state"; |
| 5 | import { primeTopLevelBlockDragState } from "@/client/components/editor/lib/block-drag-state"; |
| 6 | import { createHeadingNode, createParagraphNode } from "@tests/client/util/editor-fixtures"; |
| 7 | |
| 8 | const schema = getSchema([StarterKit.configure({ undoRedo: false })]); |
| 9 | |
| 10 | describe("block drag state priming", () => { |
| 11 | it("primes an empty top-level paragraph for internal drag/drop", () => { |
| 12 | const doc = schema.nodeFromJSON({ |
| 13 | type: "doc", |
| 14 | content: [ |
| 15 | createHeadingNode("Sup Bro"), |
| 16 | createParagraphNode("Test"), |
| 17 | { type: "paragraph" }, |
| 18 | createParagraphNode("Mentions"), |
| 19 | ], |
| 20 | }); |
| 21 | const state = EditorState.create({ schema, doc }); |
| 22 | const emptyPos = doc.child(0)!.nodeSize + doc.child(1)!.nodeSize; |
| 23 | const dispatches: EditorState[] = []; |
| 24 | const view: Parameters<typeof primeTopLevelBlockDragState>[0] = { |
| 25 | state, |
| 26 | dragging: null, |
| 27 | dispatch(tr: Transaction) { |
| 28 | this.state = this.state.apply(tr); |
| 29 | dispatches.push(this.state); |
| 30 | }, |
| 31 | }; |
| 32 | |
| 33 | expect(primeTopLevelBlockDragState(view, emptyPos)).toBe(true); |
| 34 | expect(dispatches).toHaveLength(1); |
| 35 | expect(view.dragging?.move).toBe(true); |
| 36 | expect(view.dragging?.slice.content.toJSON()).toEqual([{ type: "paragraph" }]); |
| 37 | expect(view.state.selection).toBeInstanceOf(NodeSelection); |
| 38 | expect((view.state.selection as NodeSelection).from).toBe(emptyPos); |
| 39 | }); |
| 40 | |
| 41 | it("ignores invalid block positions", () => { |
| 42 | const doc = schema.nodeFromJSON({ |
| 43 | type: "doc", |
| 44 | content: [createParagraphNode("Test")], |
| 45 | }); |
| 46 | const state = EditorState.create({ schema, doc }); |
| 47 | const view: Parameters<typeof primeTopLevelBlockDragState>[0] = { |
| 48 | state, |
| 49 | dragging: null, |
| 50 | dispatch() { |
| 51 | throw new Error("should not dispatch"); |
| 52 | }, |
| 53 | }; |
| 54 | |
| 55 | expect(primeTopLevelBlockDragState(view, 999)).toBe(false); |
| 56 | expect(view.dragging).toBeNull(); |
| 57 | }); |
| 58 | }); |