2017-07-03 16 views
0

私はReactに単純な成分を持っています。ユーザーがボタンをクリックすると、このコンポーネントのメソッドをテストしたい。私はそれをテストしたが、最終的には合格しない。反応成分の試験方法が機能しない

コンポーネント:そのため

import React, { Component } from 'react'; 
import PropTypes from 'prop-types'; 
import axios from 'axios'; 

class TestInvokingMethod extends Component { 
    onClick() { 
    } 
    render() { 
     return (
      <div> 
       <input id='buttonTest' type='button' value={10} onClick={this.onClick} /> 
      </div> 
     ); 
    } 
} 

export default TestInvokingMethod; 

とテスト:

import React from 'react'; 
import { shallow } from 'enzyme'; 
import TestInvokingMethod from '../../components/TestComponent/TestInvokeMethod'; 

const component = shallow(
    <TestInvokingMethod /> 
); 

test('Testing invoke method',() => { 

    const mockFn = jest.fn(); 

    component.instance().onClick = mockFn; 
    component.update(); 
    component.find('#buttonTest').simulate('click'); 

    expect(component.instance().onClick.mock.calls.length).toBe(1); 
}); 
+0

を使用してみてくださいモック関数。 –

+0

私は解決策を見つけました。私は浅いが酵素ライブラリーからマウントしていない。その後、テストは失敗せず、模擬機能が使用されます。 –

答えて

0

は使用しません( 'クリック')私は何を意味すると、そのシミュレートされ冗談のSpyOn

const spy = expect.spyOn(wrapper.instance(), "onClick"); 
wrapper.update(); 
wrapper.find('#buttonTest').simulate('click'); 
expect(spy).toHaveBeenCalled(); 
関連する問題