refactor(tests): move public test APIs to TestBed (#10621)

Completely remove deprecated TestComponentBuilder and friends.
This commit is contained in:
Julie Ralph 2016-08-10 11:51:40 -07:00 committed by vikerman
parent 43512aa5eb
commit aff1bc9f2d
2 changed files with 456 additions and 515 deletions

View File

@ -8,7 +8,7 @@
import {XHR} from '@angular/compiler';
import {Component, bind} from '@angular/core';
import {TestComponentBuilder, addProviders, async, fakeAsync, inject, tick} from '@angular/core/testing';
import {TestBed, async, fakeAsync, flushMicrotasks, inject, tick} from '@angular/core/testing';
import {XHRImpl} from '../src/xhr/xhr_impl';
@ -56,7 +56,10 @@ export function main() {
describe('using the test injector with the inject helper', () => {
describe('setting up Providers', () => {
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
beforeEach(() => {
TestBed.configureTestingModule(
{providers: [{provide: FancyService, useValue: new FancyService()}]});
});
it('provides a real XHR instance',
inject([XHR], (xhr: XHR) => { expect(xhr instanceof XHRImpl).toBeTruthy(); }));
@ -96,10 +99,10 @@ export function main() {
it('should fail when an XHR fails', (done: any /** TODO #9100 */) => {
var itPromise = patchJasmineIt();
it('should fail with an error from a promise',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb.createAsync(BadTemplateUrl);
})));
it('should fail with an error from a promise', async(() => {
TestBed.configureTestingModule({declarations: [BadTemplateUrl]});
TestBed.compileComponents();
}));
itPromise.then(
() => { done.fail('Expected test to fail, but it did not'); },
@ -113,17 +116,15 @@ export function main() {
});
describe('test component builder', function() {
it('should allow an external templateUrl',
async(inject(
[TestComponentBuilder],
(tcb: TestComponentBuilder) => {
tcb.createAsync(ExternalTemplateComp).then((componentFixture) => {
it('should allow an external templateUrl', async(() => {
TestBed.configureTestingModule({declarations: [ExternalTemplateComp]});
TestBed.compileComponents().then(() => {
let componentFixture = TestBed.createComponent(ExternalTemplateComp);
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement.textContent)
.toEqual('from external template\n');
});
})),
}),
10000); // Long timeout here because this test makes an actual XHR, and is slow on Edge.
});
});

View File

@ -9,15 +9,14 @@
import {NgIf} from '@angular/common';
import {CompilerConfig, XHR} from '@angular/compiler';
import {CUSTOM_ELEMENTS_SCHEMA, Component, ComponentFactoryResolver, ComponentMetadata, Directive, DirectiveMetadata, HostBinding, Injectable, Input, NgModule, NgModuleMetadata, Pipe, PipeMetadata, ViewMetadata, provide} from '@angular/core';
import {TestBed, TestComponentBuilder, addProviders, async, fakeAsync, inject, tick, withModule, withProviders} from '@angular/core/testing';
import {TestBed, async, fakeAsync, inject, tick, withModule} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers';
import {stringify} from '../src/facade/lang';
// Services, and components for the tests.
@Component(
{selector: 'child-comp', template: `<span>Original {{childBinding}}</span>`, directives: []})
@Component({selector: 'child-comp', template: `<span>Original {{childBinding}}</span>`})
@Injectable()
class ChildComp {
childBinding: string;
@ -32,7 +31,6 @@ class MockChildComp {
@Component({
selector: 'parent-comp',
template: `Parent(<child-comp></child-comp>)`,
directives: [ChildComp]
})
@Injectable()
class ParentComp {
@ -52,7 +50,6 @@ class ChildChildComp {
@Component({
selector: 'child-comp',
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
directives: [ChildChildComp]
})
@Injectable()
class ChildWithChildComp {
@ -60,11 +57,6 @@ class ChildWithChildComp {
constructor() { this.childBinding = 'Child'; }
}
@Component({selector: 'child-child-comp', template: `<span>ChildChild Mock</span>`})
@Injectable()
class MockChildChildComp {
}
class FancyService {
value: string = 'real value';
getAsyncValue() { return Promise.resolve('async value'); }
@ -123,6 +115,7 @@ class CompWithUrlTemplate {
}
export function main() {
describe('public testing API', () => {
describe('using the async helper', () => {
var actuallyDone: boolean;
@ -150,9 +143,12 @@ export function main() {
describe('using the test injector with the inject helper', () => {
describe('setting up Providers', () => {
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
beforeEach(() => {
TestBed.configureTestingModule(
{providers: [{provide: FancyService, useValue: new FancyService()}]});
it('should use set up providers', inject([FancyService], (service: any /** TODO #9100 */) => {
it('should use set up providers',
inject([FancyService], (service: any /** TODO #9100 */) => {
expect(service.value).toEqual('real value');
}));
@ -208,22 +204,6 @@ export function main() {
}));
});
});
describe('per test providers', () => {
it('should allow per test providers',
withProviders(() => [{provide: FancyService, useValue: new FancyService()}])
.inject([FancyService], (service: any /** TODO #9100 */) => {
expect(service.value).toEqual('real value');
}));
it('should return value from inject', () => {
let retval = withProviders(() => [{provide: FancyService, useValue: new FancyService()}])
.inject([FancyService], (service: any /** TODO #9100 */) => {
expect(service.value).toEqual('real value');
return 10;
})();
expect(retval).toBe(10);
});
});
});
@ -310,7 +290,7 @@ export function main() {
});
});
describe('overwrite metadata', () => {
describe('overwriting metadata', () => {
@Pipe({name: 'undefined'})
class SomePipe {
transform(value: string): string { return `transformed ${value}`; }
@ -325,7 +305,7 @@ export function main() {
class SomeComponent {
}
@Component({selector: 'othercomp', template: 'someOtherText'})
@Component({selector: 'comp', template: 'someOtherText'})
class SomeOtherComponent {
}
@ -347,7 +327,8 @@ export function main() {
describe('component', () => {
beforeEach(() => {
TestBed.overrideComponent(SomeComponent, {set: {selector: 'comp', template: 'newText'}});
TestBed.overrideComponent(
SomeComponent, {set: {selector: 'comp', template: 'newText'}});
});
it('should work', () => {
expect(TestBed.createComponent(SomeComponent).nativeElement).toHaveText('newText');
@ -388,15 +369,18 @@ export function main() {
describe('providers', () => {
beforeEach(() => {
let xhrGet = jasmine.createSpy('xhrGet').and.returnValue(Promise.resolve('Hello world!'));
let xhrGet =
jasmine.createSpy('xhrGet').and.returnValue(Promise.resolve('Hello world!'));
TestBed.configureTestingModule({declarations: [CompWithUrlTemplate]});
TestBed.configureCompiler({providers: [{provide: XHR, useValue: {get: xhrGet}}]});
});
it('should use set up providers',
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
let compFixture = tcb.createFakeAsync(CompWithUrlTemplate);
it('should use set up providers', fakeAsync(() => {
TestBed.compileComponents();
tick();
let compFixture = TestBed.createComponent(CompWithUrlTemplate);
expect(compFixture.nativeElement).toHaveText('Hello world!');
})));
}));
});
describe('useJit true', () => {
@ -496,28 +480,6 @@ export function main() {
restoreJasmineIt();
});
describe('using addProviders', () => {
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
beforeEach(inject([FancyService], (service: any /** TODO #9100 */) => {
expect(service.value).toEqual('real value');
}));
describe('nested addProviders', () => {
it('should fail when the injector has already been used', () => {
patchJasmineBeforeEach();
expect(() => {
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
})
.toThrowError(
`Cannot configure the test module when the test module has already been instantiated. ` +
`Make sure you are not using \`inject\` before \`TestBed.configureTestingModule\`.`);
restoreJasmineBeforeEach();
});
});
});
describe('components', () => {
let xhrGet: jasmine.Spy;
beforeEach(() => {
@ -530,8 +492,8 @@ export function main() {
var itPromise = patchJasmineIt();
expect(
() =>
it('should fail', withModule(
() => it(
'should fail', withModule(
{declarations: [CompWithUrlTemplate]},
() => { TestBed.createComponent(CompWithUrlTemplate); })))
.toThrowError(
@ -562,107 +524,85 @@ export function main() {
});
});
describe('test component builder', function() {
it('should instantiate a component with valid DOM',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
describe('creating components', function() {
tcb.createAsync(ChildComp).then((componentFixture) => {
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement).toHaveText('Original Child');
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
ChildComp,
MyIfComp,
ChildChildComp,
ParentComp,
TestProvidersComp,
TestViewProvidersComp,
]
});
});
})));
it('should allow changing members of the component',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
it('should instantiate a component with valid DOM', async(() => {
var fixture = TestBed.createComponent(ChildComp);
fixture.detectChanges();
tcb.createAsync(MyIfComp).then((componentFixture) => {
expect(fixture.debugElement.nativeElement).toHaveText('Original Child');
}));
it('should allow changing members of the component', async(() => {
var componentFixture = TestBed.createComponent(MyIfComp);
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf()');
componentFixture.debugElement.componentInstance.showMore = true;
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf(More)');
});
})));
}));
it('should override a template',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.overrideTemplate(MockChildComp, '<span>Mock</span>')
.createAsync(MockChildComp)
.then((componentFixture) => {
it('should override a template', async(() => {
TestBed.overrideComponent(ChildComp, {set: {template: '<span>Mock</span>'}});
let componentFixture = TestBed.createComponent(ChildComp);
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement).toHaveText('Mock');
});
})));
}));
it('should override a view',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.overrideView(
ChildComp, new ViewMetadata({template: '<span>Modified {{childBinding}}</span>'}))
.createAsync(ChildComp)
.then((componentFixture) => {
it('should override a provider', async(() => {
TestBed.overrideComponent(
TestProvidersComp,
{set: {providers: [{provide: FancyService, useClass: MockFancyService}]}});
var componentFixture = TestBed.createComponent(TestProvidersComp);
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement).toHaveText('Modified Child');
expect(componentFixture.debugElement.nativeElement)
.toHaveText('injected value: mocked out value');
}));
it('should override a viewProvider', async(() => {
TestBed.overrideComponent(
TestViewProvidersComp,
{set: {viewProviders: [{provide: FancyService, useClass: MockFancyService}]}});
var componentFixture = TestBed.createComponent(TestViewProvidersComp);
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement)
.toHaveText('injected value: mocked out value');
}));
});
describe('using alternate components', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
MockChildComp,
ParentComp,
]
});
});
})));
it('should override component dependencies',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
it('should override component dependencies', async(() => {
tcb.overrideDirective(ParentComp, ChildComp, MockChildComp)
.createAsync(ParentComp)
.then((componentFixture) => {
let componentFixture = TestBed.createComponent(ParentComp);
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement).toHaveText('Parent(Mock)');
}));
});
})));
it('should override child component\'s dependencies',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.overrideDirective(ParentComp, ChildComp, ChildWithChildComp)
.overrideDirective(ChildWithChildComp, ChildChildComp, MockChildChildComp)
.createAsync(ParentComp)
.then((componentFixture) => {
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement)
.toHaveText('Parent(Original Child(ChildChild Mock))');
});
})));
it('should override a provider',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.overrideProviders(
TestProvidersComp, [{provide: FancyService, useClass: MockFancyService}])
.createAsync(TestProvidersComp)
.then((componentFixture) => {
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement)
.toHaveText('injected value: mocked out value');
});
})));
it('should override a viewProvider',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.overrideViewProviders(
TestViewProvidersComp, [{provide: FancyService, useClass: MockFancyService}])
.createAsync(TestViewProvidersComp)
.then((componentFixture) => {
componentFixture.detectChanges();
expect(componentFixture.debugElement.nativeElement)
.toHaveText('injected value: mocked out value');
});
})));
});
}