首页 > 解决方案 > 重定向类中断终端 - 测试

问题描述

我正在尝试使用 Jest 和 Ezyme 运行测试

Editworkflow 是一种状态,它正在重定向到另一个位置。如果我将它设置为 true,它会破坏终端。

经过一些研究,我能够做到以下几点

describe("when user has permission", () => {
  beforeEach(() => {
  wrapper.setState({editWorkflow: true, selectedWorkId:true });
  wrapper.instance().history.replace('/add'); // replace the MemoryRouter location with 
  wrapper.update(); // update the wrapper with the changes
  })

it("Test click event on Workflows - Cancel/AddWork", () => {
  baseProps.onClick.mockClear();
  expect(wrapper.containsMatchingElement(<Redirect to="/add" />)).toEqual(true)
  expect(wrapper.containsMatchingElement(<Redirect to="/edit/" />)).toEqual(true)
  wrapper.find('Workflows').find('#add-work-button').at(0).simulate("click");
  wrapper.find('Workflows').find('#add-work-button').at(1).simulate("click");
  );

目前终端没有损坏,但测试也没有通过。

这是错误:

 expect(received).toEqual(expected)

 Expected value to equal:
   true
 Received:
   false

 expect(wrapper.containsMatchingElement(<Redirect to="/add"/>)).toEqual(true) 

如果我将上面的语句设置为 False,则终端再次中断

这是状态调用重定向的地方

{ this.state.editWorkflow ? <Redirect push to={`${this.props.location.pathname}/add`} /> : null }
{ this.state.selectedWorkId ? <Redirect push to={`${this.props.location.pathname}/edit/${this.state.selectedWorkId}`} /> : null }

我如何使用 Jest 和 Enzyme 编写以下测试并防止这种情况发生

标签: reactjsunit-testingjestjs

解决方案


推荐阅读