feat(PrivateComponentLoader): Explicit error message when loading a non-component

fixes #1062
This commit is contained in:
Victor Berchet 2015-03-24 09:34:47 +01:00
parent 65d759316b
commit 101a4aa3cf
2 changed files with 39 additions and 1 deletions

View File

@ -2,8 +2,9 @@ import {Compiler} from './compiler';
import {ShadowDomStrategy} from './shadow_dom_strategy';
import {EventManager} from 'angular2/src/core/events/event_manager';
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
import {Component} from 'angular2/src/core/annotations/annotations';
import {PrivateComponentLocation} from './private_component_location';
import {Type} from 'angular2/src/facade/lang';
import {Type, stringify, BaseException} from 'angular2/src/facade/lang';
export class PrivateComponentLoader {
@ -23,6 +24,11 @@ export class PrivateComponentLoader {
load(type:Type, location:PrivateComponentLocation) {
var annotation = this.directiveMetadataReader.read(type).annotation;
if (!(annotation instanceof Component)) {
throw new BaseException(`Could not load '${stringify(type)}' because it is not a component.`);
}
return this.compiler.compile(type).then((componentProtoView) => {
location.createComponent(
type, annotation,

View File

@ -0,0 +1,32 @@
import {ddescribe, describe, it, iit, expect, beforeEach} from 'angular2/test_lib';
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
import {PrivateComponentLoader} from 'angular2/src/core/compiler/private_component_loader';
import {Decorator, Viewport} from 'angular2/src/core/annotations/annotations';
@Decorator({selector: 'someDecorator'})
class SomeDecorator {}
@Viewport({selector: 'someViewport'})
class SomeViewport {}
export function main() {
describe("PrivateComponentLoader", () => {
var loader;
beforeEach(() => {
loader = new PrivateComponentLoader(null, null, null, new DirectiveMetadataReader());
});
describe('Load errors', () => {
it('should throw when trying to load a decorator', () => {
expect(() => loader.load(SomeDecorator, null))
.toThrowError("Could not load 'SomeDecorator' because it is not a component.");
});
it('should throw when trying to load a viewport', () => {
expect(() => loader.load(SomeViewport, null))
.toThrowError("Could not load 'SomeViewport' because it is not a component.");
});
});
});
}