2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
it,
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {stringify} from '../src/facade/lang';
|
|
|
|
import {MockViewResolver} from '../testing';
|
|
|
|
import {Component, ViewMetadata} from '@angular/core';
|
|
|
|
import {isBlank} from '../src/facade/lang';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-06-24 04:54:04 -04:00
|
|
|
describe('MockViewResolver', () => {
|
2016-02-19 14:49:31 -05:00
|
|
|
var viewResolver: MockViewResolver;
|
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-12-16 02:47:48 -05:00
|
|
|
it('should allow overriding an overridden @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-12-16 02:47:48 -05:00
|
|
|
it('should allow overriding a directive from an overridden @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 {}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'cmp',
|
2015-04-09 15:20:11 -04:00
|
|
|
template: 'template',
|
2015-03-30 09:45:03 -04:00
|
|
|
directives: [SomeDirective],
|
|
|
|
})
|
|
|
|
class SomeComponent {
|
|
|
|
}
|
|
|
|
|
2015-06-02 20:41:44 -04:00
|
|
|
class SomeOtherDirective {}
|