Skip to content

Commit

Permalink
added test for registerDefaultNodes fn
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinansfield committed Oct 19, 2023
1 parent c0b475e commit 97e4d5d
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/kg-default-transforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"access": "public"
},
"devDependencies": {
"@lexical/headless": "^0.12.2",
"@lexical/link": "^0.12.2",
"c8": "8.0.1",
"mocha": "10.2.0",
"sinon": "16.1.0",
Expand Down
8 changes: 0 additions & 8 deletions packages/kg-default-transforms/test/hello.test.ts

This file was deleted.

294 changes: 294 additions & 0 deletions packages/kg-default-transforms/test/kg-default-transforms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
import assert from 'assert/strict';
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
import {ListItemNode, ListNode} from '@lexical/list';
import {LinkNode} from '@lexical/link';
import {DEFAULT_NODES} from '@tryghost/kg-default-nodes';
import {createHeadlessEditor} from '@lexical/headless';
import {CreateEditorArgs} from 'lexical';
import {registerDefaultTransforms} from '../';

const defaultNodes: any[] = [
// basic HTML nodes
HeadingNode,
LinkNode,
ListItemNode,
ListNode,
QuoteNode,

// Koenig nodes
...DEFAULT_NODES
];

const defaultEditorConfig: CreateEditorArgs = {
nodes: defaultNodes,
onError(e: Error) {
throw e;
}
};

const createEditor = function (config?: CreateEditorArgs) {
const editorConfig: CreateEditorArgs = Object.assign({}, defaultEditorConfig, config);
const editor = createHeadlessEditor(editorConfig);

return editor;
};

describe('Default transforms', function () {
it('registerDefaultTransforms() registers all transforms', function () {
const editor = createEditor();

// method under test
registerDefaultTransforms(editor);

// "invalid" editor state that should be transformed
const state = JSON.stringify({
root: {
children: [
// nested paragraphs
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'Normal',
type: 'extended-text',
version: 1
},
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'Nested',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1
},

// adjacent lists
{
children: [
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'one',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
version: 1,
value: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'list',
version: 1,
listType: 'bullet',
start: 1,
tag: 'ul'
},
{
children: [
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'two',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
version: 1,
value: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'list',
version: 1,
listType: 'bullet',
start: 1,
tag: 'ul'
},

// heading with center alignment
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'Aligned header',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: 2,
indent: 0,
type: 'heading',
version: 1,
tag: 'h1'
}
]
}
});

const editorState = editor.parseEditorState(state);
editor.setEditorState(editorState);

const transformedEditorState = editor.getEditorState().toJSON();
assert.deepEqual(transformedEditorState, {
root: {
children: [
// de-nested paragraphs
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'Normal',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1
},
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'Nested',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1
},

// adjacent lists merged
{
children: [
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'one',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
version: 1,
value: 1
},
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'two',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
version: 1,
value: 2
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'list',
version: 1,
listType: 'bullet',
start: 1,
tag: 'ul'
},

// heading with alignment format reset
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'Aligned header',
type: 'extended-text',
version: 1
}
],
direction: 'ltr',
format: '',
indent: 0,
type: 'heading',
version: 1,
tag: 'h1'
}
]
}
});
});
});
4 changes: 4 additions & 0 deletions packages/kg-default-transforms/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
// required for tests to find module declarations in the src folder
"ts-node": {
"files": true
},
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

Expand Down

0 comments on commit 97e4d5d

Please sign in to comment.