Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

chore: update testing example to RC4 #1839

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions public/docs/_examples/testing/ts/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import {
beforeEach, beforeEachProviders,
describe, ddescribe, xdescribe,
expect, it, iit, xit,
async, inject
} from '@angular/core/testing';

import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing';

import { Hero, HeroService, MockHeroService } from './mock-hero.service';

Expand Down Expand Up @@ -45,7 +42,7 @@ describe('AppComponent', () => {
it('can get title from template', () => {
fixture.detectChanges();
let titleEl = fixture.debugElement.query(By.css('h1')).nativeElement;
expect(titleEl).toHaveText(comp.title);
expect(titleEl.textContent).toContain(comp.title);
});

it('can get RouterLinks from template', () => {
Expand Down
16 changes: 7 additions & 9 deletions public/docs/_examples/testing/ts/app/bad-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

import {
beforeEach, beforeEachProviders,
describe, ddescribe, xdescribe,
expect, it, iit, xit,
addProviders,
async, inject
} from '@angular/core/testing';

import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing';

import { ViewMetadata } from '@angular/core';
import { Observable } from 'rxjs/Rx';
Expand Down Expand Up @@ -143,20 +141,20 @@ xdescribe('async & inject testing errors', () => {
restoreJasmineIt();
}, 10000);

describe('using beforeEachProviders', () => {
beforeEachProviders(() => [{ provide: FancyService, useValue: new FancyService() }]);
describe('using addProviders', () => {
addProviders([{ provide: FancyService, useValue: new FancyService() }]);

beforeEach(
inject([FancyService], (service: FancyService) => { expect(service.value).toEqual('real value'); }));

describe('nested beforeEachProviders', () => {
describe('nested addProviders', () => {

it('should fail when the injector has already been used', () => {
patchJasmineBeforeEach();
expect(() => {
beforeEachProviders(() => [{ provide: FancyService, useValue: new FancyService() }]);
addProviders([{ provide: FancyService, useValue: new FancyService() }]);
})
.toThrowError('beforeEachProviders was called after the injector had been used ' +
.toThrowError('addProviders was called after the injector had been used ' +
'in a beforeEach or it block. This invalidates the test injector');
restoreJasmineBeforeEach();
});
Expand Down
91 changes: 25 additions & 66 deletions public/docs/_examples/testing/ts/app/bag.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Based on https://github.com/angular/angular/blob/master/modules/angular2/test/testing/testing_public_spec.ts
/* tslint:disable */
import {
BadTemplateUrl, ButtonComp,
ButtonComp,
ChildChildComp, ChildComp, ChildWithChildComp,
ExternalTemplateComp,
FancyService, MockFancyService,
Expand All @@ -16,62 +16,19 @@ import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

import {
beforeEach, beforeEachProviders,
describe, ddescribe, xdescribe,
expect, it, iit, xit,
async, inject,
addProviders,
inject, async,
fakeAsync, tick, withProviders
} from '@angular/core/testing';

import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing';

import { ViewMetadata } from '@angular/core';

import { Observable } from 'rxjs/Rx';

//////// SPECS /////////////

/// Verify can use Angular testing's DOM abstraction to access DOM

describe('angular2 jasmine matchers', () => {
describe('toHaveCssClass', () => {
it('should assert that the CSS class is present', () => {
let el = document.createElement('div');
el.classList.add('bombasto');
expect(el).toHaveCssClass('bombasto');
});

it('should assert that the CSS class is not present', () => {
let el = document.createElement('div');
el.classList.add('bombasto');
expect(el).not.toHaveCssClass('fatias');
});
});

describe('toHaveCssStyle', () => {
it('should assert that the CSS style is present', () => {
let el = document.createElement('div');
expect(el).not.toHaveCssStyle('width');

el.style.setProperty('width', '100px');
expect(el).toHaveCssStyle('width');
});

it('should assert that the styles are matched against the element', () => {
let el = document.createElement('div');
expect(el).not.toHaveCssStyle({width: '100px', height: '555px'});

el.style.setProperty('width', '100px');
expect(el).toHaveCssStyle({width: '100px'});
expect(el).not.toHaveCssStyle({width: '100px', height: '555px'});

el.style.setProperty('height', '555px');
expect(el).toHaveCssStyle({height: '555px'});
expect(el).toHaveCssStyle({width: '100px', height: '555px'});
});
});
});

describe('using the async helper', () => {
let actuallyDone = false;

Expand Down Expand Up @@ -101,7 +58,7 @@ describe('using the async helper', () => {
p.catch(() => { actuallyDone = true; });
}));

it('should run async test with successful Observable', async(() => {
xit('should run async test with successful Observable', async(() => {
let source = Observable.of(true).delay(10);
source.subscribe(
val => {},
Expand All @@ -114,9 +71,11 @@ describe('using the async helper', () => {
describe('using the test injector with the inject helper', () => {

describe('setting up Providers with FancyService', () => {
beforeEachProviders(() => [
{ provide: FancyService, useValue: new FancyService() }
]);
beforeEach(() => {
addProviders([
{ provide: FancyService, useValue: new FancyService() }
]);
});

it('should use FancyService',
inject([FancyService], (service: FancyService) => {
Expand All @@ -142,7 +101,7 @@ describe('using the test injector with the inject helper', () => {
);
})));

it('test should wait for FancyService.getObservableDelayValue',
xit('test should wait for FancyService.getObservableDelayValue',
async(inject([FancyService], (service: FancyService) => {
service.getObservableDelayValue().subscribe(
value => { expect(value).toEqual('observable delay value'); }
Expand Down Expand Up @@ -197,7 +156,7 @@ describe('test component builder', function() {

tcb.createAsync(ChildComp).then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('Original Child');
expect(fixture.nativeElement.textContent).toContain('Original Child');
});
})));

Expand All @@ -206,11 +165,11 @@ describe('test component builder', function() {

tcb.createAsync(MyIfComp).then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('MyIf()');
expect(fixture.nativeElement.textContent).toContain('MyIf()');

fixture.debugElement.componentInstance.showMore = true;
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('MyIf(More)');
expect(fixture.nativeElement.textContent).toContain('MyIf(More)');
});
})));

Expand Down Expand Up @@ -262,7 +221,7 @@ describe('test component builder', function() {
.createAsync(MockChildComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('Mock');
expect(fixture.nativeElement.textContent).toContain('Mock');
});
})));

Expand All @@ -276,7 +235,7 @@ describe('test component builder', function() {
.createAsync(ChildComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('Modified Child');
expect(fixture.nativeElement.textContent).toContain('Modified Child');

});
})));
Expand All @@ -288,7 +247,7 @@ describe('test component builder', function() {
.createAsync(ParentComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('Parent(Mock)');
expect(fixture.nativeElement.textContent).toContain('Parent(Mock)');

});
})));
Expand All @@ -302,8 +261,8 @@ describe('test component builder', function() {
.createAsync(ParentComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement)
.toHaveText('Parent(Original Child(ChildChild Mock))');
expect(fixture.nativeElement.textContent)
.toContain('Parent(Original Child(ChildChild Mock))');

});
})));
Expand All @@ -318,8 +277,8 @@ describe('test component builder', function() {
.createAsync(TestProvidersComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement)
.toHaveText('injected value: mocked out value');
expect(fixture.nativeElement.textContent)
.toContain('injected value: mocked out value');
});
})));

Expand All @@ -333,8 +292,8 @@ describe('test component builder', function() {
.createAsync(TestViewProvidersComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement)
.toHaveText('injected value: mocked out value');
expect(fixture.nativeElement.textContent)
.toContain('injected value: mocked out value');
});
})));

Expand All @@ -344,8 +303,8 @@ describe('test component builder', function() {
tcb.createAsync(ExternalTemplateComp)
.then(fixture => {
fixture.detectChanges();
expect(fixture.nativeElement)
.toHaveText('from external template\n');
expect(fixture.nativeElement.textContent)
.toContain('from external template\n');
});
})), 10000); // Long timeout because this test makes an actual XHR.

Expand Down
16 changes: 7 additions & 9 deletions public/docs/_examples/testing/ts/app/dashboard.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { DashboardComponent } from './dashboard.component';
import { By } from '@angular/platform-browser';

import {
beforeEach, beforeEachProviders,
describe, ddescribe, xdescribe,
expect, it, iit, xit,
addProviders,
async, inject
} from '@angular/core/testing';

import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing';

import { Hero, HeroService, MockHeroService } from './mock-hero.service';
import { Router, MockRouter } from './mock-router';
Expand Down Expand Up @@ -70,13 +68,13 @@ describe('DashboardComponent', () => {
let comp: DashboardComponent;
let mockHeroService: MockHeroService;

beforeEachProviders(() => {
beforeEach(() => {
mockHeroService = new MockHeroService();
return [
addProviders([
{ provide: Router, useClass: MockRouter},
{ provide: MockRouter, useExisting: Router},
{ provide: HeroService, useValue: mockHeroService }
];
]);
});

it('can instantiate it',
Expand Down Expand Up @@ -138,8 +136,8 @@ describe('DashboardComponent', () => {
expect(heroNames.length).toEqual(4, 'should display 4 heroes');

// the 4th displayed hero should be the 5th mock hero
expect(heroNames[3].nativeElement)
.toHaveText(mockHeroService.mockHeroes[4].name);
expect(heroNames[3].nativeElement.textContent)
.toContain(mockHeroService.mockHeroes[4].name);
});

});
Expand Down
9 changes: 0 additions & 9 deletions public/docs/_examples/testing/ts/app/expect-proper.ts

This file was deleted.

16 changes: 8 additions & 8 deletions public/docs/_examples/testing/ts/app/http-hero.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* tslint:disable:no-unused-variable */
import {
beforeEach, beforeEachProviders,
describe, ddescribe, xdescribe,
expect, it, iit, xit,
addProviders,
async, inject, withProviders
} from '@angular/core/testing';

import { TestComponentBuilder } from '@angular/compiler/testing';
import { TestComponentBuilder } from '@angular/core/testing';

import {
MockBackend,
Expand Down Expand Up @@ -42,10 +40,12 @@ const makeResponseData = (data: {}) => {return { data }; };
//////// SPECS /////////////
describe('Http-HeroService (mockBackend)', () => {

beforeEachProviders(() => [
HTTP_PROVIDERS,
{ provide: XHRBackend, useClass: MockBackend }
]);
beforeEach(() => {
addProviders([
HTTP_PROVIDERS,
{ provide: XHRBackend, useClass: MockBackend }
]);
});

it('can instantiate service when inject service',
withProviders(() => [HeroService])
Expand Down