用@testing-library/react编写react-beautiful-dnd拖拽测试onDragEnd异常
跨列表拖拽功能测试用例执行异常说明
问题描述
在编写节点跨列表拖拽的功能校验测试用例时,预期拖拽流程结束后 onDragEnd 函数将接收到包含有效 destination、返回 reason: 'DROP' 的结果对象,实际运行结果与预期不符。
实际返回的结果结构
{ draggableId: '6001e75c6475cb6add2a8c74-new 4', type: 'DEFAULT', source: { droppableId: 'droppable-main', index: 0 }, mode: 'FLUID', combine: null, destination: null, reason: 'CANCEL' }
对应测试用例代码
export const elementToBePresent = async (id, getByTestId, toAwait = false) => { const element = toAwait ? await waitForElement(() => getByTestId(id)) : getByTestId(id) expect(element).toBeInTheDOM() } const mockGetBoundingClientRect = (el, bound = null) => jest.spyOn(el, 'getBoundingClientRect').mockImplementation(() => !bound ? { top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0, x: space, y: space, center: { x: 0, y: 0 } } : bound ) describe('Alternate Hierarchy Tree', () => { it('copy node', async () => { // 清理测试环境控制台冗余输出 console.warn = jest.fn() console.error = jest.fn() await setUpDataAlternate() const { getByTestId, debug } = render( <AlternateHierarchyPage rootId={'root'} location={alternateHierarchyLocation} /> ) /* 待拖拽节点核心信息: draggableId: 6001e75c6475cb6add2a8c74-new 4 source: { droppableId: 'droppable-main', index: 0 } */ const masterListNode = await waitForElement(() => getByTestId('master-list-node-0-0') ) mockGetBoundingClientRect(masterListNode, { bottom: 170.7291717529297, height: 48.99305725097656, left: 245.62501525878906, right: 490.6250305175781, top: 121.73611450195312, width: 245.00001525878906, x: 245.62501525878906, y: 121.73611450195312 }) // 目标放置列表元素 const listElement = await waitForElement(() => getByTestId('list-0') ) mockGetBoundingClientRect(listElement, { bottom: 430.26043701171875, height: 285.6944580078125, left: 589.7222290039062, right: 851.7361450195312, top: 144.56597900390625, width: 262.013916015625, x: 589.7222290039062, y: 144.56597900390625 }) // 按下鼠标触发拖拽起始状态 fireEvent.mouseDown(masterListNode, {clientX: 245.62501525878906, clientY: 121.73611450195312}) // 移动鼠标至目标列表区域 fireEvent.mouseMove(masterListNode, { clientX: 589.7222290039062, clientY: 144.56597900390625 }) // 松开鼠标完成放置动作 fireEvent.mouseUp(masterListNode, { clientX: 589.7222290039062, clientY: 144.56597900390625 }) }) })
内容的提问来源于stack exchange,提问作者MTA
相关产品推荐
相关产品推荐

