|
| 1 | +import { Fragment, Schema } from "prosemirror-model"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { getFirstChar } from "./fragmentUtil.js"; |
| 4 | + |
| 5 | +describe("fragmentUtil", () => { |
| 6 | + describe("getFirstChar", () => { |
| 7 | + // Create a more complex schema for testing |
| 8 | + const schema = new Schema({ |
| 9 | + nodes: { |
| 10 | + doc: { content: "block+" }, |
| 11 | + paragraph: { content: "inline*", group: "block" }, |
| 12 | + heading: { |
| 13 | + content: "inline*", |
| 14 | + group: "block", |
| 15 | + attrs: { level: { default: 1 } }, |
| 16 | + }, |
| 17 | + blockcontainer: { content: "block+", group: "block" }, |
| 18 | + text: { group: "inline" }, |
| 19 | + }, |
| 20 | + }); |
| 21 | + |
| 22 | + it("should return 0 for a text fragment", () => { |
| 23 | + const fragment = Fragment.from(schema.text("Hello")); |
| 24 | + expect(getFirstChar(fragment)).toBe(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should return correct index for a paragraph with text", () => { |
| 28 | + const paragraph = schema.node("paragraph", null, [schema.text("Hello")]); |
| 29 | + const fragment = Fragment.from(paragraph); |
| 30 | + expect(getFirstChar(fragment)).toBe(1); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should return undefined for an empty fragment", () => { |
| 34 | + const fragment = Fragment.empty; |
| 35 | + expect(getFirstChar(fragment)).toBe(undefined); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should handle nested nodes correctly", () => { |
| 39 | + const paragraph = schema.node("paragraph", null, [ |
| 40 | + schema.text("Hello"), |
| 41 | + schema.text(" World"), |
| 42 | + ]); |
| 43 | + const fragment = Fragment.from(paragraph); |
| 44 | + expect(getFirstChar(fragment)).toBe(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should handle blockcontainer with nested paragraph", () => { |
| 48 | + const paragraph = schema.node("paragraph", null, [schema.text("Hello")]); |
| 49 | + const blockcontainer = schema.node("blockcontainer", null, [paragraph]); |
| 50 | + const fragment = Fragment.from(blockcontainer); |
| 51 | + // Blockquote opening (1) + paragraph opening (1) = 2 |
| 52 | + expect(getFirstChar(fragment)).toBe(2); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should handle heading with text", () => { |
| 56 | + const heading = schema.node("heading", { level: 2 }, [ |
| 57 | + schema.text("Title"), |
| 58 | + ]); |
| 59 | + const fragment = Fragment.from(heading); |
| 60 | + expect(getFirstChar(fragment)).toBe(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should handle multiple block nodes", () => { |
| 64 | + const paragraph1 = schema.node("paragraph", null, [schema.text("First")]); |
| 65 | + const paragraph2 = schema.node("paragraph", null, [ |
| 66 | + schema.text("Second"), |
| 67 | + ]); |
| 68 | + const fragment = Fragment.from([paragraph1, paragraph2]); |
| 69 | + expect(getFirstChar(fragment)).toBe(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should handle deeply nested structure", () => { |
| 73 | + const text = schema.text("Deep text"); |
| 74 | + const paragraph = schema.node("paragraph", null, [text]); |
| 75 | + const blockcontainer = schema.node("blockcontainer", null, [paragraph]); |
| 76 | + const blockcontainer2 = schema.node("blockcontainer", null, [ |
| 77 | + blockcontainer, |
| 78 | + ]); |
| 79 | + |
| 80 | + const fragment = Fragment.from(blockcontainer2); |
| 81 | + // blockcontainer + blockcontainer + paragraph (1) = 3 |
| 82 | + expect(getFirstChar(fragment)).toBe(3); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments