File
Blob: tests/client/components/editor/extensions/top-level-block-identity.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 } from "@tiptap/pm/state"; |
| 5 | import { |
| 6 | TopLevelBlockIdentity, |
| 7 | applyTopLevelBlockIdNormalization, |
| 8 | } from "@/client/components/editor/extensions/top-level-block-identity"; |
| 9 | import { createParagraphNode } from "@tests/client/util/editor-fixtures"; |
| 10 | |
| 11 | const schema = getSchema([StarterKit.configure({ undoRedo: false }), TopLevelBlockIdentity]); |
| 12 | |
| 13 | describe("top-level block identity", () => { |
| 14 | it("assigns compact bids to top-level blocks that are missing them", () => { |
| 15 | const doc = schema.nodeFromJSON({ |
| 16 | type: "doc", |
| 17 | content: [createParagraphNode("One"), createParagraphNode("Two")], |
| 18 | }); |
| 19 | const tr = EditorState.create({ schema, doc }).tr; |
| 20 | |
| 21 | expect(applyTopLevelBlockIdNormalization(tr)).toBe(true); |
| 22 | const json = tr.doc.toJSON() as { |
| 23 | content: Array<{ attrs?: { bid?: string | null } }>; |
| 24 | }; |
| 25 | |
| 26 | const firstBid = json.content[0]?.attrs?.bid; |
| 27 | const secondBid = json.content[1]?.attrs?.bid; |
| 28 | expect(firstBid).toMatch(/^[A-Za-z0-9_-]{6}$/); |
| 29 | expect(secondBid).toMatch(/^[A-Za-z0-9_-]{6}$/); |
| 30 | expect(firstBid).not.toBe(secondBid); |
| 31 | }); |
| 32 | |
| 33 | it("repairs duplicate top-level bids and clears nested carried bids", () => { |
| 34 | const doc = schema.nodeFromJSON({ |
| 35 | type: "doc", |
| 36 | content: [ |
| 37 | createParagraphNode("Top", "dupBid"), |
| 38 | { |
| 39 | type: "bulletList", |
| 40 | attrs: { bid: "listBid" }, |
| 41 | content: [ |
| 42 | { |
| 43 | type: "listItem", |
| 44 | content: [createParagraphNode("Nested", "nestedBid")], |
| 45 | }, |
| 46 | ], |
| 47 | }, |
| 48 | createParagraphNode("Second top", "dupBid"), |
| 49 | ], |
| 50 | }); |
| 51 | const tr = EditorState.create({ schema, doc }).tr; |
| 52 | |
| 53 | expect(applyTopLevelBlockIdNormalization(tr)).toBe(true); |
| 54 | const json = tr.doc.toJSON() as { |
| 55 | content: Array<{ |
| 56 | attrs?: { bid?: string | null }; |
| 57 | content?: Array<{ content?: Array<{ attrs?: { bid?: string | null } }> }>; |
| 58 | }>; |
| 59 | }; |
| 60 | |
| 61 | expect(json.content[0]?.attrs?.bid).toBe("dupBid"); |
| 62 | expect(json.content[1]?.attrs?.bid).toBe("listBid"); |
| 63 | expect(json.content[2]?.attrs?.bid).toMatch(/^[A-Za-z0-9_-]{6}$/); |
| 64 | expect(json.content[2]?.attrs?.bid).not.toBe("dupBid"); |
| 65 | expect(json.content[1]?.content?.[0]?.content?.[0]?.attrs?.bid).toBeNull(); |
| 66 | }); |
| 67 | }); |