我正在使用材料-ui使用'react-addons-test-utils'编写反应组件的测试用例。出于某种原因,我无法让simulate.click为IconMenu工作,我不确定我做错了什么。我正在尝试模拟iconbutton上的一个点击,以便我可以遍历pop-over并获取菜单项并检查长度,但由于某种原因,我不确定我做错了什么。我还注意到,单击IconButton时,在'Provider'标记之后有一个'PopoverDefaultAnimation'附加到DOM。谢谢!
这是我的组件,它可以很好地呈现
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);
});
});
答案 0 :(得分:3)
您无法模拟点击,因为无需点击。
您有一个onTouchTap
事件,遗憾的是您无法使用Simulate.click,
事实上,您使用的插件可以阻止点击发生:
当点击发生时,浏览器会发送一个touchstart和touchend,然后300ms之后发送一个点击事件。如果点击事件紧接触摸事件(在最后一次触摸事件的750毫秒内),则此插件会忽略点击事件。