diff --git a/modules/angular2/src/core/compiler/private_component_loader.js b/modules/angular2/src/core/compiler/private_component_loader.js index 336ac9fa5d..8352c3062b 100644 --- a/modules/angular2/src/core/compiler/private_component_loader.js +++ b/modules/angular2/src/core/compiler/private_component_loader.js @@ -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, diff --git a/modules/angular2/test/core/compiler/private_component_loader_spec.js b/modules/angular2/test/core/compiler/private_component_loader_spec.js new file mode 100644 index 0000000000..e9da39f6da --- /dev/null +++ b/modules/angular2/test/core/compiler/private_component_loader_spec.js @@ -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."); + }); + }); + }); +}