Skip to content

Commit f18cff5

Browse files
etrepumclaude
andauthored
[lexical][*] Chore: stop using deprecated traversal type parameters in tests (#8667)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 27b2a33 commit f18cff5

21 files changed

Lines changed: 693 additions & 332 deletions

File tree

packages/lexical-code-core/src/__tests__/unit/CodeExtension.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import {
1515
$isElementNode,
1616
KEY_ENTER_COMMAND,
1717
} from 'lexical';
18+
import {$assertNodeType} from 'lexical/src/__tests__/utils';
1819
import {describe, expect, it} from 'vitest';
1920

2021
import {$createCodeHighlightNode} from '../../CodeHighlightNode';
21-
import {$isCodeNode, CodeNode} from '../../CodeNode';
22+
import {$isCodeNode} from '../../CodeNode';
2223

2324
describe('CodeExtension', () => {
2425
it('should not escape code block when content has consecutive blank lines (paste scenario)', () => {
@@ -67,7 +68,10 @@ describe('CodeExtension', () => {
6768
using editor = buildEditorFromExtensions(ext);
6869
editor.update(
6970
() => {
70-
const codeNode = $getRoot().getFirstChildOrThrow<CodeNode>();
71+
const codeNode = $assertNodeType(
72+
$getRoot().getFirstChild(),
73+
$isCodeNode,
74+
);
7175
codeNode.select(codeNode.getChildrenSize(), codeNode.getChildrenSize());
7276
},
7377
{discrete: true},
@@ -108,7 +112,10 @@ describe('CodeExtension', () => {
108112
using editor = buildEditorFromExtensions(ext);
109113
editor.update(
110114
() => {
111-
const codeNode = $getRoot().getFirstChildOrThrow<CodeNode>();
115+
const codeNode = $assertNodeType(
116+
$getRoot().getFirstChild(),
117+
$isCodeNode,
118+
);
112119
codeNode.select(codeNode.getChildrenSize(), codeNode.getChildrenSize());
113120
},
114121
{discrete: true},
@@ -148,7 +155,10 @@ describe('CodeExtension', () => {
148155
using editor = buildEditorFromExtensions(ext);
149156
editor.update(
150157
() => {
151-
const codeNode = $getRoot().getFirstChildOrThrow<CodeNode>();
158+
const codeNode = $assertNodeType(
159+
$getRoot().getFirstChild(),
160+
$isCodeNode,
161+
);
152162
codeNode.select(1, 1);
153163
},
154164
{discrete: true},

packages/lexical-list/src/__tests__/unit/LexicalListNode.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import {waitForReact} from '@lexical/react/src/__tests__/utils';
1818
import {$createTextNode, $getRoot, ParagraphNode, TextNode} from 'lexical';
1919
import {
20+
$assertNodeType,
2021
expectHtmlToBeEqual,
2122
html,
2223
initializeUnitTest,
@@ -244,9 +245,12 @@ describe('LexicalListNode tests', () => {
244245

245246
expect(listNode.append(...nodesToAppend)).toBe(listNode);
246247
expect($isListItemNode(listNode.getFirstChild())).toBe(true);
247-
expect(listNode.getFirstChild<ListItemNode>()!.getFirstChild()).toBe(
248-
nestedListNode,
249-
);
248+
expect(
249+
$assertNodeType(
250+
listNode.getFirstChild(),
251+
$isListItemNode,
252+
).getFirstChild(),
253+
).toBe(nestedListNode);
250254
});
251255
});
252256

@@ -354,7 +358,10 @@ describe('LexicalListNode tests', () => {
354358

355359
await waitForReact(() => {
356360
editor.update(() => {
357-
const listNode = $getRoot().getFirstChildOrThrow<ListNode>();
361+
const listNode = $assertNodeType(
362+
$getRoot().getFirstChild(),
363+
$isListNode,
364+
);
358365
listNode.setListType('bullet');
359366
});
360367
});
@@ -395,8 +402,14 @@ describe('LexicalListNode tests', () => {
395402

396403
await waitForReact(() => {
397404
editor.update(() => {
398-
const listNode = $getRoot().getFirstChildOrThrow<ListNode>();
399-
const listItemNode = listNode.getChildAtIndex<ListItemNode>(1)!;
405+
const listNode = $assertNodeType(
406+
$getRoot().getFirstChild(),
407+
$isListNode,
408+
);
409+
const listItemNode = $assertNodeType(
410+
listNode.getChildAtIndex(1),
411+
$isListItemNode,
412+
);
400413
listItemNode.append(
401414
$createListNode('bullet').append($createListItemNode()),
402415
);

packages/lexical-list/src/__tests__/unit/formatList.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import {
2121
$createTableCellNode,
2222
$createTableNode,
2323
$createTableRowNode,
24-
TableCellNode,
25-
TableNode,
26-
TableRowNode,
24+
$isTableCellNode,
25+
$isTableNode,
26+
$isTableRowNode,
2727
} from '@lexical/table';
2828
import {
2929
$createLineBreakNode,
@@ -40,6 +40,7 @@ import {
4040
LexicalNode,
4141
} from 'lexical';
4242
import {
43+
$assertNodeType,
4344
$createTestDecoratorNode,
4445
initializeUnitTest,
4546
} from 'lexical/src/__tests__/utils';
@@ -144,10 +145,9 @@ describe('insertList', () => {
144145
});
145146

146147
editor.read(() => {
147-
const cell = $getRoot()
148-
.getFirstChildOrThrow<TableNode>()
149-
.getFirstChildOrThrow<TableRowNode>()
150-
.getFirstChildOrThrow<TableCellNode>();
148+
const table = $assertNodeType($getRoot().getFirstChild(), $isTableNode);
149+
const row = $assertNodeType(table.getFirstChild(), $isTableRowNode);
150+
const cell = $assertNodeType(row.getFirstChild(), $isTableCellNode);
151151

152152
expect(cell.getChildrenSize()).toBe(1);
153153

packages/lexical-mark/__tests__/unit/LexicalMarkNode.test.ts

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
*
77
*/
88
import {$generateNodesFromDOM} from '@lexical/html';
9-
import {$isMarkNode, $wrapSelectionInMarkNode, MarkNode} from '@lexical/mark';
9+
import {$isMarkNode, $wrapSelectionInMarkNode} from '@lexical/mark';
1010
import {$insertNodeIntoLeaf} from '@lexical/utils';
1111
import {
1212
$createParagraphNode,
1313
$createRangeSelection,
1414
$createTextNode,
1515
$getRoot,
16+
$isElementNode,
1617
$isParagraphNode,
1718
$isTextNode,
1819
$setSelection,
19-
ParagraphNode,
20-
TextNode,
2120
} from 'lexical';
2221
import {
22+
$assertNodeType,
2323
$createTestDecoratorNode,
2424
$createTestElementNode,
2525
$createTestInlineElementNode,
@@ -44,8 +44,10 @@ describe('LexicalMarkNode tests', () => {
4444

4545
editor.update(() => {
4646
const textNode = $createTextNode('marked');
47-
const paragraphNode =
48-
$getRoot().getFirstChildOrThrow<ParagraphNode>();
47+
const paragraphNode = $assertNodeType(
48+
$getRoot().getFirstChild(),
49+
$isParagraphNode,
50+
);
4951
paragraphNode.append(textNode);
5052
const selection = $createRangeSelection();
5153
selection.anchor.set(textNode.getKey(), 0, 'text');
@@ -57,7 +59,10 @@ describe('LexicalMarkNode tests', () => {
5759
$wrapSelectionInMarkNode(selection, false, 'my-id');
5860

5961
expect(paragraphNode.getChildren()).toHaveLength(1);
60-
const markNode = paragraphNode.getFirstChildOrThrow<MarkNode>();
62+
const markNode = $assertNodeType(
63+
paragraphNode.getFirstChild(),
64+
$isMarkNode,
65+
);
6166
expect(markNode.getType()).toEqual('mark');
6267
expect(markNode.getIDs()).toEqual(['my-id']);
6368
expect(markNode.getChildren()).toHaveLength(1);
@@ -75,8 +80,10 @@ describe('LexicalMarkNode tests', () => {
7580

7681
editor.update(() => {
7782
const textNode = $createTextNode('unmarked marked unmarked');
78-
const paragraphNode =
79-
$getRoot().getFirstChildOrThrow<ParagraphNode>();
83+
const paragraphNode = $assertNodeType(
84+
$getRoot().getFirstChild(),
85+
$isParagraphNode,
86+
);
8087
paragraphNode.append(textNode);
8188
const selection = $createRangeSelection();
8289
selection.anchor.set(textNode.getKey(), 'unmarked '.length, 'text');
@@ -107,8 +114,10 @@ describe('LexicalMarkNode tests', () => {
107114
editor.update(() => {
108115
const decoratorNode = $createTestDecoratorNode();
109116
const textNode = $createTextNode('more text');
110-
const paragraphNode =
111-
$getRoot().getFirstChildOrThrow<ParagraphNode>();
117+
const paragraphNode = $assertNodeType(
118+
$getRoot().getFirstChild(),
119+
$isParagraphNode,
120+
);
112121
paragraphNode.append(decoratorNode, textNode);
113122
const selection = $createRangeSelection();
114123
selection.anchor.set(paragraphNode.getKey(), 0, 'element');
@@ -120,7 +129,10 @@ describe('LexicalMarkNode tests', () => {
120129
$wrapSelectionInMarkNode(selection, false, 'my-id');
121130

122131
expect(paragraphNode.getChildren()).toHaveLength(1);
123-
const markNode = paragraphNode.getFirstChildOrThrow<MarkNode>();
132+
const markNode = $assertNodeType(
133+
paragraphNode.getFirstChild(),
134+
$isMarkNode,
135+
);
124136
expect(markNode.getType()).toEqual('mark');
125137
expect(markNode.getChildren().map(c => c.getKey())).toEqual([
126138
decoratorNode.getKey(),
@@ -173,8 +185,10 @@ describe('LexicalMarkNode tests', () => {
173185
editor.update(() => {
174186
const elementNode = $createTestInlineElementNode();
175187
const textNode = $createTextNode('more text');
176-
const paragraphNode =
177-
$getRoot().getFirstChildOrThrow<ParagraphNode>();
188+
const paragraphNode = $assertNodeType(
189+
$getRoot().getFirstChild(),
190+
$isParagraphNode,
191+
);
178192
paragraphNode.append(elementNode, textNode);
179193
const selection = $createRangeSelection();
180194
selection.anchor.set(paragraphNode.getKey(), 0, 'element');
@@ -186,7 +200,10 @@ describe('LexicalMarkNode tests', () => {
186200
$wrapSelectionInMarkNode(selection, false, 'my-id');
187201

188202
expect(paragraphNode.getChildren()).toHaveLength(1);
189-
const markNode = paragraphNode.getFirstChildOrThrow<MarkNode>();
203+
const markNode = $assertNodeType(
204+
paragraphNode.getFirstChild(),
205+
$isMarkNode,
206+
);
190207
expect(markNode.getType()).toEqual('mark');
191208
expect(markNode.getChildren().map(c => c.getKey())).toEqual([
192209
elementNode.getKey(),
@@ -201,8 +218,10 @@ describe('LexicalMarkNode tests', () => {
201218
editor.update(() => {
202219
const elementNode = $createTestElementNode();
203220
const textNode = $createTextNode('more text');
204-
const paragraphNode =
205-
$getRoot().getFirstChildOrThrow<ParagraphNode>();
221+
const paragraphNode = $assertNodeType(
222+
$getRoot().getFirstChild(),
223+
$isParagraphNode,
224+
);
206225
paragraphNode.append(elementNode, textNode);
207226
const selection = $createRangeSelection();
208227
selection.anchor.set(paragraphNode.getKey(), 0, 'element');
@@ -219,7 +238,10 @@ describe('LexicalMarkNode tests', () => {
219238
);
220239

221240
// the text part of the selection should still be marked
222-
const markNode = paragraphNode.getChildAtIndex(1) as MarkNode;
241+
const markNode = $assertNodeType(
242+
paragraphNode.getChildAtIndex(1),
243+
$isMarkNode,
244+
);
223245
expect(markNode.getType()).toEqual('mark');
224246
expect(markNode.getChildren()).toHaveLength(1);
225247
expect(markNode.getTextContent()).toEqual('more text');
@@ -247,11 +269,22 @@ describe('LexicalMarkNode tests', () => {
247269
const nodes = $generateNodesFromDOM(editor, dom);
248270

249271
expect(nodes).toHaveLength(1);
250-
const paragraphNode = nodes[0] as ParagraphNode;
272+
const paragraphNode = $assertNodeType(nodes[0], $isElementNode);
251273
expect(paragraphNode.getChildren()).toHaveLength(3);
252-
const textNode1 = paragraphNode.getChildAtIndex(0) as TextNode;
253-
const markNode = paragraphNode.getChildAtIndex(1) as MarkNode;
254-
const textNode2 = paragraphNode.getChildAtIndex(2) as TextNode;
274+
// The <mark> element is imported as plain text in this test, so all
275+
// three children are TextNodes.
276+
const textNode1 = $assertNodeType(
277+
paragraphNode.getChildAtIndex(0),
278+
$isTextNode,
279+
);
280+
const markNode = $assertNodeType(
281+
paragraphNode.getChildAtIndex(1),
282+
$isTextNode,
283+
);
284+
const textNode2 = $assertNodeType(
285+
paragraphNode.getChildAtIndex(2),
286+
$isTextNode,
287+
);
255288

256289
expect(textNode1.getTextContent()).toEqual('Foo ');
257290
expect(markNode.getTextContent()).toEqual('Bar');

0 commit comments

Comments
 (0)