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);
});
我的意思是,模擬( '點擊'),不要使用模擬功能。 –
我找到了解決方案。我沒有使用淺層,但從酶庫安裝。在那之後測試不失敗並且使用模擬功能。 –