reactjs - 开玩笑/反应。如何在expect(setInterval).toHaveBeenLastCalledWith()中调用函数?
问题描述
因此,我需要在测试组件 throw Jest 环境期间进行function
回调。setInteval
但现在我总是得到an error message
以下字符串expect(setInterval).toHaveBeenLastCalledWith(expect.any(timer()), 20000)
:
预期的模拟函数最后一次调用时使用:未定义为参数 1,但它是使用 [Function anonymous] 调用的。
我真的无法理解如何将我的功能_startPoolingTimer
从组件传递到toHaveBeenLastCalledWith
......
我会很感激任何帮助
测试文件:
import React from 'react'
import { shallow, mount } from 'enzyme'
import initialState, { attemptButtonActive } from './mocks/row'
import Row from '../Row'
describe('<Row />', () => {
it('timer should start, make pooling request and after 20000 mc make pool request again', () => {
jest.useFakeTimers()
const Component = mount(<Row {...initialState} />)
const { poollingTimerId, count } = Component.instance().state
const timer = Component.instance()._startPoolingTimer
expect(count).toBe(1)
expect(poollingTimerId).toBeTruthy()
expect(setInterval).toHaveBeenCalledTimes(1)
expect(setInterval).toHaveBeenLastCalledWith(expect.any(timer()), 20000)
expect(count).toBe(9)
})
})
组件文件:
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
export class Row extends PureComponent {
constructor(props) {
super(props)
this.state = {
count: 0,
poollingTimerId: null
}
}
componentDidMount() {
let poollingTimerId = setInterval(this._startPoolingTimer, 20000)
}
_startPoolingTimer = () => {
const { startPooling, additionalInfoGlobal } = this.props
const moneyFromServer = additionalInfoGlobal.moneyToCollect
startPooling()
this.setState({
count: moneyFromServer
})
}
render() {
return (
<Row {...props}>
{'hh'}
</Row>
)
}
}
export default Row
解决方案
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import {connect} from 'react-redux';
export class Row4Collect extends PureComponent {
constructor(props) {
super(props)
this.state = {
count: 0,
poollingTimerId: null
}
}
componentDidMount() {
let poollingTimerId = setInterval(this.props._startPoolingTimer, 20000)
}
_startPoolingTimer = () => {
const { startPooling, additionalInfoGlobal } = this.props
const moneyFromServer = additionalInfoGlobal.moneyToCollect
startPooling()
this.setState({
count: moneyFromServer
})
}
render() {
return (
<Row {...props}>
{'hh'}
</Row>
)
}
}
const mapDispatchToProps = (dispatch) => {
return {
_startPoolingTimer: () => this._startPoolingTimer()
}
}
export default connect(undefined, mapDispatchToProps )(Row);
像这样更新它。
完成更新后,请使用 spy 作为道具并测试您的功能。
你的测试看起来像这样
import React from 'react'
import { shallow, mount } from 'enzyme'
import Row from '../Row'
describe('<Row />', () => {
it('timer should start, make pooling request and after 20000 mc make pool request again', () => {
const spyFn = jest.fn();
const Component = mount(<Row {...initialState} _startPoolingTimer= {spyFn} />)
expect(spyFn).toHaveBeenLastCalledWith();
})
})
我开玩笑地做到了这一点。
推荐阅读
- xpath - 如何找到 Chrome 插件元素的路径?
- jquery - 以编程方式调用 jquery 数据表单元格单击事件
- scala - 从嵌套的 for-comprehensions 处理堆叠的 State 和 IO monads 返回最终结果
- javascript - 使用来自 localStorage 的 jQuery eq() 填充一组文本区域
- sql - 报表中的 DoCmd.OpenReport 和 ORDER BY
- javascript - 如何修复此 for 循环以翻译数字
- tuples - 有条件地将元组写回excel(仅值> 0)
- javascript - 如何使用 converse.js 中的按钮单击事件在全屏和覆盖聊天之间切换
- java - 如何使用 Vitamio 播放加密的视频文件 (.mpg)?
- django - 使用 Django 的 Kendo UI 网格