refactor(core): update entry_components_integration_spec not to use TestComponentBuilder

This commit is contained in:
vsavkin 2016-08-16 20:20:35 -07:00 committed by Vikram Subramanian
parent cc22b051e3
commit 740e80492d
1 changed files with 39 additions and 76 deletions

View File

@ -7,10 +7,7 @@
*/
import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, ComponentFactoryResolver, NoComponentFactoryError, forwardRef} from '@angular/core';
import {ViewMetadata} from '@angular/core/src/metadata/view';
import {TestBed} from '@angular/core/testing';
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
import {Console} from '../../src/console';
import {stringify} from '../../src/facade/lang';
@ -34,62 +31,32 @@ function declareTests({useJit}: {useJit: boolean}) {
TestBed.configureCompiler(
{useJit: useJit, providers: [{provide: Console, useValue: console}]});
TestBed.configureTestingModule({declarations: [MainComp, ChildComp, NestedChildComp]});
TestBed.compileComponents();
});
it('should warn and auto declare if the component was not declared nor imported by the module',
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
@Component({selector: 'nestedchild', template: ''})
class NestedChildComp {
}
@Component({selector: 'child', template: '', entryComponents: [NestedChildComp]})
class ChildComp {
}
@Component({template: 'comp', entryComponents: [ChildComp]})
class SomeComp {
}
const compFixture = tcb.createSync(SomeComp);
const cf = compFixture.componentRef.injector.get(ComponentFactoryResolver)
.resolveComponentFactory(ChildComp);
expect(cf.componentType).toBe(ChildComp);
expect(console.warnings).toEqual([
`Component ${stringify(SomeComp)} in NgModule DynamicTestModule uses ${stringify(ChildComp)} via "entryComponents" but it was neither declared nor imported into the module! This warning will become an error after final.`,
`Component ${stringify(ChildComp)} in NgModule DynamicTestModule uses ${stringify(NestedChildComp)} via "entryComponents" but it was neither declared nor imported into the module! This warning will become an error after final.`
]);
}));
it('should resolve ComponentFactories from the same component',
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
const compFixture = tcb.createSync(MainComp);
it('should resolve ComponentFactories from the same component', () => {
const compFixture = TestBed.createComponent(MainComp);
let mainComp: MainComp = compFixture.componentInstance;
expect(compFixture.componentRef.injector.get(ComponentFactoryResolver)).toBe(mainComp.cfr);
var cf = mainComp.cfr.resolveComponentFactory(ChildComp);
expect(cf.componentType).toBe(ChildComp);
}));
});
it('should resolve ComponentFactories via ANALYZE_FOR_ENTRY_COMPONENTS',
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
let compFixture = tcb.createSync(CompWithAnalyzeEntryComponentsProvider);
it('should resolve ComponentFactories via ANALYZE_FOR_ENTRY_COMPONENTS', () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({declarations: [CompWithAnalyzeEntryComponentsProvider]});
let compFixture = TestBed.createComponent(CompWithAnalyzeEntryComponentsProvider);
let mainComp: CompWithAnalyzeEntryComponentsProvider = compFixture.componentInstance;
let cfr: ComponentFactoryResolver =
compFixture.componentRef.injector.get(ComponentFactoryResolver);
expect(cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
expect(cfr.resolveComponentFactory(NestedChildComp).componentType).toBe(NestedChildComp);
}));
});
it('should be able to get a component form a parent component (view hiearchy)',
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
const compFixture =
tcb.overrideView(
MainComp,
new ViewMetadata({template: '<child></child>', directives: [ChildComp]}))
.createSync(MainComp);
it('should be able to get a component form a parent component (view hiearchy)', () => {
TestBed.overrideComponent(MainComp, {set: {template: '<child></child>'}});
const compFixture = TestBed.createComponent(MainComp);
let childCompEl = compFixture.debugElement.children[0];
let childComp: ChildComp = childCompEl.componentInstance;
// declared on ChildComp directly
@ -97,23 +64,19 @@ function declareTests({useJit}: {useJit: boolean}) {
.toBe(NestedChildComp);
// inherited from MainComp
expect(childComp.cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
}));
});
it('should not be able to get components from a parent component (content hierarchy)',
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
const compFixture = tcb.overrideView(MainComp, new ViewMetadata({
template: '<child><nested></nested></child>',
directives: [ChildComp, NestedChildComp]
}))
.overrideTemplate(ChildComp, '<ng-content></ng-content>')
.createSync(MainComp);
it('should not be able to get components from a parent component (content hierarchy)', () => {
TestBed.overrideComponent(MainComp, {set: {template: '<child><nested></nested></child>'}});
TestBed.overrideComponent(ChildComp, {set: {template: '<ng-content></ng-content>'}});
const compFixture = TestBed.createComponent(MainComp);
let nestedChildCompEl = compFixture.debugElement.children[0].children[0];
let nestedChildComp: NestedChildComp = nestedChildCompEl.componentInstance;
expect(nestedChildComp.cfr.resolveComponentFactory(ChildComp).componentType)
.toBe(ChildComp);
expect(nestedChildComp.cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
expect(() => nestedChildComp.cfr.resolveComponentFactory(NestedChildComp))
.toThrow(new NoComponentFactoryError(NestedChildComp));
}));
});
});
}