【发布时间】:2019-03-23 23:23:31
【问题描述】:
我们使用jest 进行模拟。 enzyme 用于在我们的应用程序中进行渲染。
在这里我试图模拟URLSearchParams 的get 方法。
我尝试使用
jest.spyOn(URLSearchParams, 'get');
但它不起作用。
我的反应类如下所示
export default class Concepts extends React.Component {
static getDerivedStateFromProps(props) {
const searchParams = new URLSearchParams(props.search);
return {
keyword: searchParams.get('q')
};
}
我的测试如下所示
it('should be able to change the state', () => {
jest.spyOn(URLSearchParams, 'get');
const wrapper = mount(
<Concepts search="test" />
);
});
这是正确的方法吗?还有其他方法吗?在此先感谢:)
【问题讨论】: