docs(testing): port to Angular v4 (#3524)

This commit is contained in:
Jesús Rodríguez 2017-04-12 04:42:05 +02:00 committed by Ward Bell
parent 2287304c8b
commit 69294a25c2
7 changed files with 21 additions and 20 deletions

View File

@ -141,7 +141,7 @@ function createComponent() {
comp = fixture.componentInstance; comp = fixture.componentInstance;
const injector = fixture.debugElement.injector; const injector = fixture.debugElement.injector;
location = injector.get(Location); location = injector.get(Location) as SpyLocation;
router = injector.get(Router); router = injector.get(Router);
router.initialNavigation(); router.initialNavigation();
spyOn(injector.get(TwainService), 'getQuote') spyOn(injector.get(TwainService), 'getQuote')

View File

@ -34,7 +34,7 @@ describe('Angular async helper', () => {
// Use done. Cannot use setInterval with async or fakeAsync // Use done. Cannot use setInterval with async or fakeAsync
// See https://github.com/angular/angular/issues/10127 // See https://github.com/angular/angular/issues/10127
it('should run async test with successful delayed Observable', done => { it('should run async test with successful delayed Observable', (done: any) => {
const source = Observable.of(true).delay(10); const source = Observable.of(true).delay(10);
source.subscribe( source.subscribe(
val => actuallyDone = true, val => actuallyDone = true,

View File

@ -18,7 +18,7 @@ describe('FancyService without the TestBed', () => {
expect(service.getValue()).toBe('real value'); expect(service.getValue()).toBe('real value');
}); });
it('#getAsyncValue should return async value', done => { it('#getAsyncValue should return async value', (done: DoneFn) => {
service.getAsyncValue().then(value => { service.getAsyncValue().then(value => {
expect(value).toBe('async value'); expect(value).toBe('async value');
done(); done();
@ -26,7 +26,7 @@ describe('FancyService without the TestBed', () => {
}); });
// #docregion getTimeoutValue // #docregion getTimeoutValue
it('#getTimeoutValue should return timeout value', done => { it('#getTimeoutValue should return timeout value', (done: DoneFn) => {
service = new FancyService(); service = new FancyService();
service.getTimeoutValue().then(value => { service.getTimeoutValue().then(value => {
expect(value).toBe('timeout value'); expect(value).toBe('timeout value');
@ -35,7 +35,7 @@ describe('FancyService without the TestBed', () => {
}); });
// #enddocregion getTimeoutValue // #enddocregion getTimeoutValue
it('#getObservableValue should return observable value', done => { it('#getObservableValue should return observable value', (done: DoneFn) => {
service.getObservableValue().subscribe(value => { service.getObservableValue().subscribe(value => {
expect(value).toBe('observable value'); expect(value).toBe('observable value');
done(); done();

View File

@ -73,7 +73,7 @@ describe('use inject helper in beforeEach', () => {
})); }));
// Must use done. See https://github.com/angular/angular/issues/10127 // Must use done. See https://github.com/angular/angular/issues/10127
it('test should wait for FancyService.getObservableDelayValue', done => { it('test should wait for FancyService.getObservableDelayValue', (done: DoneFn) => {
service.getObservableDelayValue().subscribe(value => { service.getObservableDelayValue().subscribe(value => {
expect(value).toBe('observable delay value'); expect(value).toBe('observable delay value');
done(); done();
@ -187,20 +187,21 @@ describe('TestBed Component Tests', () => {
expect(selected).toHaveText(hero.name); expect(selected).toHaveText(hero.name);
}); });
it('can access the instance variable of an `*ngFor` row', () => { it('can access the instance variable of an `*ngFor` row component', () => {
const fixture = TestBed.createComponent(IoParentComponent); const fixture = TestBed.createComponent(IoParentComponent);
const comp = fixture.componentInstance; const comp = fixture.componentInstance;
const heroName = comp.heroes[0].name; // first hero's name
fixture.detectChanges(); fixture.detectChanges();
const heroEl = fixture.debugElement.query(By.css('.hero')); // first hero const ngForRow = fixture.debugElement.query(By.directive(IoComponent)); // first hero ngForRow
const ngForRow = heroEl.parent; // Angular's NgForRow wrapper element const hero = ngForRow.context['hero']; // the hero object passed into the row
expect(hero.name).toBe(heroName, 'ngRow.context.hero');
// jasmine.any is instance-of-type test. const rowComp = ngForRow.componentInstance;
expect(ngForRow.componentInstance).toEqual(jasmine.any(IoComponent), 'component is IoComp'); // jasmine.any is an "instance-of-type" test.
expect(rowComp).toEqual(jasmine.any(IoComponent), 'component is IoComp');
const hero = ngForRow.context['$implicit']; // the hero object expect(rowComp.hero.name).toBe(heroName, 'component.hero');
expect(hero.name).toBe(comp.heroes[0].name, '1st hero\'s name');
}); });
@ -343,7 +344,7 @@ describe('TestBed Component Tests', () => {
const childComp = el.componentInstance as BankAccountComponent; const childComp = el.componentInstance as BankAccountComponent;
expect(childComp).toEqual(jasmine.any(BankAccountComponent)); expect(childComp).toEqual(jasmine.any(BankAccountComponent));
expect(el.context).toBe(comp, 'context is the parent component'); expect(el.context).toBe(childComp, 'context is the child component');
expect(el.attributes['account']).toBe(childComp.id, 'account attribute'); expect(el.attributes['account']).toBe(childComp.id, 'account attribute');
expect(el.attributes['bank']).toBe(childComp.bank, 'bank attribute'); expect(el.attributes['bank']).toBe(childComp.bank, 'bank attribute');
@ -447,7 +448,7 @@ describe('TestBed Component Overrides:', () => {
// `inject` uses TestBed's injector // `inject` uses TestBed's injector
inject([FancyService], (s: FancyService) => testBedProvider = s)(); inject([FancyService], (s: FancyService) => testBedProvider = s)();
tcProvider = fixture.debugElement.injector.get(FancyService); tcProvider = fixture.debugElement.injector.get(FancyService);
tpcProvider = fixture.debugElement.children[0].injector.get(FancyService); tpcProvider = fixture.debugElement.children[0].injector.get(FancyService) as FakeFancyService;
expect(testBedProvider).not.toBe(tcProvider, 'testBed/tc not same providers'); expect(testBedProvider).not.toBe(tcProvider, 'testBed/tc not same providers');
expect(testBedProvider).not.toBe(tpcProvider, 'testBed/tpc not same providers'); expect(testBedProvider).not.toBe(tpcProvider, 'testBed/tpc not same providers');

View File

@ -12,7 +12,7 @@ describe('HeroDetailComponent - no TestBed', () => {
let hds: any; let hds: any;
let router: any; let router: any;
beforeEach( done => { beforeEach((done: any) => {
expectedHero = new Hero(42, 'Bubba'); expectedHero = new Hero(42, 'Bubba');
activatedRoute = new ActivatedRouteStub(); activatedRoute = new ActivatedRouteStub();
activatedRoute.testParams = { id: expectedHero.id }; activatedRoute.testParams = { id: expectedHero.id };
@ -45,7 +45,7 @@ describe('HeroDetailComponent - no TestBed', () => {
expect(router.navigate.calls.any()).toBe(false, 'router.navigate not called yet'); expect(router.navigate.calls.any()).toBe(false, 'router.navigate not called yet');
}); });
it('should navigate when click save resolves', done => { it('should navigate when click save resolves', (done: any) => {
comp.save(); comp.save();
// waits for async save to complete before navigating // waits for async save to complete before navigating
hds.saveHero.calls.first().returnValue hds.saveHero.calls.first().returnValue

View File

@ -90,7 +90,7 @@ function overrideSetup() {
beforeEach( async(() => { beforeEach( async(() => {
createComponent(); createComponent();
// get the component's injected HeroDetailServiceSpy // get the component's injected HeroDetailServiceSpy
hdsSpy = fixture.debugElement.injector.get(HeroDetailService); hdsSpy = fixture.debugElement.injector.get(HeroDetailService) as any;
})); }));
it('should have called `getHero`', () => { it('should have called `getHero`', () => {

View File

@ -78,7 +78,7 @@ describe('TwainComponent', () => {
// #enddocregion tests // #enddocregion tests
// #docregion done-test // #docregion done-test
it('should show quote after getQuote promise (done)', done => { it('should show quote after getQuote promise (done)', (done: any) => {
fixture.detectChanges(); fixture.detectChanges();
// get the spy promise and wait for it to resolve // get the spy promise and wait for it to resolve