我有一个带有函数的module:
const utils = {
redirectTo(url) {
if (url) {
window.location = url;
}
},
};
export default utils;
它在 React 组件中的某处使用,如下所示:
import utils from '../../lib/utils';
componentWillUpdate() {
this.redirectTo('foo')
}
现在我想检查redirectTo
使用 equals 调用的值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: [] }
});
这就是我所拥有的,但它不起作用。我该怎么做呢?