如何在redux app中对addTodo进行单元测试?

时间:2017-09-14 05:06:00

标签: reactjs unit-testing

我正在尝试这个例子:

https://github.com/reactjs/redux/tree/master/examples/todomvc

基于此解决方案,我创建了一个单元测试,如下所示:

it('should call addTodo if length of text is greater than 0', () => {

    const props = {
      addTodo: jest.fn()
    }

    let cb = shallow(<Header {...props} />)

    expect(props.addTodo).not.toBeCalled()

    cb.find('TodoTextInput').simulate("onSave",{text:"fsdhsd"});

    //error starts here:
    expect(props.addTodo).toBeCalled()


  });

这个的结果是: FAIL src / components / NewHeaderTest.spec.js   ●

Header enzyme style › should call addTodo if length of text is greater than 0

    expect(jest.fn()).toBeCalled()

    Expected mock function to have been called.

    at Object.it (src/components/NewHeaderTest.spec.js:46:27)

这是组件的一部分:

handleSave = text => {
    console.log('handlesave');
    if (text.length > 0) {
      this.props.addTodo(text);
    }
  }

  render = () => {
    return (
      <header className="header">
        <TodoTextInput onSave={this.handleSave}>
        </TodoTextInput>
      </header>)
  }

如何修复unittest或如何为simulate语句传递参数:cb.find(&#39; TodoTextInput&#39;)。模拟(&#34; onSave&#34;,{text:& #34; fsdhsd&#34;});

0 个答案:

没有答案