2015-03-30 09:45:03 -04:00
|
|
|
import {
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
it,
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {stringify} from 'angular2/src/core/facade/lang';
|
2015-06-02 20:41:44 -04:00
|
|
|
|
2015-06-24 04:54:04 -04:00
|
|
|
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2015-08-20 07:51:48 -04:00
|
|
|
import {Component, View, ViewMetadata} from 'angular2/metadata';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {isBlank} from 'angular2/src/core/facade/lang';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-06-24 04:54:04 -04:00
|
|
|
describe('MockViewResolver', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
var viewResolver;
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2015-06-24 05:10:29 -04:00
|
|
|
beforeEach(() => { viewResolver = new MockViewResolver(); });
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
describe('View overriding', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
it('should fallback to the default ViewResolver when templates are not overridden', () => {
|
|
|
|
var view = viewResolver.resolve(SomeComponent);
|
|
|
|
expect(view.template).toEqual('template');
|
|
|
|
expect(view.directives).toEqual([SomeDirective]);
|
|
|
|
});
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
it('should allow overriding the @View', () => {
|
2015-08-20 07:51:48 -04:00
|
|
|
viewResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
2015-06-24 05:10:29 -04:00
|
|
|
var view = viewResolver.resolve(SomeComponent);
|
|
|
|
expect(view.template).toEqual('overridden template');
|
|
|
|
expect(isBlank(view.directives)).toBe(true);
|
2015-03-30 09:45:03 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-06-24 04:54:04 -04:00
|
|
|
it('should not allow overriding a view after it has been resolved', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.resolve(SomeComponent);
|
2015-06-03 16:42:57 -04:00
|
|
|
expect(() => {
|
2015-08-20 07:51:48 -04:00
|
|
|
viewResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
2015-06-03 16:42:57 -04:00
|
|
|
})
|
2015-06-02 20:41:44 -04:00
|
|
|
.toThrowError(
|
|
|
|
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
describe('inline template definition overriding', () => {
|
|
|
|
it('should allow overriding the default template', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.setInlineTemplate(SomeComponent, 'overridden template');
|
|
|
|
var view = viewResolver.resolve(SomeComponent);
|
|
|
|
expect(view.template).toEqual('overridden template');
|
|
|
|
expect(view.directives).toEqual([SomeDirective]);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
it('should allow overriding an overriden @View', () => {
|
2015-08-20 07:51:48 -04:00
|
|
|
viewResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
|
|
|
|
var view = viewResolver.resolve(SomeComponent);
|
|
|
|
expect(view.template).toEqual('overridden template x 2');
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
2015-06-24 04:54:04 -04:00
|
|
|
it('should not allow overriding a view after it has been resolved', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.resolve(SomeComponent);
|
|
|
|
expect(() => { viewResolver.setInlineTemplate(SomeComponent, 'overridden template'); })
|
2015-06-02 20:41:44 -04:00
|
|
|
.toThrowError(
|
|
|
|
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('Directive overriding', () => {
|
2015-06-24 04:54:04 -04:00
|
|
|
it('should allow overriding a directive from the default view', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
|
|
|
var view = viewResolver.resolve(SomeComponent);
|
|
|
|
expect(view.directives.length).toEqual(1);
|
|
|
|
expect(view.directives[0]).toBe(SomeOtherDirective);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
it('should allow overriding a directive from an overriden @View', () => {
|
2015-08-20 07:51:48 -04:00
|
|
|
viewResolver.setView(SomeComponent, new ViewMetadata({directives: [SomeOtherDirective]}));
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
|
|
|
|
var view = viewResolver.resolve(SomeComponent);
|
|
|
|
expect(view.directives.length).toEqual(1);
|
|
|
|
expect(view.directives[0]).toBe(SomeComponent);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when the overridden directive is not present', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
|
|
|
|
expect(() => { viewResolver.resolve(SomeComponent); })
|
2015-06-02 20:41:44 -04:00
|
|
|
.toThrowError(
|
|
|
|
`Overriden directive ${stringify(SomeOtherDirective)} not found in the template of ${stringify(SomeComponent)}`);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
2015-06-24 04:54:04 -04:00
|
|
|
it('should not allow overriding a directive after its view has been resolved', () => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.resolve(SomeComponent);
|
2015-06-03 16:42:57 -04:00
|
|
|
expect(() => {
|
2015-06-24 05:10:29 -04:00
|
|
|
viewResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
2015-06-03 16:42:57 -04:00
|
|
|
})
|
2015-06-02 20:41:44 -04:00
|
|
|
.toThrowError(
|
|
|
|
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-02 20:41:44 -04:00
|
|
|
class SomeDirective {}
|
|
|
|
|
2015-03-30 09:45:03 -04:00
|
|
|
@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 {
|
|
|
|
}
|
|
|
|
|
2015-06-02 20:41:44 -04:00
|
|
|
class SomeOtherDirective {}
|