refactor(tests): move public test APIs to TestBed (#10621)
Completely remove deprecated TestComponentBuilder and friends.
This commit is contained in:
parent
43512aa5eb
commit
aff1bc9f2d
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import {XHR} from '@angular/compiler';
|
import {XHR} from '@angular/compiler';
|
||||||
import {Component, bind} from '@angular/core';
|
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';
|
import {XHRImpl} from '../src/xhr/xhr_impl';
|
||||||
|
|
||||||
@ -56,7 +56,10 @@ export function main() {
|
|||||||
|
|
||||||
describe('using the test injector with the inject helper', () => {
|
describe('using the test injector with the inject helper', () => {
|
||||||
describe('setting up Providers', () => {
|
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',
|
it('provides a real XHR instance',
|
||||||
inject([XHR], (xhr: XHR) => { expect(xhr instanceof XHRImpl).toBeTruthy(); }));
|
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 */) => {
|
it('should fail when an XHR fails', (done: any /** TODO #9100 */) => {
|
||||||
var itPromise = patchJasmineIt();
|
var itPromise = patchJasmineIt();
|
||||||
|
|
||||||
it('should fail with an error from a promise',
|
it('should fail with an error from a promise', async(() => {
|
||||||
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.configureTestingModule({declarations: [BadTemplateUrl]});
|
||||||
return tcb.createAsync(BadTemplateUrl);
|
TestBed.compileComponents();
|
||||||
})));
|
}));
|
||||||
|
|
||||||
itPromise.then(
|
itPromise.then(
|
||||||
() => { done.fail('Expected test to fail, but it did not'); },
|
() => { done.fail('Expected test to fail, but it did not'); },
|
||||||
@ -113,17 +116,15 @@ export function main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('test component builder', function() {
|
describe('test component builder', function() {
|
||||||
it('should allow an external templateUrl',
|
it('should allow an external templateUrl', async(() => {
|
||||||
async(inject(
|
TestBed.configureTestingModule({declarations: [ExternalTemplateComp]});
|
||||||
[TestComponentBuilder],
|
TestBed.compileComponents().then(() => {
|
||||||
(tcb: TestComponentBuilder) => {
|
let componentFixture = TestBed.createComponent(ExternalTemplateComp);
|
||||||
|
|
||||||
tcb.createAsync(ExternalTemplateComp).then((componentFixture) => {
|
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement.textContent)
|
expect(componentFixture.debugElement.nativeElement.textContent)
|
||||||
.toEqual('from external template\n');
|
.toEqual('from external template\n');
|
||||||
});
|
});
|
||||||
})),
|
}),
|
||||||
10000); // Long timeout here because this test makes an actual XHR, and is slow on Edge.
|
10000); // Long timeout here because this test makes an actual XHR, and is slow on Edge.
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -9,15 +9,14 @@
|
|||||||
import {NgIf} from '@angular/common';
|
import {NgIf} from '@angular/common';
|
||||||
import {CompilerConfig, XHR} from '@angular/compiler';
|
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 {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 {expect} from '@angular/platform-browser/testing/matchers';
|
||||||
|
|
||||||
import {stringify} from '../src/facade/lang';
|
import {stringify} from '../src/facade/lang';
|
||||||
|
|
||||||
// Services, and components for the tests.
|
// Services, and components for the tests.
|
||||||
|
|
||||||
@Component(
|
@Component({selector: 'child-comp', template: `<span>Original {{childBinding}}</span>`})
|
||||||
{selector: 'child-comp', template: `<span>Original {{childBinding}}</span>`, directives: []})
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
class ChildComp {
|
class ChildComp {
|
||||||
childBinding: string;
|
childBinding: string;
|
||||||
@ -32,7 +31,6 @@ class MockChildComp {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'parent-comp',
|
selector: 'parent-comp',
|
||||||
template: `Parent(<child-comp></child-comp>)`,
|
template: `Parent(<child-comp></child-comp>)`,
|
||||||
directives: [ChildComp]
|
|
||||||
})
|
})
|
||||||
@Injectable()
|
@Injectable()
|
||||||
class ParentComp {
|
class ParentComp {
|
||||||
@ -52,7 +50,6 @@ class ChildChildComp {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'child-comp',
|
selector: 'child-comp',
|
||||||
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
|
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
|
||||||
directives: [ChildChildComp]
|
|
||||||
})
|
})
|
||||||
@Injectable()
|
@Injectable()
|
||||||
class ChildWithChildComp {
|
class ChildWithChildComp {
|
||||||
@ -60,11 +57,6 @@ class ChildWithChildComp {
|
|||||||
constructor() { this.childBinding = 'Child'; }
|
constructor() { this.childBinding = 'Child'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component({selector: 'child-child-comp', template: `<span>ChildChild Mock</span>`})
|
|
||||||
@Injectable()
|
|
||||||
class MockChildChildComp {
|
|
||||||
}
|
|
||||||
|
|
||||||
class FancyService {
|
class FancyService {
|
||||||
value: string = 'real value';
|
value: string = 'real value';
|
||||||
getAsyncValue() { return Promise.resolve('async value'); }
|
getAsyncValue() { return Promise.resolve('async value'); }
|
||||||
@ -123,6 +115,7 @@ class CompWithUrlTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
|
describe('public testing API', () => {
|
||||||
describe('using the async helper', () => {
|
describe('using the async helper', () => {
|
||||||
var actuallyDone: boolean;
|
var actuallyDone: boolean;
|
||||||
|
|
||||||
@ -150,9 +143,12 @@ export function main() {
|
|||||||
|
|
||||||
describe('using the test injector with the inject helper', () => {
|
describe('using the test injector with the inject helper', () => {
|
||||||
describe('setting up Providers', () => {
|
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');
|
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'})
|
@Pipe({name: 'undefined'})
|
||||||
class SomePipe {
|
class SomePipe {
|
||||||
transform(value: string): string { return `transformed ${value}`; }
|
transform(value: string): string { return `transformed ${value}`; }
|
||||||
@ -325,7 +305,7 @@ export function main() {
|
|||||||
class SomeComponent {
|
class SomeComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component({selector: 'othercomp', template: 'someOtherText'})
|
@Component({selector: 'comp', template: 'someOtherText'})
|
||||||
class SomeOtherComponent {
|
class SomeOtherComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,7 +327,8 @@ export function main() {
|
|||||||
|
|
||||||
describe('component', () => {
|
describe('component', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.overrideComponent(SomeComponent, {set: {selector: 'comp', template: 'newText'}});
|
TestBed.overrideComponent(
|
||||||
|
SomeComponent, {set: {selector: 'comp', template: 'newText'}});
|
||||||
});
|
});
|
||||||
it('should work', () => {
|
it('should work', () => {
|
||||||
expect(TestBed.createComponent(SomeComponent).nativeElement).toHaveText('newText');
|
expect(TestBed.createComponent(SomeComponent).nativeElement).toHaveText('newText');
|
||||||
@ -388,15 +369,18 @@ export function main() {
|
|||||||
|
|
||||||
describe('providers', () => {
|
describe('providers', () => {
|
||||||
beforeEach(() => {
|
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}}]});
|
TestBed.configureCompiler({providers: [{provide: XHR, useValue: {get: xhrGet}}]});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use set up providers',
|
it('should use set up providers', fakeAsync(() => {
|
||||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.compileComponents();
|
||||||
let compFixture = tcb.createFakeAsync(CompWithUrlTemplate);
|
tick();
|
||||||
|
let compFixture = TestBed.createComponent(CompWithUrlTemplate);
|
||||||
expect(compFixture.nativeElement).toHaveText('Hello world!');
|
expect(compFixture.nativeElement).toHaveText('Hello world!');
|
||||||
})));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('useJit true', () => {
|
describe('useJit true', () => {
|
||||||
@ -496,28 +480,6 @@ export function main() {
|
|||||||
restoreJasmineIt();
|
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', () => {
|
describe('components', () => {
|
||||||
let xhrGet: jasmine.Spy;
|
let xhrGet: jasmine.Spy;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -530,8 +492,8 @@ export function main() {
|
|||||||
var itPromise = patchJasmineIt();
|
var itPromise = patchJasmineIt();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
() =>
|
() => it(
|
||||||
it('should fail', withModule(
|
'should fail', withModule(
|
||||||
{declarations: [CompWithUrlTemplate]},
|
{declarations: [CompWithUrlTemplate]},
|
||||||
() => { TestBed.createComponent(CompWithUrlTemplate); })))
|
() => { TestBed.createComponent(CompWithUrlTemplate); })))
|
||||||
.toThrowError(
|
.toThrowError(
|
||||||
@ -562,107 +524,85 @@ export function main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('test component builder', function() {
|
describe('creating components', function() {
|
||||||
it('should instantiate a component with valid DOM',
|
|
||||||
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
||||||
|
|
||||||
tcb.createAsync(ChildComp).then((componentFixture) => {
|
beforeEach(() => {
|
||||||
componentFixture.detectChanges();
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Original Child');
|
ChildComp,
|
||||||
|
MyIfComp,
|
||||||
|
ChildChildComp,
|
||||||
|
ParentComp,
|
||||||
|
TestProvidersComp,
|
||||||
|
TestViewProvidersComp,
|
||||||
|
]
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})));
|
|
||||||
|
|
||||||
it('should allow changing members of the component',
|
it('should instantiate a component with valid DOM', async(() => {
|
||||||
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
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();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf()');
|
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf()');
|
||||||
|
|
||||||
componentFixture.debugElement.componentInstance.showMore = true;
|
componentFixture.debugElement.componentInstance.showMore = true;
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf(More)');
|
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf(More)');
|
||||||
});
|
}));
|
||||||
})));
|
|
||||||
|
|
||||||
it('should override a template',
|
it('should override a template', async(() => {
|
||||||
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.overrideComponent(ChildComp, {set: {template: '<span>Mock</span>'}});
|
||||||
|
let componentFixture = TestBed.createComponent(ChildComp);
|
||||||
tcb.overrideTemplate(MockChildComp, '<span>Mock</span>')
|
|
||||||
.createAsync(MockChildComp)
|
|
||||||
.then((componentFixture) => {
|
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Mock');
|
expect(componentFixture.debugElement.nativeElement).toHaveText('Mock');
|
||||||
|
|
||||||
});
|
}));
|
||||||
})));
|
|
||||||
|
|
||||||
it('should override a view',
|
it('should override a provider', async(() => {
|
||||||
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.overrideComponent(
|
||||||
|
TestProvidersComp,
|
||||||
tcb.overrideView(
|
{set: {providers: [{provide: FancyService, useClass: MockFancyService}]}});
|
||||||
ChildComp, new ViewMetadata({template: '<span>Modified {{childBinding}}</span>'}))
|
var componentFixture = TestBed.createComponent(TestProvidersComp);
|
||||||
.createAsync(ChildComp)
|
|
||||||
.then((componentFixture) => {
|
|
||||||
componentFixture.detectChanges();
|
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',
|
it('should override component dependencies', async(() => {
|
||||||
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
||||||
|
|
||||||
tcb.overrideDirective(ParentComp, ChildComp, MockChildComp)
|
let componentFixture = TestBed.createComponent(ParentComp);
|
||||||
.createAsync(ParentComp)
|
|
||||||
.then((componentFixture) => {
|
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Parent(Mock)');
|
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');
|
|
||||||
});
|
|
||||||
})));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user