Skip to content

Commit

Permalink
Use withReact and withHistory for tests (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Jan 9, 2025
1 parent 62ebd6a commit 62997ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/slate-editor/src/hyperscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
} from '@prezly/slate-types';
import type { PropsWithChildren } from 'react';
import { createEditor as createSlateEditor } from 'slate';
import { withHistory } from 'slate-history';
import type { HyperscriptCreators } from 'slate-hyperscript';
import {
createEditor as createEditorFactory,
Expand Down Expand Up @@ -122,7 +123,9 @@ const extensions = [
];

const creators: Record<keyof HElements, HyperscriptCreators[string]> = {
editor: createEditorFactory(() => createEditor(createSlateEditor(), () => extensions)),
editor: createEditorFactory(() =>
createEditor(createSlateEditor(), () => extensions, [withReact, withHistory]),
),
'editor-pure': createEditorFactory(() => withReact(createSlateEditor())),
'h:text': createText,
'h-text': createText,
Expand Down
9 changes: 7 additions & 2 deletions packages/slate-editor/src/modules/editor/createEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
withNormalization,
withUserFriendlyDeleteBehavior,
} from '@prezly/slate-commons';
import type { Extension } from '@prezly/slate-commons';
import type { Extension, WithOverrides } from '@prezly/slate-commons';
import { isNotUndefined } from '@technically/is-not-undefined';
import { flow } from '@technically/lodash';
import type { SlateEditor } from '@udecode/plate-common';
Expand All @@ -21,12 +21,17 @@ import {
withSerialization,
} from './plugins';

export function createEditor(baseEditor: SlateEditor, getExtensions: () => Extension[]) {
export function createEditor(
baseEditor: SlateEditor,
getExtensions: () => Extension[],
plugins: WithOverrides[] = [],
) {
const overrides = getExtensions()
.map(({ withOverrides }) => withOverrides)
.filter(isNotUndefined);

return flow([
...plugins,
withNodesHierarchy(hierarchySchema),
withBreaksOnExpandedSelection,
withBreaksOnVoidNodes,
Expand Down
9 changes: 7 additions & 2 deletions packages/slate-editor/src/modules/editor/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Alignment } from '@prezly/slate-types';
import { noop } from '@technically/lodash';
import type { SlateEditor } from '@udecode/plate-common';
import type { PlateEditor } from '@udecode/plate-common/react';
import { withHistory } from 'slate-history';
import { withReact } from 'slate-react';

import { coverage, createDelayedResolve, oembedInfo } from '#modules/tests';

Expand Down Expand Up @@ -101,5 +103,8 @@ export function getAllExtensions() {
}

export function createEditor(input: JSX.Element) {
return createBaseEditor(input as unknown as SlateEditor, getAllExtensions);
return createBaseEditor(input as unknown as PlateEditor, getAllExtensions, [
withReact,
withHistory,
]);
}

0 comments on commit 62997ef

Please sign in to comment.