arunselvakumar / AngularNotes

0 stars 0 forks source link

Angular Testing #2

Open arunselvakumar opened 5 years ago

arunselvakumar commented 5 years ago

import { TestBed } from '@angular/core/testing';

arunselvakumar commented 5 years ago

Creating a dynamic testing module. TestBed.configureTestingModule({ declarations: [VotingComponent] });

Straight out of NgModule - A Ripoff

arunselvakumar commented 5 years ago

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 It's a wrapper around the component, so that it provides access to the template

arunselvakumar commented 5 years ago

let component: VoterComponent; let componentFixture: ComponentFixture

arunselvakumar commented 5 years ago

componentFixture = TestBed.createComponent(VoterComponent); component = componentFixture.componentInstance();

arunselvakumar commented 5 years ago

componentFixture.nativeElement // exposes the full DOM componentFixutre.debugElement // allows to query.

arunselvakumar commented 5 years ago

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;

arunselvakumar commented 5 years ago

expect(el.innerText).toContain(21);

arunselvakumar commented 5 years ago

fixture.detectChanges();

arunselvakumar commented 5 years ago

fixture.debugElement.query(By.directive(VoterComponent));

arunselvakumar commented 5 years ago

var button = fixture.debugElement.query(By.directive(VoterComponent)); button.triggerEventHandler('click', null); // Assert