2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-16 16:59:06 -04:00
|
|
|
import {Component, ComponentMetadata, Injector} from '@angular/core';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {beforeEach, ddescribe, describe, expect, iit, inject, it} from '@angular/core/testing/testing_internal';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2016-08-16 16:59:06 -04:00
|
|
|
import {ViewMetadata} from '../core_private';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {isBlank, stringify} from '../src/facade/lang';
|
2016-07-28 09:31:26 -04:00
|
|
|
import {MockDirectiveResolver} from '../testing';
|
2015-03-30 09:45:03 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-07-28 09:31:26 -04:00
|
|
|
describe('MockDirectiveResolver', () => {
|
|
|
|
var dirResolver: MockDirectiveResolver;
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2016-07-28 09:31:26 -04:00
|
|
|
beforeEach(inject([Injector], (injector: Injector) => {
|
|
|
|
dirResolver = new MockDirectiveResolver(injector);
|
|
|
|
}));
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2016-07-28 09:45:22 -04:00
|
|
|
describe('Directive overriding', () => {
|
|
|
|
it('should fallback to the default DirectiveResolver when templates are not overridden',
|
|
|
|
() => {
|
|
|
|
var ngModule = dirResolver.resolve(SomeComponent);
|
|
|
|
expect(ngModule.selector).toEqual('cmp');
|
|
|
|
});
|
|
|
|
|
2016-07-28 07:54:49 -04:00
|
|
|
it('should allow overriding the @Directive', () => {
|
2016-07-28 09:45:22 -04:00
|
|
|
dirResolver.setDirective(
|
|
|
|
SomeComponent, new ComponentMetadata({selector: 'someOtherSelector'}));
|
|
|
|
var metadata = dirResolver.resolve(SomeComponent);
|
|
|
|
expect(metadata.selector).toEqual('someOtherSelector');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
expect(view.template).toEqual('overridden template');
|
|
|
|
expect(isBlank(view.directives)).toBe(true);
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
it('should allow overriding a view after it has been resolved', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.resolve(SomeComponent);
|
|
|
|
dirResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2016-06-24 11:46:43 -04:00
|
|
|
expect(view.template).toEqual('overridden template');
|
|
|
|
expect(isBlank(view.directives)).toBe(true);
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.setInlineTemplate(SomeComponent, 'overridden template');
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.setView(SomeComponent, new ViewMetadata({template: 'overridden template'}));
|
|
|
|
dirResolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
expect(view.template).toEqual('overridden template x 2');
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
it('should allow overriding a view after it has been resolved', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.resolve(SomeComponent);
|
|
|
|
dirResolver.setInlineTemplate(SomeComponent, 'overridden template');
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2016-06-24 11:46:43 -04:00
|
|
|
expect(view.template).toEqual('overridden template');
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.setView(SomeComponent, new ViewMetadata({directives: [SomeOtherDirective]}));
|
|
|
|
dirResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
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', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
|
|
|
|
expect(() => { dirResolver.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
|
|
|
});
|
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
it('should allow overriding a directive after its view has been resolved', () => {
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver.resolve(SomeComponent);
|
|
|
|
dirResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
|
|
|
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);
|
2016-06-24 11:46:43 -04:00
|
|
|
expect(view.directives.length).toEqual(1);
|
|
|
|
expect(view.directives[0]).toBe(SomeOtherDirective);
|
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 {}
|