我正在尝试测试componentDidMount()
。当我尝试安装组件时,我收到一个空实例。当我浅浅渲染它时,从未调用componentDidMount,因为我无法安装它。
我的代码:
class SingleOrderContainer extends Component {
constructor(props) {
super(props)
this.toggle = this.toggle.bind(this)
let initialOrderTab
if(props.location && props.location.state) {
initialOrderTab = props.locationa.state.followUpOrder
}
this.state = {
activeTab: '1',
activeOrderTab: initialOrderTab || 'primary',
firstOrderLoaded: false,
followUpOrderTabs: [],
selectedDoc: null,
noOrderFound: false,
viewPdf: false,
isDragOver: false
}
props.fees.filter()
}
componentDidMount() {
window.addEventListener('dragover', (e) => {
e = e || event
e.preventDefault()
}, false)
window.addEventListener('drop', (e) => {
e = e || event
e.preventDefault()
}, false)
}
我的测试:
it('componentDidUpdate', () => {
// Shoutout to Connect(Prefetch) for this awesome next line of 100000 dives.
const shallowWrap = shallow(<Router>
<SingleOrderContainer {...props} store={store}/>
</Router>
).dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive()
const componentDidMountSpy = jest.spyOn(shallowWrap.instance(), 'componentDidMount')
mount(<Router><Provider store={store}>{shallowWrap.get(0)}</Provider></Router>)
expect(componentDidMountSpy).toHaveBeenCalledTimes(1)
})
(。dive()垃圾邮件归因于Connect(PrefetchResourceContainer))
非常感谢您的帮助:)