2017-02-04 7 views
0

私はバインドされたメソッド(https://babeljs.io/docs/plugins/transform-class-properties/)でシノンを使用するのにいくつかの問題があります。バインドされたReactコンポーネントメソッドでsinonを使用するにはどうすればよいですか?

どのようにスパイをつけるのですか? https://gist.github.com/stevens32/b5eee5cc1781a687be03bf80ce8425e0

になり:

bound method spying 
    √ should be an instance of FormComponent 
    should spy on boundChangeInput 
    √ should have calledOnce prop on boundChangeInput from spy 
    1) should have boundChangeInput.calledOnce true on simulated input change 
    √ has the correct value 
    should spy on notBoundChangeInput 
    √ should have calledOnce prop on notBoundChangeInput from spy 
    √ should have notBoundChangeInput.calledOnce true on simulated input change 
    √ has the correct value 

6 passing (133ms) 
    1 failing 

    1) bound method spying should spy on boundChangeInput should have boundChangeInput.calledOnce true on 
mulated input change: 

    AssertionError: expected false to equal true 
    + expected - actual 

    -false 
    +true 
+1

私は方法を見つけた、と誰かがこれをdownvoteする理由 –

+0

誰もが知っているコンポーネントのコンストラクタ内の関数をバインドすることにしましたいませんでしたか? – stevens32

答えて

2

あなたが最初のコンポーネントのインスタンスを作成する必要があるかもしれませんここで要点です。これを試してみてください:

describe('should have boundChangeInput.calledOnce true on simulated input change', function() { 

    const node = mount(<FormComponent />) 
    const component = wrapper.instance() 

    let boundChangeSpy = sinon.spy(node, 'boundChangeInput') 

    component.forceUpdate() 
    wrapper.update() 

    wrapper.find('input').at(0).simulate('change',{target:{value:'some value'}}) 

    expect(node.boundChangeSpy.calledOnce).to.equal(true) 
}) 

出典:Test custom method on React component has been called, using Enzyme and Sinon

+0

ありがとう!私はwrapper.instance()を使ってみましたが、それもうまくいきませんでした - reactInstance.forceUpdate()とwrapper.update()のように見えました。 – stevens32

関連する問題