Created
November 15, 2019 16:23
-
-
Save apflieger/608816b15fbbf21fae298d2fc888666f to your computer and use it in GitHub Desktop.
Mock implementations of react-dnd useDrag/useDrop to use in tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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