|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; |
| 3 | +import { Fragment } from "prosemirror-model"; |
| 4 | +import { findWrappingPath, wrapFragmentInDoc } from "./YSuggestions.js"; |
| 5 | + |
| 6 | +/** |
| 7 | + * @vitest-environment jsdom |
| 8 | + */ |
| 9 | + |
| 10 | +describe("findWrappingPath", () => { |
| 11 | + it("finds path [doc, blockGroup, blockContainer] for a checkListItem node", () => { |
| 12 | + const editor = BlockNoteEditor.create(); |
| 13 | + const schema = editor.pmSchema; |
| 14 | + |
| 15 | + const checkListItem = schema.nodes.checkListItem.create( |
| 16 | + { |
| 17 | + backgroundColor: "default", |
| 18 | + textColor: "default", |
| 19 | + textAlignment: "left", |
| 20 | + checked: false, |
| 21 | + }, |
| 22 | + [schema.text("ds")], |
| 23 | + ); |
| 24 | + |
| 25 | + const path = findWrappingPath(schema, checkListItem); |
| 26 | + |
| 27 | + expect(path).not.toBeNull(); |
| 28 | + expect(path!.map((t) => t.name)).toEqual([ |
| 29 | + "doc", |
| 30 | + "blockGroup", |
| 31 | + "blockContainer", |
| 32 | + ]); |
| 33 | + }); |
| 34 | + |
| 35 | + it("finds path [doc, blockGroup, blockContainer] for a paragraph node", () => { |
| 36 | + const editor = BlockNoteEditor.create(); |
| 37 | + const schema = editor.pmSchema; |
| 38 | + |
| 39 | + const paragraph = schema.nodes.paragraph.create(null, [ |
| 40 | + schema.text("hello"), |
| 41 | + ]); |
| 42 | + |
| 43 | + const path = findWrappingPath(schema, paragraph); |
| 44 | + |
| 45 | + expect(path).not.toBeNull(); |
| 46 | + expect(path!.map((t) => t.name)).toEqual([ |
| 47 | + "doc", |
| 48 | + "blockGroup", |
| 49 | + "blockContainer", |
| 50 | + ]); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("wrapFragmentInDoc", () => { |
| 55 | + it("wraps a fragment with a single checkListItem", () => { |
| 56 | + const editor = BlockNoteEditor.create(); |
| 57 | + const schema = editor.pmSchema; |
| 58 | + |
| 59 | + const checkListItem = schema.nodes.checkListItem.create( |
| 60 | + { |
| 61 | + backgroundColor: "default", |
| 62 | + textColor: "default", |
| 63 | + textAlignment: "left", |
| 64 | + checked: false, |
| 65 | + }, |
| 66 | + [schema.text("ds")], |
| 67 | + ); |
| 68 | + const fragment = Fragment.from(checkListItem); |
| 69 | + |
| 70 | + const result = wrapFragmentInDoc(fragment, schema); |
| 71 | + |
| 72 | + expect(result).not.toBeNull(); |
| 73 | + expect(result!.type.name).toBe("doc"); |
| 74 | + // Walk down to verify the content |
| 75 | + const blockGroup = result!.firstChild!; |
| 76 | + expect(blockGroup.type.name).toBe("blockGroup"); |
| 77 | + expect(blockGroup.childCount).toBe(1); |
| 78 | + const bc = blockGroup.firstChild!; |
| 79 | + expect(bc.type.name).toBe("blockContainer"); |
| 80 | + expect(bc.firstChild!.type.name).toBe("checkListItem"); |
| 81 | + expect(bc.firstChild!.textContent).toBe("ds"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("wraps a fragment with multiple block content nodes", () => { |
| 85 | + const editor = BlockNoteEditor.create(); |
| 86 | + const schema = editor.pmSchema; |
| 87 | + |
| 88 | + const item1 = schema.nodes.checkListItem.create( |
| 89 | + { |
| 90 | + backgroundColor: "default", |
| 91 | + textColor: "default", |
| 92 | + textAlignment: "left", |
| 93 | + checked: false, |
| 94 | + }, |
| 95 | + [schema.text("first")], |
| 96 | + ); |
| 97 | + const item2 = schema.nodes.paragraph.create(null, [ |
| 98 | + schema.text("second"), |
| 99 | + ]); |
| 100 | + const fragment = Fragment.from([item1, item2]); |
| 101 | + |
| 102 | + const result = wrapFragmentInDoc(fragment, schema); |
| 103 | + |
| 104 | + expect(result).not.toBeNull(); |
| 105 | + expect(result!.type.name).toBe("doc"); |
| 106 | + const blockGroup = result!.firstChild!; |
| 107 | + expect(blockGroup.type.name).toBe("blockGroup"); |
| 108 | + // Should have two blockContainers |
| 109 | + expect(blockGroup.childCount).toBe(2); |
| 110 | + expect(blockGroup.child(0).firstChild!.type.name).toBe("checkListItem"); |
| 111 | + expect(blockGroup.child(0).firstChild!.textContent).toBe("first"); |
| 112 | + expect(blockGroup.child(1).firstChild!.type.name).toBe("paragraph"); |
| 113 | + expect(blockGroup.child(1).firstChild!.textContent).toBe("second"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("returns null for an empty fragment", () => { |
| 117 | + const editor = BlockNoteEditor.create(); |
| 118 | + const result = wrapFragmentInDoc(Fragment.empty, editor.pmSchema); |
| 119 | + expect(result).toBeNull(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments