Reputation: 1035
I'm currently putting together some best practices for testing Angular 2 apps on a component level.
I've seen a few tutorials query a fixture's NativeElement object for selectors and the like, e.g.
it('should render "Hello World!" after click', async(() => {
builder.createAsync(HelloWorld).then((fixture: ComponentFixture<HelloWorld>) => {
fixture.detectChanges();
let el = fixture.nativeElement;
el.querySelector('h1').click();
fixture.detectChanges();
expect(el.querySelector('h1')).toHaveText('Hello World!');
});
}));
However, in juliemr's Angular 2 test seed she accesses the NativeElement through a parent DebugElement object.
it('should render "Hello World!" after click', async(() => {
builder.createAsync(HelloWorld).then((fixture: ComponentFixture<HelloWorld>) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
compiled.querySelector('h1').click();
fixture.detectChanges();
expect(compiled.querySelector('h1')).toHaveText('Hello World!');
});
}));
Are there any specific cases you'd use a fixture's debugElement.nativeElement over its nativeElement?
Upvotes: 93
Views: 45723
Reputation: 256
.nativeElement()
returns DOM tree whereas debugElement
returns a JS object (debugElement tree). debugElement
is a Angular's method.
.nativeElement()
is Browser specific API that returns or give access to DOM tree. But what if application is running on non-browser platform (such as server or web-worker), in that case .nativeElement()
may throw error.
If we are sure that our application will run on browser only, then unhesitantly we can use
let el = fixture.nativeElement
. But if we are not sure about the platform then to be on safer side uselet le = fixture.debugElement
because it returns a plain JS Object.
Upvotes: 10
Reputation: 4359
to add on to what has been mentioned already :
abstract class ComponentFixture {
debugElement; // test helper
componentInstance; // access properties and methods
nativeElement; // access DOM
detectChanges(); // trigger component change detection
}
Upvotes: 37
Reputation: 657546
nativeElement
returns a reference to the DOM elementDebugElement
is an Angular2 class that contains all kinds of references and methods relevant to investigate an element or component (See the source of DebugNode and DebugElementUpvotes: 72
Reputation: 6802
Take a look at Angular discussion about this topic and related PR.
Mainly:
fixture.componentInstance == fixture.debugElement.componentInstance;
fixture.nativeElement == fixture.debugElement.nativeElement;
Upvotes: 21