我想测试在模拟按钮单击时调用了从 mapDispatchToProps 传递的函数。
如何测试从 mapDispatchToProps 传递的函数被调用?
我试图通过道具传递一个模拟函数,但它不起作用。任何帮助将不胜感激。
下面是我的假类代码和测试示例。
我的组件
// All required imports
class App extends React.Component<Props> {
render() {
const { onClick } = this.props;
return (
<>
<h1>Form</h1>
<input />
<button onClick={() => onClick()} />
</>
);
}
}
const mapStateToProps = (state) => {
return {
state
};
};
const mapDispatchToProps = (dispatch) => {
return {
onClick: () => dispatch(actions.onClick())
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
Run Code Online (Sandbox Code Playgroud)
我的测试文件
import { configure, mount } from 'enzyme';
import Adapter …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Flow 类型连接到 Redux 存储的组件。我用:
"flow-bin": "^0.89.0",
"flow-typed": "^2.5.1",
"react-redux": "^5.0.7",
Run Code Online (Sandbox Code Playgroud)
我为 mapStateToProps 和 mapDispatchToProps 函数设置了类型
我的 index.js
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import type { Dispatch } from 'redux';
import type { DispatchProps, StateProps, NotificationItem, NotificationsProps, State, OwnProps } from './index.types';
import { createNotificationAction, createRemoveNotificationAction } from './redux';
import View from './View';
function Notifications(props: NotificationsProps) {
return <View {...props} />;
}
const mapStateToProps = ({ notifications }: State): StateProps => …Run Code Online (Sandbox Code Playgroud)