|
| 1 | +import { afterEach, describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { getBlockElement, getDirectChildBlocks } from "./blockDOM.js"; |
| 4 | + |
| 5 | +let root: HTMLElement | undefined; |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + root?.remove(); |
| 9 | + root = undefined; |
| 10 | +}); |
| 11 | + |
| 12 | +/** Attaches the markup to the document and returns a lookup by `data-id`. */ |
| 13 | +function mount(html: string) { |
| 14 | + root = document.createElement("div"); |
| 15 | + root.innerHTML = html; |
| 16 | + document.body.append(root); |
| 17 | + return (id: string) => |
| 18 | + root!.querySelector( |
| 19 | + `[data-id="${id}"]:not([data-node-type="blockOuter"])`, |
| 20 | + )!; |
| 21 | +} |
| 22 | + |
| 23 | +// A column list with one block per column, the way BlockNote renders it: each |
| 24 | +// regular block is a `blockContainer` wrapped in `blockOuter` chrome. |
| 25 | +const columns = ` |
| 26 | + <div class="bn-editor"> |
| 27 | + <div data-node-type="columnList" data-id="list"> |
| 28 | + <div data-node-type="column" data-id="col-a"> |
| 29 | + <div data-node-type="blockOuter" data-id="a"> |
| 30 | + <div data-node-type="blockContainer" data-id="a"><span>A</span></div> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + <div data-node-type="column" data-id="col-b"> |
| 34 | + <div data-node-type="blockOuter" data-id="b"> |
| 35 | + <div data-node-type="blockContainer" data-id="b">B</div> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + </div> |
| 39 | + </div>`; |
| 40 | + |
| 41 | +// A block that embeds another editor, with a block of its own. |
| 42 | +const nestedEditor = ` |
| 43 | + <div class="bn-editor"> |
| 44 | + <div data-node-type="blockOuter" data-id="outer"> |
| 45 | + <div data-node-type="blockContainer" data-id="outer"> |
| 46 | + <div class="bn-editor" data-test="inner"> |
| 47 | + <div data-node-type="blockOuter" data-id="inner"> |
| 48 | + <div data-node-type="blockContainer" data-id="inner">inner</div> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + </div>`; |
| 54 | + |
| 55 | +describe("getDirectChildBlocks", () => { |
| 56 | + it("returns direct child blocks, skipping nested grandchildren", () => { |
| 57 | + const byId = mount(columns); |
| 58 | + // The blocks inside each column must not come back as the list's own |
| 59 | + // children. |
| 60 | + expect(getDirectChildBlocks(byId("list"))).toEqual([ |
| 61 | + byId("col-a"), |
| 62 | + byId("col-b"), |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("sees through blockOuter wrappers to the blockContainer child", () => { |
| 67 | + const byId = mount(columns); |
| 68 | + expect(getDirectChildBlocks(byId("col-a"))).toEqual([byId("a")]); |
| 69 | + }); |
| 70 | + |
| 71 | + it("skips the blocks of an editor nested in the block", () => { |
| 72 | + const byId = mount(nestedEditor); |
| 73 | + expect(getDirectChildBlocks(byId("outer"))).toEqual([]); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("getBlockElement", () => { |
| 78 | + it("returns the block an element is in", () => { |
| 79 | + const byId = mount(columns); |
| 80 | + const editor = root!.querySelector<HTMLElement>(".bn-editor")!; |
| 81 | + const text = byId("a").querySelector("span")!; |
| 82 | + expect(getBlockElement(text, { dom: editor })).toBe(byId("a")); |
| 83 | + expect(getBlockElement(byId("col-a"), { dom: editor })).toBe(byId("col-a")); |
| 84 | + }); |
| 85 | + |
| 86 | + it("returns nothing for the editor element itself", () => { |
| 87 | + mount(columns); |
| 88 | + const editor = root!.querySelector<HTMLElement>(".bn-editor")!; |
| 89 | + expect(getBlockElement(editor, { dom: editor })).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("leaves the blocks of a nested editor to that editor", () => { |
| 93 | + const byId = mount(nestedEditor); |
| 94 | + const outer = root!.querySelector<HTMLElement>(".bn-editor")!; |
| 95 | + const inner = root!.querySelector<HTMLElement>('[data-test="inner"]')!; |
| 96 | + expect(getBlockElement(byId("inner"), { dom: outer })).toBeUndefined(); |
| 97 | + expect(getBlockElement(byId("inner"), { dom: inner })).toBe(byId("inner")); |
| 98 | + // The inner editor's own padding is in the outer block, which belongs to |
| 99 | + // the outer editor. |
| 100 | + expect(getBlockElement(inner, { dom: inner })).toBeUndefined(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments