Open arunselvakumar opened 5 years ago
Creating a dynamic testing module. TestBed.configureTestingModule({ declarations: [VotingComponent] });
Straight out of NgModule - A Ripoff
To Create a instance of this component, We call
TestBed.createComponent(VoterComponent); // It doesn't return an instance of the component, it returns a component fixture. ComponentFixture
let component: VoterComponent;
let componentFixture: ComponentFixture
componentFixture = TestBed.createComponent(VoterComponent); component = componentFixture.componentInstance();
componentFixture.nativeElement // exposes the full DOM componentFixutre.debugElement // allows to query.
import { By } from '@angular/platform-browser'; fixture.debugElement.query(By.css('.value')); // returns the first element of the list. fixture.debugElement.queryAll(By.css('.value')); // returns all the matched items from the list.
let el: HTMLElement = de.nativeElement;
expect(el.innerText).toContain(21);
fixture.detectChanges();
fixture.debugElement.query(By.directive(VoterComponent));
var button = fixture.debugElement.query(By.directive(VoterComponent)); button.triggerEventHandler('click', null); // Assert
import { TestBed } from '@angular/core/testing';