我在我的应用程序中使用react-cookies包并尝试将测试写入我的应用程序。我正在尝试模拟cookie.remove
方法并对其进行验证,以下是代码:
// 应用程序.js
export class App extends Component {
static propTypes = {
cookies: PropTypes.instanceOf(Cookies).isRequired,
}
handleClick() {
// Remove data from browser cookies
this.props.cookies.remove('data', { path: '/' });
}
// 测试文件
it('should be able to remove cookies', () => {
const mockFn = jest.fn();
const cookies = { remove: mockFn };
const button = mount(<App cookies={cookies} />).find('button');
button.props().onClick();
expect(mockRemove).toHaveBeenCalledTimes(1);
expect(mockRemove).toHaveBeenCalledWith('data', { path: '/' });
});
测试运行正常并通过了,但是在控制台中有这个错误的 proptype 传递到 props 的警告:
console.error node_modules/react/node_modules/prop-types/checkPropTypes.js:20
Warning: Failed prop type: Invalid prop `cookies` of type `Object` supplied to `App`, expected instance of `Cookies`.
Cookies
在对方法进行存根时,如何将 的实例提供到我的测试中remove
?