【发布时间】:2021-04-18 10:01:09
【问题描述】:
我有反应应用程序并开始用单元测试覆盖它。 我的应用文件包含:
export default class App extends Component {
render() {
return (
<Provider store={store}>
<Router history={history}>
<div>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/detail/:collectionName/:RecordId/:firmId" component={DetailDialog} />
<Route exact path="/dashboard" component={Dashboard} />
<Route path="/auth-login" component={LogIn} />
<Route path="/auth-register" component={Register} />
<Route path="/user-manager" component={this.props.decoded.role==='Admin' ? Admin : Home} />
</Switch>
</div>
</Router>
</Provider>
);
}
}
我要做下一个测试:去登录,检查href和通过href检查组件。
it.only('test right component is attached', () => {
const wrapper = shallow(<Login loginUser={fn}/>);
const registerLink = wrapper.find("[data-qa='sign-up']").props().href;
const history = jest.fn();
const props = {
decoded: {role: 'Admin'}
}
const router = mount(
<MemoryRouter initialEntries={[registerLink]}>
<App history={history} {...props} />
</MemoryRouter>
);
expect(router.find(Register)).toHaveLength(1);
});
直到安装一切正常。但是在安装时我收到下一个错误:
控制台错误 错误:未捕获 [错误:无法在未安装的组件上找到节点。]
我做错了什么?如何挂载组件并检查 Register 是否在其中?
【问题讨论】: