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 {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 {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 {Console} from '../../src/console';
|
||||||
import {stringify} from '../../src/facade/lang';
|
import {stringify} from '../../src/facade/lang';
|
||||||
|
|
||||||
|
@ -34,62 +31,32 @@ function declareTests({useJit}: {useJit: boolean}) {
|
||||||
TestBed.configureCompiler(
|
TestBed.configureCompiler(
|
||||||
{useJit: useJit, providers: [{provide: Console, useValue: console}]});
|
{useJit: useJit, providers: [{provide: Console, useValue: console}]});
|
||||||
TestBed.configureTestingModule({declarations: [MainComp, ChildComp, NestedChildComp]});
|
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',
|
it('should resolve ComponentFactories from the same component', () => {
|
||||||
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
const compFixture = TestBed.createComponent(MainComp);
|
||||||
|
|
||||||
@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);
|
|
||||||
let mainComp: MainComp = compFixture.componentInstance;
|
let mainComp: MainComp = compFixture.componentInstance;
|
||||||
expect(compFixture.componentRef.injector.get(ComponentFactoryResolver)).toBe(mainComp.cfr);
|
expect(compFixture.componentRef.injector.get(ComponentFactoryResolver)).toBe(mainComp.cfr);
|
||||||
var cf = mainComp.cfr.resolveComponentFactory(ChildComp);
|
var cf = mainComp.cfr.resolveComponentFactory(ChildComp);
|
||||||
expect(cf.componentType).toBe(ChildComp);
|
expect(cf.componentType).toBe(ChildComp);
|
||||||
}));
|
});
|
||||||
|
|
||||||
|
it('should resolve ComponentFactories via ANALYZE_FOR_ENTRY_COMPONENTS', () => {
|
||||||
it('should resolve ComponentFactories via ANALYZE_FOR_ENTRY_COMPONENTS',
|
TestBed.resetTestingModule();
|
||||||
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.configureTestingModule({declarations: [CompWithAnalyzeEntryComponentsProvider]});
|
||||||
let compFixture = tcb.createSync(CompWithAnalyzeEntryComponentsProvider);
|
let compFixture = TestBed.createComponent(CompWithAnalyzeEntryComponentsProvider);
|
||||||
let mainComp: CompWithAnalyzeEntryComponentsProvider = compFixture.componentInstance;
|
let mainComp: CompWithAnalyzeEntryComponentsProvider = compFixture.componentInstance;
|
||||||
let cfr: ComponentFactoryResolver =
|
let cfr: ComponentFactoryResolver =
|
||||||
compFixture.componentRef.injector.get(ComponentFactoryResolver);
|
compFixture.componentRef.injector.get(ComponentFactoryResolver);
|
||||||
expect(cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
|
expect(cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
|
||||||
expect(cfr.resolveComponentFactory(NestedChildComp).componentType).toBe(NestedChildComp);
|
expect(cfr.resolveComponentFactory(NestedChildComp).componentType).toBe(NestedChildComp);
|
||||||
}));
|
});
|
||||||
|
|
||||||
it('should be able to get a component form a parent component (view hiearchy)',
|
it('should be able to get a component form a parent component (view hiearchy)', () => {
|
||||||
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.overrideComponent(MainComp, {set: {template: '<child></child>'}});
|
||||||
const compFixture =
|
|
||||||
tcb.overrideView(
|
const compFixture = TestBed.createComponent(MainComp);
|
||||||
MainComp,
|
|
||||||
new ViewMetadata({template: '<child></child>', directives: [ChildComp]}))
|
|
||||||
.createSync(MainComp);
|
|
||||||
let childCompEl = compFixture.debugElement.children[0];
|
let childCompEl = compFixture.debugElement.children[0];
|
||||||
let childComp: ChildComp = childCompEl.componentInstance;
|
let childComp: ChildComp = childCompEl.componentInstance;
|
||||||
// declared on ChildComp directly
|
// declared on ChildComp directly
|
||||||
|
@ -97,23 +64,19 @@ function declareTests({useJit}: {useJit: boolean}) {
|
||||||
.toBe(NestedChildComp);
|
.toBe(NestedChildComp);
|
||||||
// inherited from MainComp
|
// inherited from MainComp
|
||||||
expect(childComp.cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
|
expect(childComp.cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
|
||||||
}));
|
});
|
||||||
|
|
||||||
it('should not be able to get components from a parent component (content hierarchy)',
|
it('should not be able to get components from a parent component (content hierarchy)', () => {
|
||||||
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
TestBed.overrideComponent(MainComp, {set: {template: '<child><nested></nested></child>'}});
|
||||||
const compFixture = tcb.overrideView(MainComp, new ViewMetadata({
|
TestBed.overrideComponent(ChildComp, {set: {template: '<ng-content></ng-content>'}});
|
||||||
template: '<child><nested></nested></child>',
|
|
||||||
directives: [ChildComp, NestedChildComp]
|
const compFixture = TestBed.createComponent(MainComp);
|
||||||
}))
|
|
||||||
.overrideTemplate(ChildComp, '<ng-content></ng-content>')
|
|
||||||
.createSync(MainComp);
|
|
||||||
let nestedChildCompEl = compFixture.debugElement.children[0].children[0];
|
let nestedChildCompEl = compFixture.debugElement.children[0].children[0];
|
||||||
let nestedChildComp: NestedChildComp = nestedChildCompEl.componentInstance;
|
let nestedChildComp: NestedChildComp = nestedChildCompEl.componentInstance;
|
||||||
expect(nestedChildComp.cfr.resolveComponentFactory(ChildComp).componentType)
|
expect(nestedChildComp.cfr.resolveComponentFactory(ChildComp).componentType).toBe(ChildComp);
|
||||||
.toBe(ChildComp);
|
|
||||||
expect(() => nestedChildComp.cfr.resolveComponentFactory(NestedChildComp))
|
expect(() => nestedChildComp.cfr.resolveComponentFactory(NestedChildComp))
|
||||||
.toThrow(new NoComponentFactoryError(NestedChildComp));
|
.toThrow(new NoComponentFactoryError(NestedChildComp));
|
||||||
}));
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue