Skip to content

Commit 5bea1a6

Browse files
committed
unit tests for $splitAtPointCaretNext
1 parent 3e60e64 commit 5bea1a6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

packages/lexical/src/caret/__tests__/unit/LexicalCaret.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
$getSelection,
3232
$getSiblingCaret,
3333
$getTextPointCaret,
34+
$isParagraphNode,
3435
$isSiblingCaret,
3536
$isTextNode,
3637
$isTextPointCaret,
@@ -41,6 +42,7 @@ import {
4142
$setPointFromCaret,
4243
$setSelection,
4344
$setSelectionFromCaretRange,
45+
$splitAtPointCaretNext,
4446
ChildCaret,
4547
ElementNode,
4648
LexicalNode,
@@ -1947,3 +1949,65 @@ describe('LexicalSelectionHelpers', () => {
19471949
});
19481950
});
19491951
});
1952+
1953+
describe('$splitAtPointCaretNext', () => {
1954+
initializeUnitTest((testEnv) => {
1955+
test('Does not split a TextNode at the beginning', () => {
1956+
testEnv.editor.update(
1957+
() => {
1958+
const textNode = $createTextNode('test');
1959+
const paragraphNode = $createParagraphNode();
1960+
$getRoot().clear().append(paragraphNode.append(textNode));
1961+
const caret = $getTextPointCaret(textNode, 'next', 0);
1962+
const after = $splitAtPointCaretNext(caret);
1963+
expect(textNode.getTextContent()).toEqual('test');
1964+
expect(
1965+
$getChildCaret(paragraphNode, 'next').isSamePointCaret(after),
1966+
).toBe(true);
1967+
},
1968+
{discrete: true},
1969+
);
1970+
});
1971+
test('Splits a TextNode in the middle', () => {
1972+
testEnv.editor.update(
1973+
() => {
1974+
const textNode = $createTextNode('test');
1975+
const paragraphNode = $createParagraphNode();
1976+
$getRoot().clear().append(paragraphNode.append(textNode));
1977+
const caret = $getTextPointCaret(textNode, 'next', 2);
1978+
const after = $splitAtPointCaretNext(caret);
1979+
expect(textNode.getTextContent()).toEqual('te');
1980+
const nextCaret = $getSiblingCaret(textNode, 'next');
1981+
expect(nextCaret.isSamePointCaret(after)).toBe(true);
1982+
const splitNode = nextCaret.getNodeAtCaret();
1983+
expect(
1984+
$isTextNode(splitNode) ? splitNode.getTextContent() : null,
1985+
).toEqual('st');
1986+
},
1987+
{discrete: true},
1988+
);
1989+
});
1990+
test('Splits a ParagraphNode', () => {
1991+
testEnv.editor.update(
1992+
() => {
1993+
const beforeTextNode = $createTextNode('before');
1994+
const afterTextNode = $createTextNode('after');
1995+
const paragraphNode = $createParagraphNode();
1996+
$getRoot()
1997+
.clear()
1998+
.append(paragraphNode.append(beforeTextNode, afterTextNode));
1999+
const caret = $getSiblingCaret(beforeTextNode, 'next');
2000+
const after = $splitAtPointCaretNext(caret);
2001+
expect(paragraphNode.getAllTextNodes()).toEqual([beforeTextNode]);
2002+
const nextCaret = $getSiblingCaret(paragraphNode, 'next');
2003+
expect(nextCaret.isSamePointCaret(after)).toBe(true);
2004+
const splitNode = nextCaret.getNodeAtCaret();
2005+
expect(
2006+
$isParagraphNode(splitNode) ? splitNode.getAllTextNodes() : null,
2007+
).toEqual([afterTextNode]);
2008+
},
2009+
{discrete: true},
2010+
);
2011+
});
2012+
});
2013+
});

0 commit comments

Comments
 (0)