我正在测试一个React组件,并需要模拟单击组件内特定元素。 我不想仅为测试而添加id
,是否有一种通过文本选择此元素的方法?
const ReactTestUtils = require('react-test-utils');
const Sidebar = require('components/sidebar');
describe('clicking on More button', function() {
it('triggers analytics event', function() {
const component = renderComponent(<Sidebar policyPreferences={ this.policyPreferences } />);
const moreButton = component.getDOMElementByText('More'); // <- I need something like this
ReactTestUtils.Simulate.click(moreButton);
expect(analytics.track).toHaveBeenCalled();
}
}
.innerHTML()
选择那个元素吗? - Petr Gazarov