【问题标题】:Testing Reacts Material-UI IconMenu. Simulate.click not not working测试 Reacts Material-UI IconMenu。 Simulate.click 不工作
【发布时间】:2016-08-21 00:41:31
【问题描述】:

我正在使用“react-addons-test-utils”为使用 material-ui 的反应组件编写测试用例。出于某种原因,我无法让模拟.click 为 IconMenu 工作,并且不确定我做错了什么。我正在尝试模拟对 iconbutton 的单击,以便我可以遍历弹出窗口并获取菜单项并检查长度,但由于某种原因,我不确定我做错了什么。我还注意到,当单击 IconButton 时,在“Provider”标签之后的 DOM 中附加了一个“PopoverDefaultAnimation”。谢谢!

这是我的组件,它渲染得很好

import React, {PropTypes} from 'react'
/** material-ui **/
import IconMenu from 'material-ui/IconMenu'
import IconButton from 'material-ui/IconButton'
import MenuItem from 'material-ui/MenuItem'
import Divider from 'material-ui/Divider'
import Help from 'material-ui/svg-icons/action/help-outline'
import getMuiTheme from 'material-ui/styles/getMuiTheme'


    export default class MndyHelp extends React.Component{
    constructor(props) {
        //console.log('Main: constructor()');
        super(props);
    }

     static childContextTypes = {
        muiTheme: React.PropTypes.object
    }
    getChildContext() {
        return {
            muiTheme: getMuiTheme()
        }
    }

    render(){

    var urlLink = "https://www.google.com";

        return(
            <IconMenu
                iconButtonElement={
                      <IconButton style={ {padding: 0, width: "auto", height: "auto", right: 44, top: 4 } } iconStyle={{ height: 30, width: 30, fill: "#304954"}}><Help/></IconButton>}>
                <MenuItem onTouchTap={() => {window.open(urlLink, '_blank');}} primaryText='Item1'/>
                <MenuItem onTouchTap={() => {window.open(urlLink, '_blank');}} primaryText='Item2'/>
            </IconMenu>
        );
    }
}

这是我的单元测试:

import React from 'react'

import {renderIntoDocument,
    scryRenderedDOMComponentsWithTag,
    scryRenderedComponentsWithType,
    Simulate
} from 'react-addons-test-utils'

import chai from 'chai'
import ReactDOM from 'react-dom'
import IconButton from 'material-ui/IconButton'
import IconMenu from 'material-ui/IconMenu'
import MenuItem from 'material-ui/MenuItem'
import Popover from 'material-ui/Popover';
import Help from 'material-ui/svg-icons/action/help-outline'
var should = chai.should(),
    expect = chai.expect;

import MndyHelp from './MndyHelp.jsx';
describe('<MndyHelp/>', () => {
    //render kndyhelp
    //get the iconbutton
    //get the popover
    //click the icon button

    it('should have 2 menuItems', () => {
        var domElement  = renderIntoDocument(<MndyHelp/>),
            buttons     = scryRenderedComponentsWithType(domElement,IconButton),
            firstButton = ReactDOM.findDOMNode(buttons[0]);
            Simulate.click(firstButton);
            var popOver = scryRenderedComponentsWithType(domElement,Popover);
            var menuItem = scryRenderedComponentsWithType(domElement,MenuItem);
            //make sure popover is open i.e. true
            expect(popOver[0].props.open).to.equal(true);
            //Make sure menu items exist
            expect(menuItem.length).to.not.equal(0);
            expect(menuItem.length).to.equal(2);
    });

});

【问题讨论】:

    标签: reactjs chai material-ui


    【解决方案1】:

    您无法模拟点击,因为没有可点击的内容。

    你有一个onTouchTap 事件,很遗憾你不能使用 Simulate。点击它,

    事实上,您使用的插件会阻止点击发生:

    当点击发生时,浏览器会发送一个 touchstart 和 touchend,然后在 300 毫秒后发送一个点击事件。如果点击事件之前立即有触摸事件(在最后一个触摸事件的 750 毫秒内),此插件将忽略点击事件。

    https://github.com/zilverline/react-tap-event-plugin

    【讨论】:

    • 是的!这对我完全有用。太感谢了!我在单元测试中添加了 injectTapEventPlugin 并使用了 Simulate.touchTap。我的下一个挑战是测试 MenuItems 的长度。关于如何做到这一点的任何想法?
    猜你喜欢
    • 2015-10-24
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2020-05-04
    • 2015-12-30
    • 2023-03-13
    相关资源
    最近更新 更多