react-redux - 如何开玩笑测试在componentDidMount中有api调用的连接组件
问题描述
我正在尝试测试一个连接的组件,但它似乎没有在 componentDidMount 函数中进行 api 调用。我需要它来进行 api 调用,这样我就可以根据 api 调用返回的值来测试这个组件将如何呈现。api 调用由 axios 使用 redux 操作进行。一切都存储在 redux 中。
这是我的测试
it('should dispatch an action on mount', () => {
const component = shallow(
<ProcessingStatus store={store}/>
);
const didMount = jest.spyOn(component, 'componentDidMount');
expect(didMount).toHaveBeenCalledTimes(1);
//console.log(component.html())
expect(store.dispatch).toHaveBeenCalledTimes(3);
});
这是componentDidMount
我的组件
componentDidMount() {
const {
matches: { params: { id } },
processingStatus,
securityStatus,
routingStatus,
soxStatus,
remedyStatus,
user: {
priv: {
my_sox_requests,
view_remedy
}
}
} = this.props;
let params = 'id=' + id;
if(processingStatus !== undefined){
processingStatus(params)
.catch(thrown => {
console.log(thrown);
});
}
if(securityStatus !== undefined){
securityStatus(params)
.catch(thrown => {
console.log(thrown);
});
}
if(routingStatus !== undefined){
routingStatus(params)
.catch(thrown => {
console.log(thrown);
});
}
if(my_sox_requests && my_sox_requests === 'on' && soxStatus !== undefined){
soxStatus(params)
.catch(thrown => {
console.log(thrown);
});
}
if(view_remedy && view_remedy === 'on' && remedyStatus !== undefined){
remedyStatus(params)
.catch(thrown => {
console.log(thrown);
});
}
}
我得到的错误是
FAIL tests/jest/components/common/ProcessingStatus/index.test.js
<ProcessingStatus />
✓ should render with given state from Redux store (90ms)
✕ should dispatch an action on mount (7ms)
● <ProcessingStatus /> › should dispatch an action on mount
Cannot spy the componentDidMount property because it is not a function; undefined given instead
85 | <ProcessingStatus store={store}/>
86 | );
> 87 | const didMount = jest.spyOn(component, 'componentDidMount');
| ^
88 | expect(didMount).toHaveBeenCalledTimes(1);
89 |
90 | //console.log(component.html())
at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:841:15)
at Object.<anonymous> (tests/jest/components/common/ProcessingStatus/index.test.js:87:31)
我试过了const didMount = jest.spyOn(ProcessingStatus.prototype, 'componentDidMount');
,我得到的错误是
● <ProcessingStatus /> › should dispatch an action on mount
expect(jest.fn()).toHaveBeenCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
85 | );
86 | const didMount = jest.spyOn(ProcessingStatus.prototype, 'componentDidMount');
> 87 | expect(didMount).toHaveBeenCalledTimes(1);
| ^
88 |
89 | //console.log(component.html())
90 | expect(store.dispatch).toHaveBeenCalledTimes(3);
我设法测试了是否调用了 didmount 但不确定如何检查是否已进行 api 调用。
it('should run componentDidMount', () => {
spy = jest.spyOn(ProcessingStatus.prototype, 'componentDidMount');
component = mount(
<ProcessingStatus store={store}/>
);
expect(spy).toHaveBeenCalledTimes(1);
});
解决方案
我一直在这里搜索类似的问题,发现您有一些订单错误:
- 你应该在 shallow(Component) 之前设置 spyOn componentDidMount
- 你应该要求在浅组件之后调用 Component.prototype.componentDidMount
jest.spyOn(Component.prototype, 'componentDidMount')
shallow(<Component/>)
expect(Component.prototype.componentDidMount).toHaveBeenCalled();
如果组件希望接收将在 componentDidMount 内部调用的 props 函数,则应在浅组件时添加它们,例如
const mockExpectedFunction= jest.fn()
shallow(<Component expectedFunction={mockExpectedFunction} />
推荐阅读
- dictionary - 如何更新地图中的结构属性
- python - Python 列表有列吗?
- python - 为什么python中的csv只写最后一行?
- php - php 7 调用未定义函数 bcpowmod()
- javascript - 十进制格式验证
- java - 知道如何解决这个问题吗?线程“WindowsNativeRunloopThread”中的异常 java.lang.NoSuchMethodError
- python-3.x - 从excel表制作python字典
- c# - Button_Clicked 事件操作特定元素(纯 C# (Page.cs))
- android - Android 未经检查的分配
- forms - 您如何访问 Gravity Form 字段预提交?