3 This is testing guidance specific for this Lexical-based WYSIWYG editor.
4 There is a lot of pre-existing test code carried over form the fork of lexical, but since there we've added a range of helpers and altered how testing can be done to make things a bit simpler and aligned with how we run tests.
6 This document is an attempt to document the new best options for added tests with an aim for standardisation on these approaches going forward.
10 Most core test utils can be found in the file at path: resources/js/wysiwyg/lexical/core/__tests__/utils/index.ts
14 This is an example of a typical test using the common modern utilities to help perform actions or assertions. Comments are for this example only, and are not expected in actual test files.
19 dispatchKeydownEventForNode,
20 expectEditorStateJSONPropToEqual,
21 expectNodeShapeToMatch
22 } from "lexical/__tests__/utils";
29 describe('A specific service or file or function', () => {
30 test('it does thing', async () => {
31 // Create the editor context and get an editor reference
32 const {editor} = createTestContext();
34 // Run an action within the editor.
35 let pNode: ParagraphNode;
36 editor.updateAndCommit(() => {
37 pNode = new ParagraphNode();
38 const text = new TextNode('Hello!');
40 $getRoot().append(pNode);
43 // Dispatch key events via the DOM
44 dispatchKeydownEventForNode(pNode!, editor, ' ');
46 // Check the shape (and text) of the resulting state
47 expectNodeShapeToMatch(editor, [{type: 'paragraph', children: [
51 // Check specific props in the resulting JSON state
52 expectEditorStateJSONPropToEqual(editor, '0.0.text', 'Hello!');