tl/dr:如何等待异步
componentDidMount
我有一个反应组件
组件didmount
在我的酶/摩卡测试中,我可以等待
组件didmount
it('should show an input with quantity 0 by default', async () => {
const component = shallow(<ShowPage />);
await component.instance().componentDidMount();
component.update();
expect(component.find('h1').text()).to.eq('Show page');
});
这很好,但是我还想测试一个按钮点击,它将用户重定向到另一个页面。它使用:
this.props.history.push('/redirect-to')
history
道具不可用,除非我用
MemoryRouter
const component = mount(<MemoryRouter><ShowPage /></MemoryRouter>);
但现在我无法访问
ShowPage
组件didmount
因为:
await component.find('BurgerShow').instance().componentDidMount();
instance() can only be called on the root
我觉得我的天气很糟糕。正确的方法是什么?