首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应TestUtils,我如何模拟文档mouseMove?

响应TestUtils,我如何模拟文档mouseMove?
EN

Stack Overflow用户
提问于 2015-06-25 16:23:28
回答 2查看 11.7K关注 0票数 3

我想在TestUtils.Simulate.mouseMove上使用document。我有一个组件Dragger,它将一个mouseMove事件侦听器添加到document中。以下是一个不完整的版本:

代码语言:javascript
复制
// Dragger.js
'use strict';

var React = require('react');

export default React.createClass({
    propTypes: {
        handleDrag: React.PropTypes.func // callback set by parent
    },
    getInitialState: function() {
        return {dragging: false}
    },
    componentDidUpdate: function(props, state) {
        // 
        if (this.state.dragging && !state.dragging) {
            document.addEventListener('mousemove', this.onMouseMove)
        } else if (!this.state.dragging && state.dragging) {
            document.removeEventListener('mousemove', this.onMouseMove)
        }
    },
    onMouseDown: function(e) {
        this.setState({dragging: true})
    },
    onMouseMove: function(e) {
        // Calls back to the parent with the drag
        this.props.handleDrag(e);
    },
    render: function() {
        return <div onMouseDown={this.onMouseDown} ></div>
    }
});

我使用的是茉莉,我希望确保handleDrag回调是在mouseDownmouseMove之后调用的。

代码语言:javascript
复制
// Dragger.spec.js

var React = require('react/addons');
import Dragger from './Dragger';

var TestUtils = React.addons.TestUtils;

describe('Dragger', function() {
    it('should call the callback after drag interaction', function() {
        // make callback to spy on
        var f = {callback: function(e){return}};

        // render Dragger
        var dragger = TestUtils.renderIntoDocument(<Dragger handleDrag={f.callback} />);

        // spy on callback
        spyOn(f, 'callback');

        // simulate a mouseDown and mouseMove
        TestUtils.Simulate.mouseDown(dragger.getDOMNode(), {button: 0});
        TestUtils.Simulate.mouseMove(document);

        expect(f.callback).toHaveBeenCalled(); // FAILS!
    }
}

但是mouseMove事件没有被正确地模拟。我看到了两个问题

  1. 我可能需要将事件数据传递给TestUtils.Simulate.mouseMove。例如,调用TestUtils.Simulate.mouseDown(dragger.getDOMNode())使不工作,直到我将它更改为TestUtils.Simulate.mouseDown(dragger.getDOMNode(), {button: 0})。应该将哪些事件数据传递给TestUtils.Simulate.mouseMove
  2. document不是将测试组件呈现到的分离DOM的一部分。这可能是Simulate.mouseMove不能工作的另一个原因。在测试中我可以用什么代替document

如何使用TestUtils.Simulate.mouseMove

EN

回答 2

Stack Overflow用户

发布于 2016-09-07 17:08:41

在用酶法和react的TestUtils尝试了几个小时之后,我终于找到了在纯JS中创建和分派事件的方法,它在我的jest测试中工作如下

代码语言:javascript
复制
it('calls handler on mouseDown on element, mouseMove on document', () => {
  const handler = jest.fn();
  const props = {
    foo: {
      uid: '1',
      resizable: true,
    },
    resizeHandler,
  };

  const instance = mount(<Header {...props} />);
  const resizer = instance.find('.resizer');
  const top = window.document.documentElement;  // target the documentElement
  resizer.simulate('mouseDown', { preventDefault: () => true });   // uses enzyme to simulate this event, adding listener to documentElement on mousemove
  const mouseMove = new Event('mousemove');  // creates a new event
  top.dispatchEvent(mouseMove);              // dispatches it
  const mouseUp = new Event('mouseup');
  top.dispatchEvent(mouseUp);
  expect(resizeHandler).toBeCalled();        // the passed in handler is called on mousemove
});

基本上,您可以找到带有document.documentElementwindow.document.documentElement,并像任何其他element一样从它中分派事件。

票数 4
EN

Stack Overflow用户

发布于 2016-08-05 17:16:41

这是一个旧的帖子,但我看到还没有张贴的解决方案,我遇到它,因为我正在写一个类似的组件。我认为问题在于,您关注的是错误的事件,您应该使用onDrag来拖动检测,下面是适合我的代码的改编版本:

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

import React from 'react';

export default React.createClass({
    propTypes: {
        handleDrag: React.PropTypes.func // callback set by parent
    },
    getInitialState: function() {
        return {dragging: false}
    },
    onDragStart: function(e) {
        // Calls back to the parent with the drag
        this.setState({ dragging: true });
        this.props.handleDrag(e);
    },
    onDragEnd: function() {
        this.setState({ dragging: false });
    },
    render: function() {
        return <div onDragStart={this.onDragStart} onDragEnd={this.onDragEnd}></div>;
    }
});

代码语言:javascript
复制
// Dragger.spec.js

import React from 'react';
import Dragger from '../src/Dragger';
import {
    renderIntoDocument,
    scryRenderedDOMComponentsWithTag,
    Simulate
} from 'react-addons-test-utils';

import { expect } from 'chai';

describe('Dragger', function() {
    it('should call the callback after drag interaction', function() {
        // spy on callback
        var called = false;
        // make callback to spy on
        function callback() {
            called = true;
        };

        // render Dragger
        var dragger = renderIntoDocument(<Dragger handleDrag={callback} />);

        // simulate a dragStart and dragEnd
        const element = scryRenderedDOMComponentsWithTag(dragger, 'div')[0];
        Simulate.dragStart(element);
        Simulate.dragEnd(element);
        expect(called).to.equal(true);
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31055738

复制
相关文章

相似问题

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