feat(core): add syntax sugar to make @View optional

This commit is contained in:
vsavkin
2015-10-06 17:03:37 -07:00
committed by Victor Savkin
parent f7b75330e0
commit bd31b01690
7 changed files with 264 additions and 12 deletions

View File

@ -1231,8 +1231,8 @@ export function main() {
try {
tcb.createAsync(ComponentWithoutView);
} catch (e) {
expect(e.message).toEqual(
`No View annotation found on component ${stringify(ComponentWithoutView)}`);
expect(e.message)
.toContain(`must have either 'template', 'templateUrl', or '@View' set.`);
return null;
}
}));
@ -1696,6 +1696,21 @@ export function main() {
});
}));
}
it('should support defining views in the component decorator',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
tcb.overrideView(MyComp, new ViewMetadata({
template: '<component-with-template></component-with-template>',
directives: [ComponentWithTempalte]
}))
.createAsync(MyComp)
.then((rootTC) => {
rootTC.detectChanges();
var native = rootTC.debugElement.componentViewChildren[0].nativeElement;
expect(native).toHaveText("No View Decorator: 123");
async.done();
});
}));
});
});
}
@ -2251,6 +2266,14 @@ class DirectiveThrowingAnError {
constructor() { throw new BaseException("BOOM"); }
}
@Component({
selector: 'component-with-template',
directives: [NgFor], template: `No View Decorator: <div *ng-for="#item of items">{{item}}</div>`
})
class ComponentWithTempalte {
items = [1, 2, 3];
}
@Directive({selector: 'with-prop-decorators'})
class DirectiveWithPropDecorators {
target;

View File

@ -0,0 +1,100 @@
import {ddescribe, describe, it, iit, expect, beforeEach} from 'angular2/test_lib';
import {ViewResolver} from 'angular2/src/core/linker/view_resolver';
import {Component, View, ViewMetadata} from 'angular2/src/core/metadata';
class SomeDir {}
class SomePipe {}
@Component({selector: 'sample'})
@View(
{template: "some template", directives: [SomeDir], pipes: [SomePipe], styles: ["some styles"]})
class ComponentWithView {
}
@Component({
selector: 'sample',
template: "some template",
directives: [SomeDir],
pipes: [SomePipe],
styles: ["some styles"]
})
class ComponentWithTemplate {
}
@Component({selector: 'sample', template: "some template"})
@View({template: "some template"})
class ComponentWithViewTemplate {
}
@Component({selector: 'sample'})
class ComponentWithoutView {
}
@Component({selector: 'sample', templateUrl: "some template url"})
@View({template: "some template"})
class ComponentWithViewTemplateUrl {
}
@View({template: "some template"})
class ClassWithView {
}
class SimpleClass {}
export function main() {
describe("ViewResolver", () => {
var resolver;
beforeEach(() => { resolver = new ViewResolver(); });
it('should read out the View metadata', () => {
var viewMetadata = resolver.resolve(ComponentWithView);
expect(viewMetadata)
.toEqual(new View({
template: "some template",
directives: [SomeDir],
pipes: [SomePipe],
styles: ["some styles"]
}));
});
it('should read out the View metadata from the Component metadata', () => {
var viewMetadata = resolver.resolve(ComponentWithTemplate);
expect(viewMetadata)
.toEqual(new ViewMetadata({
template: "some template",
directives: [SomeDir],
pipes: [SomePipe],
styles: ["some styles"]
}));
});
it('should read out the View metadata from a simple class', () => {
var viewMetadata = resolver.resolve(ClassWithView);
expect(viewMetadata).toEqual(new View({template: "some template"}));
});
it('should throw when Component.template is specified together with the View metadata', () => {
expect(() => resolver.resolve(ComponentWithViewTemplate))
.toThrowErrorWith(
"Component 'ComponentWithViewTemplate' cannot have both 'template' and '@View' set at the same time");
});
it('should throw when Component.template is specified together with the View metadata', () => {
expect(() => resolver.resolve(ComponentWithViewTemplateUrl))
.toThrowErrorWith(
"Component 'ComponentWithViewTemplateUrl' cannot have both 'templateUrl' and '@View' set at the same time");
});
it('should throw when Component has no View decorator and no template is set', () => {
expect(() => resolver.resolve(ComponentWithoutView))
.toThrowErrorWith(
"Component 'ComponentWithoutView' must have either 'template', 'templateUrl', or '@View' set");
});
it('should throw when simple class has no View decorator and no template is set', () => {
expect(() => resolver.resolve(SimpleClass))
.toThrowErrorWith("No View decorator found on component 'SimpleClass'");
});
});
}