File
Blob: tests/client/components/editor/controllers/formatting-toolbar-state.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, TextSelection } from "@tiptap/pm/state"; |
| 5 | import { shouldShowFormattingToolbar } from "@/client/components/editor/controllers/formatting-toolbar-state"; |
| 6 | import { DetailsBlockExtensions } from "@/client/components/editor/extensions/details-block"; |
| 7 | import { createDetailsNode } from "@tests/client/util/editor-fixtures"; |
| 8 | import { createFormattingToolbarEditor } from "@tests/client/util/editor-mocks"; |
| 9 | |
| 10 | const schema = getSchema([StarterKit.configure({ undoRedo: false }), ...DetailsBlockExtensions]); |
| 11 | |
| 12 | describe("formatting toolbar visibility", () => { |
| 13 | it("stays hidden when the selection is inside a details summary", () => { |
| 14 | const summary = "Specs"; |
| 15 | const doc = schema.nodeFromJSON({ |
| 16 | type: "doc", |
| 17 | content: [createDetailsNode({ summary })], |
| 18 | }); |
| 19 | const from = 2; |
| 20 | const to = from + summary.length; |
| 21 | const state = EditorState.create({ |
| 22 | schema, |
| 23 | doc, |
| 24 | selection: TextSelection.create(doc, from, to), |
| 25 | }); |
| 26 | |
| 27 | expect(shouldShowFormattingToolbar({ editor: createFormattingToolbarEditor(state), from, to })).toBe(false); |
| 28 | }); |
| 29 | |
| 30 | it("still shows for selections inside details content", () => { |
| 31 | const doc = schema.nodeFromJSON({ |
| 32 | type: "doc", |
| 33 | content: [createDetailsNode({ summary: "Specs", contentText: "Body copy" })], |
| 34 | }); |
| 35 | const detailsNode = doc.firstChild; |
| 36 | const summaryNode = detailsNode?.firstChild; |
| 37 | |
| 38 | expect(detailsNode).toBeTruthy(); |
| 39 | expect(summaryNode).toBeTruthy(); |
| 40 | |
| 41 | const from = TextSelection.near(doc.resolve(1 + summaryNode!.nodeSize + 1)).from; |
| 42 | const to = from + 4; |
| 43 | const state = EditorState.create({ |
| 44 | schema, |
| 45 | doc, |
| 46 | selection: TextSelection.create(doc, from, to), |
| 47 | }); |
| 48 | |
| 49 | expect(shouldShowFormattingToolbar({ editor: createFormattingToolbarEditor(state), from, to })).toBe(true); |
| 50 | }); |
| 51 | }); |