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
|
|
|
|
*/
|
|
|
|
|
2017-02-17 15:55:55 -05:00
|
|
|
import {Component, Directive, Injector, ɵViewMetadata as ViewMetadata} from '@angular/core';
|
2016-08-19 16:51:45 -04:00
|
|
|
import {TestBed, inject} from '@angular/core/testing';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {MockDirectiveResolver} from '../testing';
|
2016-08-30 21:07:40 -04:00
|
|
|
|
2015-03-30 09:45:03 -04:00
|
|
|
export function main() {
|
2016-07-28 09:31:26 -04:00
|
|
|
describe('MockDirectiveResolver', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let dirResolver: MockDirectiveResolver;
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2016-08-19 16:51:45 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule(
|
|
|
|
{declarations: [SomeDirective, SomeOtherDirective, SomeComponent]});
|
|
|
|
});
|
|
|
|
|
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',
|
|
|
|
() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const ngModule = dirResolver.resolve(SomeComponent);
|
2016-07-28 09:45:22 -04:00
|
|
|
expect(ngModule.selector).toEqual('cmp');
|
|
|
|
});
|
|
|
|
|
2016-07-28 07:54:49 -04:00
|
|
|
it('should allow overriding the @Directive', () => {
|
2016-09-12 22:14:17 -04:00
|
|
|
dirResolver.setDirective(SomeComponent, new Component({selector: 'someOtherSelector'}));
|
2016-11-12 08:08:58 -05:00
|
|
|
const metadata = dirResolver.resolve(SomeComponent);
|
2016-07-28 09:45:22 -04:00
|
|
|
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-11-12 08:08:58 -05:00
|
|
|
const view = <Component>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
expect(view.template).toEqual('template');
|
|
|
|
});
|
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'}));
|
2016-11-12 08:08:58 -05:00
|
|
|
const view = <Component>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
expect(view.template).toEqual('overridden template');
|
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'}));
|
2016-11-12 08:08:58 -05:00
|
|
|
const view = <Component>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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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');
|
2016-11-12 08:08:58 -05:00
|
|
|
const view = <Component>dirResolver.resolve(SomeComponent);
|
2015-06-24 05:10:29 -04:00
|
|
|
expect(view.template).toEqual('overridden template');
|
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');
|
2016-11-12 08:08:58 -05:00
|
|
|
const view = <Component>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');
|
2016-11-12 08:08:58 -05:00
|
|
|
const view = <Component>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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-19 16:51:45 -04:00
|
|
|
@Directive({selector: 'some-directive'})
|
|
|
|
class SomeDirective {
|
|
|
|
}
|
2015-06-02 20:41:44 -04:00
|
|
|
|
2016-08-19 16:51:45 -04:00
|
|
|
@Component({selector: 'cmp', template: 'template'})
|
2015-03-30 09:45:03 -04:00
|
|
|
class SomeComponent {
|
|
|
|
}
|
|
|
|
|
2016-08-19 16:51:45 -04:00
|
|
|
@Directive({selector: 'some-other-directive'})
|
|
|
|
class SomeOtherDirective {
|
|
|
|
}
|