尝试使用开玩笑的末端酶测试click和onchange事件,但我一直遇到此错误:
TypeError: Cannot read property 'getElementsByTagName' of null
不确定我的道具是否错误,因为其渲染正确。当我尝试单击或更改测试时,错误会不断指出我的setState
这是我的组件
componentDidUpdate(prevProps, prevState, snapshot) {
var el = document.getElementById('myTable');
var firstHeader = document.getElementById('myTable').getElementsByTagName( 'thead' )[0].getElementsByTagName( 'tr' )[0].getElementsByTagName( 'th' )[0];
if(firstHeader) {
var dragger = tableDragger(el, {
mode: 'column',
dragHandler: '.handle',
onlyBody: true,
animation: 300
});
dragger.on('drop',(from, to, el) => {
this.props.onHeaderDrop(
document.getElementById('myTable').getElementsByTagName( 'thead' )[0].getElementsByTagName( 'tr' )[0].getElementsByClassName('handle')[from].getAttribute('id'),
from + 1,
document.getElementById('myTable').getElementsByTagName( 'thead' )[0].getElementsByTagName( 'tr' )[0].getElementsByClassName('handle')[to].getAttribute('id'),
to + 1
)
});
}
这是我嘲笑道具之一的方式: 模拟功能
const onHeaderDrop = jest.fn();
const baseProps = {
onHeaderDrop,
beforeEach(() => wrapper = shallow(<Shape {...baseProps} />));