Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";

import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
import { setupTestEnv } from "../../setupTestEnv.js";
import { mergeBlocksCommand } from "./mergeBlocks.js";
import { getParentBlockInfo, mergeBlocksCommand } from "./mergeBlocks.js";

const getEditor = setupTestEnv();

Expand Down Expand Up @@ -77,6 +77,20 @@ describe("Test mergeBlocks", () => {
expect(anchorIsAtOldFirstBlockEndPos).toBeTruthy();
});

it("getParentBlockInfo returns undefined for top-level block", () => {
getEditor().setTextCursorPosition("paragraph-0");

const beforePos = getPosBeforeSelectedBlock();
const doc = getEditor()._tiptapEditor.state.doc;
const $pos = doc.resolve(beforePos);

expect($pos.depth - 1).toBeLessThan(1);

const result = getParentBlockInfo(doc, beforePos);

expect(result).toBeUndefined();
});

// We expect a no-op for each of the remaining tests as merging should only
// happen for blocks which both have inline content. We also expect
// `mergeBlocks` to return false as TipTap commands should do that instead of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const getParentBlockInfo = (
): BlockInfo | undefined => {
const $pos = doc.resolve(beforePos);
const depth = $pos.depth - 1;

if (depth < 1) {
return undefined;
}

const parentBeforePos = $pos.before(depth);
const parentNode = doc.resolve(parentBeforePos).nodeAfter;

Expand Down
Loading