首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在useDrag和useDrop中使用react-dnd进行测试

在useDrag和useDrop中使用react-dnd进行测试
EN

Stack Overflow用户
提问于 2019-09-18 22:03:41
回答 1查看 1.7K关注 0票数 10

有没有人能够使用带有useDrag和useDrop钩子的功能组件来测试https://github.com/react-dnd/react-dnd的拖放功能?

根据在http://react-dnd.github.io/react-dnd/docs/testing中找到的示例,它们要么使用DragSource装饰原始组件,要么使用DropTarget HOC装饰原始组件。例如:

代码语言:javascript
复制
// ...

export interface BoxProps {
    name: string

    // Collected Props
    isDragging: boolean
    connectDragSource: ConnectDragSource
}
const Box: React.FC<BoxProps> = ({ name, isDragging, connectDragSource }) => {
    const opacity = isDragging ? 0.4 : 1
    return (
        <div ref={connectDragSource} style={{ ...style, opacity }}>
            {name}
        </div>
    )
}

export default DragSource(
    ItemTypes.BOX,
    {
        beginDrag: (props: BoxProps) => ({ name: props.name }),
        endDrag(props: BoxProps, monitor: DragSourceMonitor) {
            const item = monitor.getItem()
            const dropResult = monitor.getDropResult()

            if (dropResult) {
                alert(`You dropped ${item.name} into ${dropResult.name}!`)
            }
        },
    },
    (connect: DragSourceConnector, monitor: DragSourceMonitor) => ({
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging(),
    }),
)(Box)

取自https://github.com/react-dnd/react-dnd/blob/master/packages/documentation/examples-decorators/src/01-dustbin/single-target/Box.tsx的示例

不过,我找不到任何使用它们的钩子的测试示例。他们确实有同时使用装饰器和钩子(https://github.com/react-dnd/react-dnd/tree/master/packages/documentation)的代码示例,但也有仅使用装饰器的测试示例。

EN

回答 1

Stack Overflow用户

发布于 2020-10-02 14:06:02

我从github ticket上复制了这段代码,并为自己工作:

代码语言:javascript
复制
const dragAndDrop = (src: Element, dst: Element) => {
  fireEvent.dragStart(src);
  fireEvent.dragEnter(dst);
  fireEvent.drop(dst);
  fireEvent.dragLeave(dst);
  fireEvent.dragEnd(src);
 };
const allSections = rendered.getAllByRole('dropzone');

const marketSection = allSections[0];
const marketExpandedSection = allSections[1];

const basisIdDrag = within(marketSection).getByRole('draggable');

act(() => {
  dragAndDrop(basisIdDrag, marketExpandedSection);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57994630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档