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);
});
を使用してみてくださいモック関数。 –
私は解決策を見つけました。私は浅いが酵素ライブラリーからマウントしていない。その後、テストは失敗せず、模擬機能が使用されます。 –