Skip to content

Instantly share code, notes, and snippets.

@apflieger
Created November 15, 2019 16:23
Show Gist options
  • Save apflieger/608816b15fbbf21fae298d2fc888666f to your computer and use it in GitHub Desktop.
Save apflieger/608816b15fbbf21fae298d2fc888666f to your computer and use it in GitHub Desktop.
Mock implementations of react-dnd useDrag/useDrop to use in tests
import {
ConnectDragPreview,
ConnectDragSource, ConnectDropTarget,
DragObjectWithType,
DragSourceHookSpec,
DragSourceMonitor,
DropTargetHookSpec, DropTargetMonitor,
XYCoord
} from "react-dnd";
import {Identifier} from "dnd-core";
const dragSourceMonitor: DragSourceMonitor = {
getHandlerId: function () {
return null;
}, receiveHandlerId: function (p1: Identifier | null) {
}, canDrag(): boolean {
return false;
}, didDrop(): boolean {
return false;
}, getClientOffset(): XYCoord | null {
return null;
}, getDifferenceFromInitialOffset(): XYCoord | null {
return null;
}, getDropResult(): any {
}, getInitialClientOffset(): XYCoord | null {
return null;
}, getInitialSourceClientOffset(): XYCoord | null {
return null;
}, getItem(): any {
}, getItemType(): string | symbol | null {
return null;
}, getSourceClientOffset(): XYCoord | null {
return null;
}, getTargetIds(): Identifier[] {
return [];
}, isDragging(): boolean {
return false;
}, subscribeToStateChange(fn: () => void, options?: { handlerIds?: Identifier[] }): () => void {
return fn;
}
};
const dropTargetMonitor: DropTargetMonitor = {
getHandlerId: function () {
return null;
}, receiveHandlerId: function (p1: Identifier | null) {
}, canDrop(): boolean {
return false;
}, didDrop(): boolean {
return false;
}, getClientOffset(): XYCoord | null {
return null;
}, getDifferenceFromInitialOffset(): XYCoord | null {
return null;
}, getDropResult(): any {
}, getInitialClientOffset(): XYCoord | null {
return null;
}, getInitialSourceClientOffset(): XYCoord | null {
return null;
}, getItem(): any {
}, getItemType(): string | symbol | null {
return null;
}, getSourceClientOffset(): XYCoord | null {
return null;
}, isOver(options?: { shallow?: boolean }): boolean {
return false;
}, subscribeToStateChange(fn: () => void, options?: { handlerIds?: Identifier[] }): () => void {
return fn;
}
};
export function useDragMock<DragObject extends DragObjectWithType, DropResult, CollectedProps>(): (spec: DragSourceHookSpec<DragObject, DropResult, CollectedProps>) => [CollectedProps, ConnectDragSource, ConnectDragPreview] {
// @ts-ignore
return spec => ([spec.collect ? spec.collect(dragSourceMonitor) : null, null, null])
}
export function useDropMock<DragObject extends DragObjectWithType, DropResult, CollectedProps>(): (spec: DropTargetHookSpec<DragObject, DropResult, CollectedProps>) => [CollectedProps, ConnectDropTarget] {
// @ts-ignore
return spec => ([spec.collect ? spec.collect(dropTargetMonitor) : null, null])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment