Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-link] Test: Removing link from node(children) #6817

Merged
merged 10 commits into from
Nov 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
LinkNode,
SerializedLinkNode,
} from '@lexical/link';
import {MarkNode} from '@lexical/mark';
import {
$getRoot,
$selectAll,
Expand Down Expand Up @@ -409,5 +410,81 @@ describe('LexicalLinkNode tests', () => {
const link = paragraph.children[0] as SerializedLinkNode;
expect(link.title).toBe('Lexical Website');
});

test('$toggleLink correctly removes link when textnode has children(like marknode)', async () => {
const {editor} = testEnv;
await editor.update(() => {
const paragraph = new ParagraphNode();
const precedingText = new TextNode('some '); // space after
const textNode = new TextNode('text');

paragraph.append(precedingText, textNode);

const linkNode = $createLinkNode('https://example.com/foo', {
rel: 'noreferrer',
});
textNode.insertAfter(linkNode);
linkNode.append(textNode);

const markNode = new MarkNode(['knetk']);
vantage-ola marked this conversation as resolved.
Show resolved Hide resolved
textNode.insertBefore(markNode);
markNode.append(textNode);
$getRoot().append(paragraph);
});
// Verify structure after all steps
expect(editor.getEditorState().toJSON().root.children[0]).toMatchObject({
children: [
{
text: 'some ',
type: 'text',
},
{
children: [
{
children: [
{
text: 'text',
type: 'text',
},
],
ids: ['knetk'],
type: 'mark',
},
],
rel: 'noreferrer',
type: 'link',
url: 'https://example.com/foo',
},
],
type: 'paragraph',
});

// 4. Remove the link
await editor.update(() => {
$selectAll();
$toggleLink(null);
});

// Verify link is removed but mark remains
expect(editor.getEditorState().toJSON().root.children[0]).toMatchObject({
children: [
{
text: 'some ',
type: 'text',
},
{
children: [
{
text: 'text',
type: 'text',
},
],
ids: ['knetk'],
type: 'mark',
},
],
type: 'paragraph',
});
});
vantage-ola marked this conversation as resolved.
Show resolved Hide resolved
});
});
2 changes: 2 additions & 0 deletions packages/lexical/src/__tests__/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {HashtagNode} from '@lexical/hashtag';
import {createHeadlessEditor} from '@lexical/headless';
import {AutoLinkNode, LinkNode} from '@lexical/link';
import {ListItemNode, ListNode} from '@lexical/list';
import {MarkNode} from '@lexical/mark';
import {OverflowNode} from '@lexical/overflow';
import {
InitialConfigType,
Expand Down Expand Up @@ -486,6 +487,7 @@ const DEFAULT_NODES: NonNullable<InitialConfigType['nodes']> = [
TestInlineElementNode,
TestShadowRootNode,
TestTextNode,
MarkNode,
];

export function TestComposer({
Expand Down
Loading