From 6106b5bc9b6fd81048ff4904d56a43e65d27c5bd Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Sat, 27 Apr 2024 18:23:59 -0700 Subject: [PATCH] fix: allow tsc to typecheck tests, fix type issues in those tests --- .../__tests__/unit/LexicalCodeNode.test.ts | 54 +++--- .../unit/LexicalHeadlessEditor.test.ts | 11 +- .../__tests__/unit/LexicalHistory.test.tsx | 4 +- .../unit/LexicalListItemNode.test.ts | 55 +++--- .../__tests__/unit/LexicalListNode.test.ts | 2 +- .../src/__tests__/unit/Collaboration.test.ts | 56 +++--- .../unit/CollaborationWithCollisions.test.ts | 16 +- .../__tests__/unit/LexicalComposer.test.tsx | 8 +- .../unit/PlainRichTextPlugin.test.tsx | 19 +- .../unit/useLexicalCharacterLimit.test.ts | 32 ++-- .../useLexicalIsTextContentEmpty.test.tsx | 16 +- .../src/__tests__/unit/utils.tsx | 106 +++++------ .../__tests__/unit/LexicalHeadingNode.test.ts | 14 +- .../__tests__/unit/LexicalQuoteNode.test.ts | 8 +- .../__tests__/unit/LexicalSelection.test.tsx | 180 ++++++++++++------ .../unit/LexicalSelectionHelpers.test.ts | 120 +++++++----- .../src/__tests__/utils/index.ts | 106 ++++++----- .../unit/LexicalTableSelection.test.tsx | 41 ++-- .../unit/LexicalEventHelpers.test.tsx | 35 ++-- .../__tests__/unit/LexicalNodeHelpers.test.ts | 18 +- .../unit/LexicalUtilsSplitNode.test.tsx | 4 +- ...xlcaiUtilsInsertNodeToNearestRoot.test.tsx | 4 +- packages/lexical-website/docs/faq.md | 49 +++++ tsconfig.json | 1 - tsconfig.test.json | 1 - 25 files changed, 561 insertions(+), 399 deletions(-) diff --git a/packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts b/packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts index b44bd3ad6060..406c8c757a8e 100644 --- a/packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts +++ b/packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts @@ -178,7 +178,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertText('function'); + $getSelection()!.insertText('function'); }); await editor.dispatchCommand(KEY_TAB_COMMAND, tabKeyboardEvent()); expect(testEnv.innerHTML).toBe( @@ -187,12 +187,12 @@ describe('LexicalCodeNode tests', () => { // CodeNode should only render diffs, make sure that the TabNode is not cloned when // appending more text - let tabKey; + let tabKey: string; await editor.update(() => { tabKey = $dfs() - .find(({node}) => $isTabNode(node)) + .find(({node}) => $isTabNode(node))! .node.getKey(); - $getSelection().insertText('foo'); + $getSelection()!.insertText('foo'); }); expect( editor.getEditorState().read(() => { @@ -214,7 +214,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertText('function'); + $getSelection()!.insertText('function'); }); // TODO consolidate editor.update - there's some bad logic in updateAndRetainSelection await editor.update(() => { @@ -238,7 +238,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertText('function'); + $getSelection()!.insertText('function'); }); // TODO consolidate editor.update - there's some bad logic in updateAndRetainSelection await editor.update(() => { @@ -253,8 +253,8 @@ describe('LexicalCodeNode tests', () => { await editor.update(() => { const root = $getRoot(); - const codeTab = root.getFirstDescendant(); - const codeText = root.getLastDescendant(); + const codeTab = root.getFirstDescendant()!; + const codeText = root.getLastDescendant()!; const selection = $createRangeSelection(); selection.anchor.set(codeTab.getKey(), 0, 'text'); selection.focus.set(codeText.getKey(), 'function'.length, 'text'); @@ -275,7 +275,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertText('function'); + $getSelection()!.insertText('function'); }); // TODO consolidate editor.update - there's some bad logic in updateAndRetainSelection await editor.update(() => { @@ -290,8 +290,8 @@ describe('LexicalCodeNode tests', () => { await editor.update(() => { const root = $getRoot(); - const codeTab = root.getFirstDescendant(); - const codeText = root.getLastDescendant(); + const codeTab = root.getFirstDescendant()!; + const codeText = root.getLastDescendant()!; const selection = $createRangeSelection(); selection.anchor.set(codeTab.getKey(), 0, 'text'); selection.focus.set(codeText.getKey(), 0, 'text'); @@ -313,12 +313,12 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertRawText('hello\tworld\nhello\tworld'); + $getSelection()!.insertRawText('hello\tworld\nhello\tworld'); }); // TODO consolidate editor.update - there's some bad logic in updateAndRetainSelection await editor.update(() => { - const firstCodeText = $getRoot().getFirstDescendant(); - const lastCodeText = $getRoot().getLastDescendant(); + const firstCodeText = $getRoot().getFirstDescendant()!; + const lastCodeText = $getRoot().getLastDescendant()!; const selection = $createRangeSelection(); selection.anchor.set(firstCodeText.getKey(), 1, 'text'); selection.focus.set(lastCodeText.getKey(), 1, 'text'); @@ -347,7 +347,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertRawText('hello\n'); + $getSelection()!.insertRawText('hello\n'); }); await editor.dispatchCommand(KEY_TAB_COMMAND, tabKeyboardEvent()); expect(testEnv.innerHTML) @@ -365,7 +365,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertRawText('\thello'); + $getSelection()!.insertRawText('\thello'); }); // TODO consolidate editor.update - there's some bad logic in updateAndRetainSelection await editor.update(() => { @@ -389,7 +389,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertRawText('abc\tdef\nghi\tjkl'); + $getSelection()!.insertRawText('abc\tdef\nghi\tjkl'); }); const keyEvent = new KeyboardEventMock(); keyEvent.altKey = true; @@ -409,16 +409,16 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - $getSelection().insertRawText('abc\tdef\nghi\tjkl\nmno\tpqr'); + $getSelection()!.insertRawText('abc\tdef\nghi\tjkl\nmno\tpqr'); }); // TODO consolidate editor.update - there's some bad logic in updateAndRetainSelection await editor.update(() => { - const firstCodeText = $getRoot().getFirstDescendant(); + const firstCodeText = $getRoot().getFirstDescendant()!; const secondCodeText = firstCodeText - .getNextSibling() // tab - .getNextSibling() // def - .getNextSibling() // linebreak - .getNextSibling(); // ghi; + .getNextSibling()! // tab + .getNextSibling()! // def + .getNextSibling()! // linebreak + .getNextSibling()!; // ghi; const selection = $createRangeSelection(); selection.anchor.set(firstCodeText.getKey(), 1, 'text'); selection.focus.set(secondCodeText.getKey(), 1, 'text'); @@ -455,7 +455,7 @@ describe('LexicalCodeNode tests', () => { const code = $createCodeNode(); root.append(code); code.selectStart(); - const selection = $getSelection(); + const selection = $getSelection()!; if (tabOrSpaces === 'tab') { selection.insertRawText('\t\tfunction foo\n\t\tfunction bar'); } else { @@ -570,7 +570,7 @@ describe('LexicalCodeNode tests', () => { const firstChild = code.getFirstChild(); invariant($isTextNode(firstChild)); if (tabOrSpaces === 'tab') { - firstChild.getNextSibling().selectNext(0, 0); + firstChild.getNextSibling()!.selectNext(0, 0); } else { firstChild.select(4, 4); } @@ -605,7 +605,7 @@ describe('LexicalCodeNode tests', () => { $isLineBreakNode(dfsNode.node), )[0].node; if (tabOrSpaces === 'tab') { - const firstTab = linebreak.getNextSibling(); + const firstTab = linebreak.getNextSibling()!; firstTab.selectNext(); } else { linebreak.selectNext(4, 4); @@ -687,7 +687,7 @@ describe('LexicalCodeNode tests', () => { $isLineBreakNode(dfsNode.node), )[0].node; if (tabOrSpaces === 'tab') { - const firstTab = linebreak.getNextSibling(); + const firstTab = linebreak.getNextSibling()!; firstTab.selectNext(0, 0); } else { linebreak.selectNext(2, 2); diff --git a/packages/lexical-headless/src/__tests__/unit/LexicalHeadlessEditor.test.ts b/packages/lexical-headless/src/__tests__/unit/LexicalHeadlessEditor.test.ts index 9f695c1f3167..afa65708d4bd 100644 --- a/packages/lexical-headless/src/__tests__/unit/LexicalHeadlessEditor.test.ts +++ b/packages/lexical-headless/src/__tests__/unit/LexicalHeadlessEditor.test.ts @@ -16,7 +16,7 @@ * */ -import type {RangeSelection} from 'lexical'; +import type {EditorState, LexicalEditor, RangeSelection} from 'lexical'; import {$generateHtmlFromNodes} from '@lexical/html'; import {JSDOM} from 'jsdom'; @@ -33,14 +33,17 @@ import { import {createHeadlessEditor} from '../..'; describe('LexicalHeadlessEditor', () => { - let editor; + let editor: LexicalEditor; - async function update(updateFn) { + async function update(updateFn: () => void) { editor.update(updateFn); await Promise.resolve(); } - function assertEditorState(editorState, nodes) { + function assertEditorState( + editorState: EditorState, + nodes: Record[], + ) { const nodesFromState = Array.from(editorState._nodeMap.values()); expect(nodesFromState).toEqual( nodes.map((node) => expect.objectContaining(node)), diff --git a/packages/lexical-history/src/__tests__/unit/LexicalHistory.test.tsx b/packages/lexical-history/src/__tests__/unit/LexicalHistory.test.tsx index c1017b186840..4b13bb94fa2e 100644 --- a/packages/lexical-history/src/__tests__/unit/LexicalHistory.test.tsx +++ b/packages/lexical-history/src/__tests__/unit/LexicalHistory.test.tsx @@ -34,12 +34,12 @@ import { } from 'lexical/src'; import {createTestEditor, TestComposer} from 'lexical/src/__tests__/utils'; import React from 'react'; -import {createRoot} from 'react-dom/client'; +import {createRoot, Root} from 'react-dom/client'; import * as ReactTestUtils from 'react-dom/test-utils'; describe('LexicalHistory tests', () => { let container: HTMLDivElement | null = null; - let reactRoot; + let reactRoot: Root; beforeEach(() => { container = document.createElement('div'); diff --git a/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts b/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts index 5abc144ae2c6..a739c5fa7ded 100644 --- a/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts +++ b/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts @@ -6,7 +6,12 @@ * */ -import {$createParagraphNode, $getRoot, TextNode} from 'lexical'; +import { + $createParagraphNode, + $createRangeSelection, + $getRoot, + TextNode, +} from 'lexical'; import { expectHtmlToBeEqual, html, @@ -147,10 +152,10 @@ describe('LexicalListItemNode tests', () => { }); describe('ListItemNode.replace()', () => { - let listNode; - let listItemNode1; - let listItemNode2; - let listItemNode3; + let listNode: ListNode; + let listItemNode1: ListItemNode; + let listItemNode2: ListItemNode; + let listItemNode3: ListItemNode; beforeEach(async () => { const {editor} = testEnv; @@ -391,7 +396,7 @@ describe('LexicalListItemNode tests', () => { // - B test('siblings are not nested', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -459,7 +464,7 @@ describe('LexicalListItemNode tests', () => { // - B test('the previous sibling is nested', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -539,7 +544,7 @@ describe('LexicalListItemNode tests', () => { // - B test('the next sibling is nested', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -619,7 +624,7 @@ describe('LexicalListItemNode tests', () => { // - B test('both siblings are nested', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -708,7 +713,7 @@ describe('LexicalListItemNode tests', () => { // - B test('the previous sibling is nested deeper than the next sibling', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -818,7 +823,7 @@ describe('LexicalListItemNode tests', () => { // - B2 test('the next sibling is nested deeper than the previous sibling', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -929,7 +934,7 @@ describe('LexicalListItemNode tests', () => { // - B2 test('both siblings are deeply nested', async () => { const {editor} = testEnv; - let x; + let x: ListItemNode; await editor.update(() => { const root = $getRoot(); @@ -1052,10 +1057,10 @@ describe('LexicalListItemNode tests', () => { }); describe('ListItemNode.insertNewAfter(): non-empty list items', () => { - let listNode; - let listItemNode1; - let listItemNode2; - let listItemNode3; + let listNode: ListNode; + let listItemNode1: ListItemNode; + let listItemNode2: ListItemNode; + let listItemNode3: ListItemNode; beforeEach(async () => { const {editor} = testEnv; @@ -1103,7 +1108,7 @@ describe('LexicalListItemNode tests', () => { const {editor} = testEnv; await editor.update(() => { - listItemNode1.insertNewAfter(); + listItemNode1.insertNewAfter($createRangeSelection()); }); expectHtmlToBeEqual( @@ -1134,7 +1139,7 @@ describe('LexicalListItemNode tests', () => { const {editor} = testEnv; await editor.update(() => { - listItemNode3.insertNewAfter(); + listItemNode3.insertNewAfter($createRangeSelection()); }); expectHtmlToBeEqual( @@ -1165,7 +1170,7 @@ describe('LexicalListItemNode tests', () => { const {editor} = testEnv; await editor.update(() => { - listItemNode3.insertNewAfter(); + listItemNode3.insertNewAfter($createRangeSelection()); }); expectHtmlToBeEqual( @@ -1217,7 +1222,7 @@ describe('LexicalListItemNode tests', () => { ); await editor.update(() => { - listItemNode1.insertNewAfter(); + listItemNode1.insertNewAfter($createRangeSelection()); }); expectHtmlToBeEqual( @@ -1264,9 +1269,9 @@ describe('LexicalListItemNode tests', () => { }); describe('ListItemNode.setIndent()', () => { - let listNode; - let listItemNode1; - let listItemNode2; + let listNode: ListNode; + let listItemNode1: ListItemNode; + let listItemNode2: ListItemNode; beforeEach(async () => { const {editor} = testEnv; @@ -1296,7 +1301,7 @@ describe('LexicalListItemNode tests', () => { }); expectHtmlToBeEqual( - editor.getRootElement().innerHTML, + editor.getRootElement()!.innerHTML, html`