简体   繁体   English

Angular 7 Jasmine单元测试:如何获取组件NativeElement?

[英]Angular 7 Jasmine unit test: how to get component NativeElement?

In my main component html there is a my-grid component 在我的主要组件html中有一个my-grid组件

main.component.html main.component.html

 <my-grid
    id="myDataGrid"
    [config]="gridOptions"
 </my-grid>

In main.component.specs.ts How can can I get my-grid NativeElement? 在main.component.specs.ts中,如何获取my-grid NativeElement?

I have 1 test so far which checks that grid is being rendered 到目前为止,我有1个测试可以检查网格是否正在渲染

it('should render grid', () => {
     fixture = TestBed.createComponent(MainComponent);
     const compiled = fixture.debugElement.nativeElement;
     expect(compiled.querySelector('my-grid')).not.toBe(null);
})

You can use the debugElement to query your component: 您可以使用debugElement来查询您的组件:

Example

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM