import type { BackendFactory } from 'dnd-core' import type { ComponentType, Ref } from 'react' import { Component, forwardRef } from 'react' import { DndProvider } from 'react-dnd' import { HTML5Backend } from 'react-dnd-html5-backend' import type { ITestBackend, TestBackendOptions } from 'react-dnd-test-backend' import { TestBackend } from 'react-dnd-test-backend' /** * Wrap a Component with a DnDContext using the TestBackend * * @param DecoratedComponent The component to decorate * @returns [Component, getBackend] The wrapped component and a utility method * to get the test backend instance. */ export function wrapWithTestBackend( DecoratedComponent: ComponentType, ): [ComponentType, () => ITestBackend | undefined] { let backend: ITestBackend | undefined const opts: TestBackendOptions = { onCreate(be) { backend = be }, } const result = wrapWithBackend(DecoratedComponent, TestBackend, opts) return [result, () => backend] } /** * Wrap a component with a DndContext providing a backend. * * @param DecoratedComponent The compoent to decorate * @param Backend The backend to use (default=HTML5Backend) * @param backendOptions The optional backend options */ export function wrapWithBackend( DecoratedComponent: ComponentType, Backend: BackendFactory = HTML5Backend, backendOptions?: unknown, ): ComponentType { class TestContextWrapper extends Component< T & { forwardedRef: Ref } > { public override render() { const { forwardedRef, ...rest } = this.props return ( ) } } const ForwardedComponent = forwardRef( function ForwardedTestContextWrapper(props, ref) { return }, ) return ForwardedComponent as unknown as ComponentType }