Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用react包装器测试window.location.assign?_Reactjs_Enzyme - Fatal编程技术网

Reactjs 如何使用react包装器测试window.location.assign?

Reactjs 如何使用react包装器测试window.location.assign?,reactjs,enzyme,Reactjs,Enzyme,我有一个react组件,它根据somecondition的值导航到不同的页面。例如: if (somecondition) { window.location.assign('some link')} } 如何使用enzymewrapper测试这个somecondition?使用enzyme和jest,您的测试可能如下所示: test('should redirect', () => { // SETUP window.location.assign = jest.fn()

我有一个
react
组件,它根据somecondition的值导航到不同的页面。例如:

if (somecondition) {
 window.location.assign('some link')}
}

如何使用
enzyme
wrapper测试这个somecondition

使用enzyme和jest,您的测试可能如下所示:

test('should redirect', () => {
  // SETUP
  window.location.assign = jest.fn()

  // EXECUTE
  const wrapper = shallow(<Component {...props} />)
  wrapper.instance().callYourFunction()

  // VERIFY
  expect(window.location.assign).toHaveBeenCalled()
  window.location.assign.mockClear()
})
test('应该重定向',()=>{
//设置
window.location.assign=jest.fn()
//执行
常量包装器=浅()
wrapper.instance().callYourFunction()
//核实
expect(window.location.assign).toHaveBeenCalled()
window.location.assign.mockClear()
})

只需模拟assign函数并测试模拟函数的调用