refactor(core): update entry_components_integration_spec not to use TestComponentBuilder
This commit is contained in:
parent
cc22b051e3
commit
740e80492d
|
@ -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,86 +31,52 @@ 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) => {
|
||||
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);
|
||||
});
|
||||
|
||||
@Component({selector: 'nestedchild', template: ''})
|
||||
class NestedChildComp {
|
||||
}
|
||||
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);
|
||||
});
|
||||
|
||||
@Component({selector: 'child', template: '', entryComponents: [NestedChildComp]})
|
||||
class ChildComp {
|
||||
}
|
||||
it('should be able to get a component form a parent component (view hiearchy)', () => {
|
||||
TestBed.overrideComponent(MainComp, {set: {template: '<child></child>'}});
|
||||
|
||||
@Component({template: 'comp', entryComponents: [ChildComp]})
|
||||
class SomeComp {
|
||||
}
|
||||
const compFixture = TestBed.createComponent(MainComp);
|
||||
let childCompEl = compFixture.debugElement.children[0];
|
||||
let childComp: ChildComp = childCompEl.componentInstance;
|
||||
// declared on ChildComp directly
|
||||
expect(childComp.cfr.resolveComponentFactory(NestedChildComp).componentType)
|
||||
.toBe(NestedChildComp);
|
||||
// inherited from MainComp
|
||||
expect(childComp.cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
|
||||
});
|
||||
|
||||
const compFixture = tcb.createSync(SomeComp);
|
||||
const cf = compFixture.componentRef.injector.get(ComponentFactoryResolver)
|
||||
.resolveComponentFactory(ChildComp);
|
||||
expect(cf.componentType).toBe(ChildComp);
|
||||
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>'}});
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
let childCompEl = compFixture.debugElement.children[0];
|
||||
let childComp: ChildComp = childCompEl.componentInstance;
|
||||
// declared on ChildComp directly
|
||||
expect(childComp.cfr.resolveComponentFactory(NestedChildComp).componentType)
|
||||
.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);
|
||||
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(NestedChildComp))
|
||||
.toThrow(new NoComponentFactoryError(NestedChildComp));
|
||||
}));
|
||||
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(NestedChildComp))
|
||||
.toThrow(new NoComponentFactoryError(NestedChildComp));
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue