Skip to content

Commit

Permalink
Fix typechecking issues in unit tests (#5462)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 authored Jan 8, 2024
1 parent e2becdd commit 8a4ac97
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 375 deletions.
201 changes: 105 additions & 96 deletions packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions packages/lexical/src/__tests__/unit/LexicalEditorState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
*
*/

import {$createParagraphNode, $createTextNode, $getRoot} from 'lexical';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
ParagraphNode,
TextNode,
} from 'lexical';

import {EditorState} from '../../LexicalEditorState';
import {$createRootNode} from '../../nodes/LexicalRootNode';
import {$createRootNode, RootNode} from '../../nodes/LexicalRootNode';
import {initializeUnitTest} from '../utils';

describe('LexicalEditorState tests', () => {
Expand All @@ -33,14 +39,14 @@ describe('LexicalEditorState tests', () => {
$getRoot().append(paragraph);
});

let root = null;
let paragraph = null;
let text = null;
let root!: RootNode;
let paragraph!: ParagraphNode;
let text!: TextNode;

editor.getEditorState().read(() => {
root = $getRoot();
paragraph = root.getFirstChild();
text = paragraph.getFirstChild();
paragraph = root.getFirstChild()!;
text = paragraph.getFirstChild()!;
});

expect(root).toEqual({
Expand Down Expand Up @@ -112,7 +118,7 @@ describe('LexicalEditorState tests', () => {
// Remove the first node, which should cause a GC for everything

await editor.update(() => {
$getRoot().getFirstChild().remove();
$getRoot().getFirstChild()!.remove();
});

expect(editor.getEditorState()._nodeMap).toEqual(
Expand Down
21 changes: 10 additions & 11 deletions packages/lexical/src/__tests__/unit/LexicalListPlugin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TestComposer,
} from 'lexical/src/__tests__/utils';
import * as React from 'react';
import {createRoot} from 'react-dom/client';
import {createRoot, Root} from 'react-dom/client';
import * as ReactTestUtils from 'react-dom/test-utils';

import {
Expand All @@ -31,8 +31,8 @@ import {
} from '../../../../lexical-list/src/index';

describe('@lexical/list tests', () => {
let container: HTMLDivElement | null = null;
let reactRoot;
let container: HTMLDivElement;
let reactRoot: Root;

beforeEach(() => {
container = document.createElement('div');
Expand All @@ -41,9 +41,8 @@ describe('@lexical/list tests', () => {
});

afterEach(() => {
if (container !== null) {
document.body.removeChild(container);
}
container.remove();
// @ts-ignore
container = null;

jest.restoreAllMocks();
Expand Down Expand Up @@ -88,7 +87,7 @@ describe('@lexical/list tests', () => {
});

expectHtmlToBeEqual(
container?.innerHTML || '',
container.innerHTML,
html`
<div
contenteditable="true"
Expand All @@ -113,7 +112,7 @@ describe('@lexical/list tests', () => {
});

expectHtmlToBeEqual(
container?.innerHTML || '',
container.innerHTML,
html`
<div
contenteditable="true"
Expand Down Expand Up @@ -143,7 +142,7 @@ describe('@lexical/list tests', () => {
});

expectHtmlToBeEqual(
container?.innerHTML || '',
container.innerHTML,
html`
<div
contenteditable="true"
Expand All @@ -168,7 +167,7 @@ describe('@lexical/list tests', () => {
});

expectHtmlToBeEqual(
container?.innerHTML || '',
container.innerHTML,
html`
<div
contenteditable="true"
Expand All @@ -195,7 +194,7 @@ describe('@lexical/list tests', () => {
});

expectHtmlToBeEqual(
container?.innerHTML || '',
container.innerHTML,
html`
<div
contenteditable="true"
Expand Down
87 changes: 51 additions & 36 deletions packages/lexical/src/__tests__/unit/LexicalNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ LexicalNode.getType = function () {
describe('LexicalNode tests', () => {
initializeUnitTest(
(testEnv) => {
let paragraphNode;
let textNode;
let paragraphNode: ParagraphNode;
let textNode: TextNode;

beforeEach(async () => {
const {editor} = testEnv;
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('LexicalNode tests', () => {

test('LexicalNode.isAttached()', async () => {
const {editor} = testEnv;
let node;
let node: LexicalNode;

await editor.update(() => {
node = new LexicalNode('__custom_key__');
Expand All @@ -125,7 +125,7 @@ describe('LexicalNode tests', () => {

test('LexicalNode.isSelected()', async () => {
const {editor} = testEnv;
let node;
let node: LexicalNode;

await editor.update(() => {
node = new LexicalNode('__custom_key__');
Expand Down Expand Up @@ -168,8 +168,8 @@ describe('LexicalNode tests', () => {

test('LexicalNode.isSelected(): selected block node range', async () => {
const {editor} = testEnv;
let newParagraphNode;
let newTextNode;
let newParagraphNode: ParagraphNode;
let newTextNode: TextNode;

await editor.update(() => {
expect(paragraphNode.isSelected()).toBe(false);
Expand Down Expand Up @@ -220,8 +220,8 @@ describe('LexicalNode tests', () => {

test('LexicalNode.isSelected(): with custom range selection', async () => {
const {editor} = testEnv;
let newParagraphNode;
let newTextNode;
let newParagraphNode: ParagraphNode;
let newTextNode: TextNode;

await editor.update(() => {
expect(paragraphNode.isSelected()).toBe(false);
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getPreviousSibling()', async () => {
const {editor} = testEnv;
let barTextNode;
let barTextNode: TextNode;

await editor.update(() => {
barTextNode = new TextNode('bar');
Expand All @@ -365,8 +365,8 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getPreviousSiblings()', async () => {
const {editor} = testEnv;
let barTextNode;
let bazTextNode;
let barTextNode: TextNode;
let bazTextNode: TextNode;

await editor.update(() => {
barTextNode = new TextNode('bar');
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getNextSibling()', async () => {
const {editor} = testEnv;
let barTextNode;
let barTextNode: TextNode;

await editor.update(() => {
barTextNode = new TextNode('bar');
Expand All @@ -425,8 +425,8 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getNextSiblings()', async () => {
const {editor} = testEnv;
let barTextNode;
let bazTextNode;
let barTextNode: TextNode;
let bazTextNode: TextNode;

await editor.update(() => {
barTextNode = new TextNode('bar');
Expand All @@ -453,11 +453,11 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getCommonAncestor()', async () => {
const {editor} = testEnv;
let quxTextNode;
let barParagraphNode;
let barTextNode;
let bazParagraphNode;
let bazTextNode;
let quxTextNode: TextNode;
let barParagraphNode: ParagraphNode;
let barTextNode: TextNode;
let bazParagraphNode: ParagraphNode;
let bazTextNode: TextNode;

await editor.update(() => {
const rootNode = $getRoot();
Expand Down Expand Up @@ -499,8 +499,8 @@ describe('LexicalNode tests', () => {

test('LexicalNode.isBefore()', async () => {
const {editor} = testEnv;
let barTextNode;
let bazTextNode;
let barTextNode: TextNode;
let bazTextNode: TextNode;

await editor.update(() => {
barTextNode = new TextNode('bar');
Expand Down Expand Up @@ -542,10 +542,10 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getNodesBetween()', async () => {
const {editor} = testEnv;
let barTextNode;
let bazTextNode;
let newParagraphNode;
let quxTextNode;
let barTextNode: TextNode;
let bazTextNode: TextNode;
let newParagraphNode: ParagraphNode;
let quxTextNode: TextNode;

await editor.update(() => {
const rootNode = $getRoot();
Expand Down Expand Up @@ -590,7 +590,7 @@ describe('LexicalNode tests', () => {

test('LexicalNode.isToken()', async () => {
const {editor} = testEnv;
let tokenTextNode;
let tokenTextNode: TextNode;

await editor.update(() => {
tokenTextNode = new TextNode('token').setMode('token');
Expand All @@ -602,15 +602,15 @@ describe('LexicalNode tests', () => {
);

await editor.getEditorState().read(() => {
expect(textNode.isToken(textNode)).toBe(false);
expect(textNode.isToken()).toBe(false);
expect(tokenTextNode.isToken()).toBe(true);
});
expect(() => textNode.isToken()).toThrow();
});

test('LexicalNode.isSegmented()', async () => {
const {editor} = testEnv;
let segmentedTextNode;
let segmentedTextNode: TextNode;

await editor.update(() => {
segmentedTextNode = new TextNode('segmented').setMode('segmented');
Expand All @@ -622,15 +622,15 @@ describe('LexicalNode tests', () => {
);

await editor.getEditorState().read(() => {
expect(textNode.isSegmented(textNode)).toBe(false);
expect(textNode.isSegmented()).toBe(false);
expect(segmentedTextNode.isSegmented()).toBe(true);
});
expect(() => textNode.isSegmented()).toThrow();
});

test('LexicalNode.isDirectionless()', async () => {
const {editor} = testEnv;
let directionlessTextNode;
let directionlessTextNode: TextNode;

await editor.update(() => {
directionlessTextNode = new TextNode(
Expand Down Expand Up @@ -662,9 +662,9 @@ describe('LexicalNode tests', () => {

test('LexicalNode.getLatest(): garbage collected node', async () => {
const {editor} = testEnv;
let node;
let text;
let block;
let node: LexicalNode;
let text: TextNode;
let block: TestElementNode;

await editor.update(() => {
node = new LexicalNode();
Expand Down Expand Up @@ -762,6 +762,7 @@ describe('LexicalNode tests', () => {
const {editor} = testEnv;

await editor.getEditorState().read(() => {
// @ts-expect-error
expect(() => textNode.replace()).toThrow();
});
expect(() => textNode.remove()).toThrow();
Expand All @@ -773,7 +774,7 @@ describe('LexicalNode tests', () => {
expect(testEnv.outerHTML).toBe(
'<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><p dir="ltr"><span data-lexical-text="true">foo</span></p></div>',
);
let barTextNode;
let barTextNode: TextNode;

await editor.update(() => {
const rootNode = $getRoot();
Expand Down Expand Up @@ -895,8 +896,10 @@ describe('LexicalNode tests', () => {
const {editor} = testEnv;

await editor.getEditorState().read(() => {
// @ts-expect-error
expect(() => textNode.insertAfter()).toThrow();
});
// @ts-expect-error
expect(() => textNode.insertAfter()).toThrow();
});

Expand Down Expand Up @@ -971,7 +974,12 @@ describe('LexicalNode tests', () => {

test('LexicalNode.insertAfter() move blocks around', async () => {
const {editor} = testEnv;
let block1, block2, block3, text1, text2, text3;
let block1: ParagraphNode,
block2: ParagraphNode,
block3: ParagraphNode,
text1: TextNode,
text2: TextNode,
text3: TextNode;

await editor.update(() => {
const root = $getRoot();
Expand Down Expand Up @@ -1003,7 +1011,12 @@ describe('LexicalNode tests', () => {

test('LexicalNode.insertAfter() move blocks around #2', async () => {
const {editor} = testEnv;
let block1, block2, block3, text1, text2, text3;
let block1: ParagraphNode,
block2: ParagraphNode,
block3: ParagraphNode,
text1: TextNode,
text2: TextNode,
text3: TextNode;

await editor.update(() => {
const root = $getRoot();
Expand Down Expand Up @@ -1043,8 +1056,10 @@ describe('LexicalNode tests', () => {
const {editor} = testEnv;

await editor.getEditorState().read(() => {
// @ts-expect-error
expect(() => textNode.insertBefore()).toThrow();
});
// @ts-expect-error
expect(() => textNode.insertBefore()).toThrow();
});

Expand Down
Loading

2 comments on commit 8a4ac97

@vercel
Copy link

@vercel vercel bot commented on 8a4ac97 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-fbopensource.vercel.app
lexical.dev
lexicaljs.com
lexical-git-main-fbopensource.vercel.app
lexicaljs.org
www.lexical.dev

@vercel
Copy link

@vercel vercel bot commented on 8a4ac97 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-git-main-fbopensource.vercel.app
lexical-playground-fbopensource.vercel.app
lexical-playground.vercel.app
playground.lexical.dev

Please sign in to comment.