parent
8ddc93d211
commit
abd860c3ca
|
@ -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';
|
||||
|
||||
|
@ -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', () => {
|
||||
|
|
|
@ -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';
|
||||
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -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,
|
||||
|
@ -16,14 +16,12 @@ 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';
|
||||
|
||||
|
@ -31,47 +29,6 @@ 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;
|
||||
|
||||
|
@ -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 => {},
|
||||
|
@ -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) => {
|
||||
|
@ -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'); }
|
||||
|
@ -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');
|
||||
});
|
||||
})));
|
||||
|
||||
|
@ -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)');
|
||||
});
|
||||
})));
|
||||
|
||||
|
@ -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');
|
||||
});
|
||||
})));
|
||||
|
||||
|
@ -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');
|
||||
|
||||
});
|
||||
})));
|
||||
|
@ -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)');
|
||||
|
||||
});
|
||||
})));
|
||||
|
@ -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))');
|
||||
|
||||
});
|
||||
})));
|
||||
|
@ -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');
|
||||
});
|
||||
})));
|
||||
|
||||
|
@ -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');
|
||||
});
|
||||
})));
|
||||
|
||||
|
@ -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.
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
@ -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',
|
||||
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
// See https://github.com/angular/angular/issues/9017
|
||||
import { expect as expectCore } from '@angular/core/testing';
|
||||
import { NgMatchers } from '@angular/platform-browser/testing';
|
||||
|
||||
export function expect(spy: Function): NgMatchers;
|
||||
export function expect(actual: any): NgMatchers;
|
||||
export function expect(actual: any): NgMatchers {
|
||||
return expectCore(actual) as NgMatchers;
|
||||
}
|
|
@ -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,
|
||||
|
@ -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])
|
||||
|
|
Loading…
Reference in New Issue