import { act, fireEvent, render, type RenderResult, } from '@testing-library/react'; import { parseHotkey } from 'is-hotkey'; import { type BasePluginInput, type CreateEditorOptions, type Editor, type EditorApplicationSchema, type PluginInput, type Value, createEditor, } from 'platejs/react'; import React from 'react'; import { EditorTest } from './PlateTest'; export type EditorTestHarnessOptions = { componentProps?: Partial>; debug?: boolean; }; type ClipboardDataType = 'image/png' | 'text/html' | 'text/plain'; type SharedPluginReference = Exclude; type PasteOptions = { types?: ClipboardDataType[]; }; export type EditorTestActions = { deleteBackward: () => Promise; deleteEntireSoftline: () => Promise; deleteForward: () => Promise; deleteHardLineBackward: () => Promise; deleteHardLineForward: () => Promise; deleteSoftLineBackward: () => Promise; deleteSoftLineForward: () => Promise; deleteWordBackward: () => Promise; deleteWordForward: () => Promise; paste: (payload: string, options?: PasteOptions) => Promise; pressEnter: () => Promise; redo: () => Promise; rerender: () => void; selectAll: () => Promise; triggerKeyboardEvent: (hotkey: string) => Promise; type: (text: string) => Promise; typeSpace: () => Promise; undo: () => Promise; }; export type EditorTestRenderResult = RenderResult; export type ReactTestEditor< TPlugins extends readonly SharedPluginReference[] = readonly [], TSchema extends EditorApplicationSchema | undefined = undefined, > = Editor< Value, RuntimePluginsFromTuple, PlatePluginsFromTuple, TSchema >; type PlatePluginsFromTuple = [TPlugins[number]] extends [BasePluginInput] ? Extract : [Extract] extends [never] ? readonly [] : number extends TPlugins['length'] ? ReadonlyArray> : TPlugins extends readonly [infer TPlugin, ...infer TRest] ? TPlugin extends BasePluginInput ? readonly [ TPlugin, ...PlatePluginsFromTuple< Extract >, ] : PlatePluginsFromTuple< Extract > : readonly []; type RuntimePluginsFromTuple< TPlugins extends readonly SharedPluginReference[], > = [TPlugins[number]] extends [BasePluginInput] ? readonly [] : [Extract] extends [never] ? TPlugins : number extends TPlugins['length'] ? ReadonlyArray> : TPlugins extends readonly [infer TPlugin, ...infer TRest] ? TPlugin extends BasePluginInput ? RuntimePluginsFromTuple< Extract > : TPlugin extends SharedPluginReference ? readonly [ TPlugin, ...RuntimePluginsFromTuple< Extract >, ] : RuntimePluginsFromTuple< Extract > : readonly []; const fireBeforeInput = (element: HTMLElement, init: InputEventInit): void => { fireEvent( element, new InputEvent('beforeinput', { bubbles: true, cancelable: true, ...init, }) ); }; export const createTestEditor = async < const TPlugins extends readonly SharedPluginReference[] = readonly [], const TSchema extends EditorApplicationSchema | undefined = undefined, >( options: CreateEditorOptions, harnessOptions: EditorTestHarnessOptions = {} ): Promise< [ ReactTestEditor, EditorTestActions, EditorTestRenderResult, ] > => { const { componentProps = {}, debug = false } = harnessOptions; const editor = createEditor(options as any) as unknown as ReactTestEditor< TPlugins, TSchema >; const editable = React.createRef(); const callerRef = componentProps.editableProps?.ref; const plateProps = { ...componentProps, editableProps: { ...componentProps.editableProps, ref: (node: HTMLDivElement | null) => { editable.current = node; if (typeof callerRef === 'function') return callerRef(node); if (callerRef) callerRef.current = node; }, }, editor, suppressInstanceWarning: true, }; const rendered = render(React.createElement(EditorTest, plateProps as any)); await act(async () => rendered); const element = editable.current; if (!element) { throw new Error('createTestEditor: EditorTest did not mount its editable'); } Object.defineProperty(element, 'isContentEditable', { configurable: true, value: true, }); if (debug) { editor.subscribeCommit((commit) => { console.info('EDITOR COMMIT', JSON.stringify(commit, null, 2)); }); } const triggerKeyboardEvent = async (hotkey: string) => act(async () => { const eventProps = parseHotkey(hotkey); const values = hotkey.split('+'); const eventInit = { bubbles: true, key: values.at(-1), ...eventProps, }; Reflect.set(eventInit, 'keyCode', Reflect.get(eventProps, 'which')); fireEvent(element, new KeyboardEvent('keydown', eventInit)); }); const type = async (text: string) => act(async () => { fireBeforeInput(element, { data: text, inputType: 'insertText', }); }); const paste = async (payload: string, pasteOptions: PasteOptions = {}) => act(async () => { const types = pasteOptions.types ?? ['text/html']; const event = new Event('paste', { bubbles: true, cancelable: true, composed: true, }) as Event & { clipboardData: { getData: () => string; types: ClipboardDataType[]; }; }; event.clipboardData = { getData: () => payload, types, }; fireEvent(element, event); }); const optionalHistoryEditor = editor as typeof editor & { redo?: () => void; undo?: () => void; }; return [ editor, { deleteBackward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteContentBackward' }); }), deleteEntireSoftline: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteEntireSoftLine' }); }), deleteForward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteContentForward' }); }), deleteHardLineBackward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteHardLineBackward' }); }), deleteHardLineForward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteHardLineForward' }); }), deleteSoftLineBackward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteSoftLineBackward' }); }), deleteSoftLineForward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteSoftLineForward' }); }), deleteWordBackward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteWordBackward' }); }), deleteWordForward: async () => act(async () => { fireBeforeInput(element, { inputType: 'deleteWordForward' }); }), paste, pressEnter: async () => { await triggerKeyboardEvent('Enter'); await act(async () => { fireBeforeInput(element, { inputType: 'insertParagraph' }); }); }, redo: async () => { optionalHistoryEditor.redo?.(); }, rerender: () => { rendered.rerender(React.createElement(EditorTest, plateProps as any)); }, selectAll: async () => act(async () => { editor.update.selection.set([]); }), triggerKeyboardEvent, type, typeSpace: () => type(' '), undo: async () => { optionalHistoryEditor.undo?.(); }, }, rendered, ] as [ ReactTestEditor, EditorTestActions, EditorTestRenderResult, ]; };