【发布时间】:2017-05-01 09:26:51
【问题描述】:
我正在尝试使用 Jest、React 和 Typescript 从Jest Tutorial Page 启动基本示例
Link.tsx
import * as React from 'react';
const STATUS = {
NORMAL: 'normal',
HOVERED: 'hovered',
};
interface theProps {
page:string
}
export default class Link extends React.Component<theProps,any> {
constructor(props) {
super(props);
this._onMouseEnter = this._onMouseEnter.bind(this);
this._onMouseLeave = this._onMouseLeave.bind(this);
this.state = {
class: STATUS.NORMAL,
};
}
_onMouseEnter() {
this.setState({class: STATUS.HOVERED});
}
_onMouseLeave() {
this.setState({class: STATUS.NORMAL});
}
render() {
return (
<a
className={this.state.class}
href={this.props.page || '#'}
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}>
{this.props.children}
</a>
);
}
}
test.tsx
import * as React from 'react';
import Link from '../app/Link';
import * as renderer from 'react-test-renderer';
it('Link changes the class when hovered', () => {
const component = renderer.create(
<Link page="http://www.facebook.com">Facebook</Link>
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
// manually trigger the callback
tree.props.onMouseEnter();
// re-renderingf
tree = component.toJSON();
expect(tree).toMatchSnapshot();
// manually trigger the callback
tree.props.onMouseLeave();
// re-rendering
tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
但即使使用jest 的测试运行良好,Webpack 和 IntelliJ 都抱怨tree.props.onMouseEnter(); 行:
Unresolved function or method onMouseLeave()
这是有道理的,因为 props 对象的类型为 { [propName: string]: string }
有什么可以包括跳过那些警告/错误消息的吗?
【问题讨论】:
标签: reactjs typescript intellij-idea webpack jestjs