|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js"; |
| 3 | +import type { Block } from "../../defaultBlocks.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for CheckListItem block behaviour, especially that the checkbox |
| 7 | + * respects the editor's editable state (disabled when not editable, |
| 8 | + * and change handler no-ops when not editable). |
| 9 | + * |
| 10 | + * @vitest-environment jsdom |
| 11 | + */ |
| 12 | +function getCheckboxFromView(view: { |
| 13 | + dom: HTMLElement | DocumentFragment; |
| 14 | +}): HTMLInputElement { |
| 15 | + const el = view.dom.querySelector('input[type="checkbox"]'); |
| 16 | + if (!(el instanceof HTMLInputElement)) { |
| 17 | + throw new Error("Checkbox input not found in rendered output"); |
| 18 | + } |
| 19 | + return el; |
| 20 | +} |
| 21 | + |
| 22 | +it("renders checkbox as enabled when editor is editable", () => { |
| 23 | + const editor = BlockNoteEditor.create(); |
| 24 | + const block: Block = { |
| 25 | + id: "1", |
| 26 | + type: "checkListItem", |
| 27 | + props: { |
| 28 | + backgroundColor: "default", |
| 29 | + textAlignment: "left", |
| 30 | + textColor: "default", |
| 31 | + checked: false, |
| 32 | + }, |
| 33 | + content: [], |
| 34 | + children: [], |
| 35 | + }; |
| 36 | + const spec = editor.schema.blockSpecs.checkListItem; |
| 37 | + const view = spec.implementation.render(block, editor); |
| 38 | + const checkbox = getCheckboxFromView(view); |
| 39 | + expect(checkbox.disabled).toBe(false); |
| 40 | +}); |
| 41 | + |
| 42 | +it("renders checkbox as disabled when editor is not editable", () => { |
| 43 | + const editor = BlockNoteEditor.create(); |
| 44 | + editor.isEditable = false; |
| 45 | + const block: Block = { |
| 46 | + id: "1", |
| 47 | + type: "checkListItem", |
| 48 | + props: { |
| 49 | + backgroundColor: "default", |
| 50 | + textAlignment: "left", |
| 51 | + textColor: "default", |
| 52 | + checked: false, |
| 53 | + }, |
| 54 | + content: [], |
| 55 | + children: [], |
| 56 | + }; |
| 57 | + const spec = editor.schema.blockSpecs.checkListItem; |
| 58 | + const view = spec.implementation.render(block, editor); |
| 59 | + const checkbox = getCheckboxFromView(view); |
| 60 | + expect(checkbox.disabled).toBe(true); |
| 61 | +}); |
0 commit comments