refactor(core): remove deprecated @Component.directives and @Component.pipes
BREAKING CHANGE: previously deprecated @Component.directives and @Component.pipes support was removed. All the components and pipes now must be declarated via an NgModule. NgModule is the basic compilation block passed into the Angular compiler via Compiler#compileModuleSync or #compileModuleAsync. Because of this change, the Compiler#compileComponentAsync and #compileComponentSync were removed as well - any code doing compilation should compile module instead using the apis mentioned above. Lastly, since modules are the basic compilation unit, the ngUpgrade module was modified to always require an NgModule to be passed into the UpgradeAdapter's constructor - previously this was optional.
This commit is contained in:
@ -6,17 +6,21 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Component, ComponentMetadata, Injector} from '@angular/core';
|
||||
import {beforeEach, ddescribe, describe, expect, iit, inject, it} from '@angular/core/testing/testing_internal';
|
||||
import {Component, ComponentMetadata, Directive, Injector} from '@angular/core';
|
||||
import {TestBed, inject} from '@angular/core/testing';
|
||||
|
||||
import {ViewMetadata} from '../core_private';
|
||||
import {isBlank, stringify} from '../src/facade/lang';
|
||||
import {MockDirectiveResolver} from '../testing';
|
||||
|
||||
export function main() {
|
||||
describe('MockDirectiveResolver', () => {
|
||||
var dirResolver: MockDirectiveResolver;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule(
|
||||
{declarations: [SomeDirective, SomeOtherDirective, SomeComponent]});
|
||||
});
|
||||
|
||||
beforeEach(inject([Injector], (injector: Injector) => {
|
||||
dirResolver = new MockDirectiveResolver(injector);
|
||||
}));
|
||||
@ -40,14 +44,12 @@ export function main() {
|
||||
it('should fallback to the default ViewResolver when templates are not overridden', () => {
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.template).toEqual('template');
|
||||
expect(view.directives).toEqual([SomeDirective]);
|
||||
});
|
||||
|
||||
it('should allow overriding the @View', () => {
|
||||
dirResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.template).toEqual('overridden template');
|
||||
expect(isBlank(view.directives)).toBe(true);
|
||||
});
|
||||
|
||||
it('should allow overriding a view after it has been resolved', () => {
|
||||
@ -55,7 +57,6 @@ export function main() {
|
||||
dirResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.template).toEqual('overridden template');
|
||||
expect(isBlank(view.directives)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@ -64,7 +65,6 @@ export function main() {
|
||||
dirResolver.setInlineTemplate(SomeComponent, 'overridden template');
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.template).toEqual('overridden template');
|
||||
expect(view.directives).toEqual([SomeDirective]);
|
||||
});
|
||||
|
||||
it('should allow overriding an overridden @View', () => {
|
||||
@ -81,50 +81,17 @@ export function main() {
|
||||
expect(view.template).toEqual('overridden template');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('Directive overriding', () => {
|
||||
it('should allow overriding a directive from the default view', () => {
|
||||
dirResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.directives.length).toEqual(1);
|
||||
expect(view.directives[0]).toBe(SomeOtherDirective);
|
||||
});
|
||||
|
||||
it('should allow overriding a directive from an overridden @View', () => {
|
||||
dirResolver.setView(SomeComponent, new ViewMetadata({directives: [SomeOtherDirective]}));
|
||||
dirResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.directives.length).toEqual(1);
|
||||
expect(view.directives[0]).toBe(SomeComponent);
|
||||
});
|
||||
|
||||
it('should throw when the overridden directive is not present', () => {
|
||||
dirResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
|
||||
expect(() => { dirResolver.resolve(SomeComponent); })
|
||||
.toThrowError(
|
||||
`Overriden directive ${stringify(SomeOtherDirective)} not found in the template of ${stringify(SomeComponent)}`);
|
||||
});
|
||||
|
||||
it('should allow overriding a directive after its view has been resolved', () => {
|
||||
dirResolver.resolve(SomeComponent);
|
||||
dirResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
||||
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
||||
expect(view.directives.length).toEqual(1);
|
||||
expect(view.directives[0]).toBe(SomeOtherDirective);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class SomeDirective {}
|
||||
@Directive({selector: 'some-directive'})
|
||||
class SomeDirective {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'cmp',
|
||||
template: 'template',
|
||||
directives: [SomeDirective],
|
||||
})
|
||||
@Component({selector: 'cmp', template: 'template'})
|
||||
class SomeComponent {
|
||||
}
|
||||
|
||||
class SomeOtherDirective {}
|
||||
@Directive({selector: 'some-other-directive'})
|
||||
class SomeOtherDirective {
|
||||
}
|
||||
|
@ -104,16 +104,7 @@ class SomeDirectiveWithViewChild {
|
||||
c: any;
|
||||
}
|
||||
|
||||
class SomeDir {}
|
||||
class SomePipe {}
|
||||
|
||||
@Component({
|
||||
selector: 'sample',
|
||||
template: 'some template',
|
||||
directives: [SomeDir],
|
||||
pipes: [SomePipe],
|
||||
styles: ['some styles']
|
||||
})
|
||||
@Component({selector: 'sample', template: 'some template', styles: ['some styles']})
|
||||
class ComponentWithTemplate {
|
||||
}
|
||||
|
||||
@ -236,8 +227,6 @@ export function main() {
|
||||
it('should read out the template related metadata from the Component metadata', () => {
|
||||
var compMetadata = <ComponentMetadata>resolver.resolve(ComponentWithTemplate);
|
||||
expect(compMetadata.template).toEqual('some template');
|
||||
expect(compMetadata.directives).toEqual([SomeDir]);
|
||||
expect(compMetadata.pipes).toEqual([SomePipe]);
|
||||
expect(compMetadata.styles).toEqual(['some styles']);
|
||||
});
|
||||
});
|
||||
|
@ -140,12 +140,12 @@ export function main() {
|
||||
|
||||
it('should throw when using a templateUrl in a nested component that has not been compiled before',
|
||||
() => {
|
||||
@NgModule({declarations: [SomeComp], entryComponents: [SomeComp]})
|
||||
@NgModule({declarations: [SomeComp, ChildComp], entryComponents: [SomeComp]})
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
dirResolver.setView(SomeComp, new ViewMetadata({template: '', directives: [ChildComp]}));
|
||||
dirResolver.setView(SomeComp, new ViewMetadata({template: ''}));
|
||||
dirResolver.setView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'}));
|
||||
expect(() => compiler.compileModuleSync(SomeModule))
|
||||
.toThrowError(
|
||||
|
Reference in New Issue
Block a user