66 *
77 */
88import { $generateNodesFromDOM } from '@lexical/html' ;
9- import { $isMarkNode , $wrapSelectionInMarkNode , MarkNode } from '@lexical/mark' ;
9+ import { $isMarkNode , $wrapSelectionInMarkNode } from '@lexical/mark' ;
1010import { $insertNodeIntoLeaf } from '@lexical/utils' ;
1111import {
1212 $createParagraphNode ,
1313 $createRangeSelection ,
1414 $createTextNode ,
1515 $getRoot ,
16+ $isElementNode ,
1617 $isParagraphNode ,
1718 $isTextNode ,
1819 $setSelection ,
19- ParagraphNode ,
20- TextNode ,
2120} from 'lexical' ;
2221import {
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