2015-03-30 09:45:03 -04:00
|
|
|
import {
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
it,
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
|
|
|
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
|
|
|
|
|
2015-04-28 21:17:00 -04:00
|
|
|
import {Component} from 'angular2/src/core/annotations_impl/annotations';
|
|
|
|
import {View} from 'angular2/src/core/annotations_impl/view';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
|
|
|
import {isBlank} from 'angular2/src/facade/lang';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('MockTemplateResolver', () => {
|
|
|
|
var resolver;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
resolver = new MockTemplateResolver();
|
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
describe('View overriding', () => {
|
2015-03-30 09:45:03 -04:00
|
|
|
it('should fallback to the default TemplateResolver when templates are not overridden', () => {
|
|
|
|
var template = resolver.resolve(SomeComponent);
|
2015-04-09 15:20:11 -04:00
|
|
|
expect(template.template).toEqual('template');
|
2015-03-30 09:45:03 -04:00
|
|
|
expect(template.directives).toEqual([SomeDirective]);
|
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
it('should allow overriding the @View', () => {
|
|
|
|
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
|
2015-03-30 09:45:03 -04:00
|
|
|
var template = resolver.resolve(SomeComponent);
|
2015-04-09 15:20:11 -04:00
|
|
|
expect(template.template).toEqual('overridden template');
|
2015-03-30 09:45:03 -04:00
|
|
|
expect(isBlank(template.directives)).toBe(true);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow overriding a template after it has been resolved', () => {
|
|
|
|
resolver.resolve(SomeComponent);
|
|
|
|
expect(() => {
|
2015-04-09 15:20:11 -04:00
|
|
|
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
|
2015-03-30 09:45:03 -04:00
|
|
|
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
describe('inline template definition overriding', () => {
|
|
|
|
it('should allow overriding the default template', () => {
|
2015-03-30 09:45:03 -04:00
|
|
|
resolver.setInlineTemplate(SomeComponent, 'overridden template');
|
|
|
|
var template = resolver.resolve(SomeComponent);
|
2015-04-09 15:20:11 -04:00
|
|
|
expect(template.template).toEqual('overridden template');
|
2015-03-30 09:45:03 -04:00
|
|
|
expect(template.directives).toEqual([SomeDirective]);
|
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
it('should allow overriding an overriden @View', () => {
|
|
|
|
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
|
2015-03-30 09:45:03 -04:00
|
|
|
resolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
|
|
|
|
var template = resolver.resolve(SomeComponent);
|
2015-04-09 15:20:11 -04:00
|
|
|
expect(template.template).toEqual('overridden template x 2');
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow overriding a template after it has been resolved', () => {
|
|
|
|
resolver.resolve(SomeComponent);
|
|
|
|
expect(() => {
|
|
|
|
resolver.setInlineTemplate(SomeComponent, 'overridden template');
|
|
|
|
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('Directive overriding', () => {
|
|
|
|
it('should allow overriding a directive from the default template', () => {
|
2015-05-15 19:42:52 -04:00
|
|
|
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
2015-03-30 09:45:03 -04:00
|
|
|
var template = resolver.resolve(SomeComponent);
|
|
|
|
expect(template.directives.length).toEqual(1);
|
|
|
|
expect(template.directives[0]).toBe(SomeOtherDirective);
|
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
it('should allow overriding a directive from an overriden @View', () => {
|
|
|
|
resolver.setView(SomeComponent, new View({directives: [SomeOtherDirective]}));
|
2015-05-15 19:42:52 -04:00
|
|
|
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
|
2015-03-30 09:45:03 -04:00
|
|
|
var template = resolver.resolve(SomeComponent);
|
|
|
|
expect(template.directives.length).toEqual(1);
|
|
|
|
expect(template.directives[0]).toBe(SomeComponent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when the overridden directive is not present', () => {
|
2015-05-15 19:42:52 -04:00
|
|
|
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
|
2015-03-30 09:45:03 -04:00
|
|
|
expect(() => { resolver.resolve(SomeComponent); })
|
|
|
|
.toThrowError('Overriden directive SomeOtherDirective not found in the template of SomeComponent');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow overriding a directive after its template has been resolved', () => {
|
|
|
|
resolver.resolve(SomeComponent);
|
|
|
|
expect(() => {
|
2015-05-15 19:42:52 -04:00
|
|
|
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
2015-03-30 09:45:03 -04:00
|
|
|
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'cmp'})
|
2015-04-09 15:20:11 -04:00
|
|
|
@View({
|
|
|
|
template: 'template',
|
2015-03-30 09:45:03 -04:00
|
|
|
directives: [SomeDirective],
|
|
|
|
})
|
|
|
|
class SomeComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
class SomeDirective {
|
|
|
|
}
|
|
|
|
|
|
|
|
class SomeOtherDirective {
|
|
|
|
}
|