[英]Angular 7 Jasmine unit test: how to get component NativeElement?
在我的主要组件html中有一个my-grid
组件
main.component.html
<my-grid
id="myDataGrid"
[config]="gridOptions"
</my-grid>
在main.component.specs.ts中,如何获取my-grid
NativeElement?
到目前为止,我有1个测试可以检查网格是否正在渲染
it('should render grid', () => {
fixture = TestBed.createComponent(MainComponent);
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('my-grid')).not.toBe(null);
})
您可以使用debugElement
来查询您的组件:
例
const debugElement = fixture.debugElement.query(By.directive(MyGridComponent));
const nativeElement = debugElement.nativeElement;
const component: MyGridComponent = debugElement.componentInstance;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.