【发布时间】:2017-03-29 01:04:17
【问题描述】:
我有这个模块,里面有一个函数:
const utils = {
redirectTo(url) {
if (url) {
window.location = url;
}
},
};
export default utils;
它在 React 组件中的某处使用,如下所示:
import utils from '../../lib/utils';
componentWillUpdate() {
this.redirectTo('foo')
}
现在我想检查调用redirectTo 的值是否等于foo。
it('should redirect if no rights', () => {
const mockRedirectFn = jest.fn();
utils.redirectTo = mockRedirectFn;
mount(
<SomeComponent />,
);
expect(mockRedirectFn).toBeCalled();
expect(mockRedirectFn).toBeCalledWith('foo');
console.log(mockRedirectFn.mock);
// { calls: [], instances: [] }
});
这就是我所拥有的,但它不起作用。我该怎么做?
【问题讨论】:
-
组件在哪里调用了函数?
-
componentWillUpdate,也会更新描述
标签: javascript reactjs unit-testing jestjs