Skip to content

Commit

Permalink
fix: allow tsc to typecheck tests, fix type issues in those tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Apr 28, 2024
1 parent 32b346c commit 6106b5b
Show file tree
Hide file tree
Showing 25 changed files with 561 additions and 399 deletions.
54 changes: 27 additions & 27 deletions packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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');
Expand All @@ -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(() => {
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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(() => {
Expand All @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string, unknown>[],
) {
const nodesFromState = Array.from(editorState._nodeMap.values());
expect(nodesFromState).toEqual(
nodes.map((node) => expect.objectContaining(node)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*
*/

import {$createParagraphNode, $getRoot, TextNode} from 'lexical';
import {
$createParagraphNode,
$createRangeSelection,
$getRoot,
TextNode,
} from 'lexical';
import {
expectHtmlToBeEqual,
html,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1103,7 +1108,7 @@ describe('LexicalListItemNode tests', () => {
const {editor} = testEnv;

await editor.update(() => {
listItemNode1.insertNewAfter();
listItemNode1.insertNewAfter($createRangeSelection());
});

expectHtmlToBeEqual(
Expand Down Expand Up @@ -1134,7 +1139,7 @@ describe('LexicalListItemNode tests', () => {
const {editor} = testEnv;

await editor.update(() => {
listItemNode3.insertNewAfter();
listItemNode3.insertNewAfter($createRangeSelection());
});

expectHtmlToBeEqual(
Expand Down Expand Up @@ -1165,7 +1170,7 @@ describe('LexicalListItemNode tests', () => {
const {editor} = testEnv;

await editor.update(() => {
listItemNode3.insertNewAfter();
listItemNode3.insertNewAfter($createRangeSelection());
});

expectHtmlToBeEqual(
Expand Down Expand Up @@ -1217,7 +1222,7 @@ describe('LexicalListItemNode tests', () => {
);

await editor.update(() => {
listItemNode1.insertNewAfter();
listItemNode1.insertNewAfter($createRangeSelection());
});

expectHtmlToBeEqual(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1296,7 +1301,7 @@ describe('LexicalListItemNode tests', () => {
});

expectHtmlToBeEqual(
editor.getRootElement().innerHTML,
editor.getRootElement()!.innerHTML,
html`
<ul>
<li value="1">
Expand Down Expand Up @@ -1330,7 +1335,7 @@ describe('LexicalListItemNode tests', () => {
});

expectHtmlToBeEqual(
editor.getRootElement().innerHTML,
editor.getRootElement()!.innerHTML,
html`
<ul>
<li value="1" dir="ltr">
Expand Down
Loading

0 comments on commit 6106b5b

Please sign in to comment.