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-04-28 20:50:03 -04:00
|
|
|
import {DirectiveResolver} from '@angular/compiler/src/directive_resolver';
|
2016-07-28 09:31:26 -04:00
|
|
|
import {Component, ComponentMetadata, ContentChild, ContentChildren, Directive, DirectiveMetadata, HostBinding, HostListener, Input, Output, ViewChild, ViewChildren} from '@angular/core/src/metadata';
|
2015-09-03 18:10:48 -04:00
|
|
|
|
|
|
|
@Directive({selector: 'someDirective'})
|
|
|
|
class SomeDirective {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someChildDirective'})
|
|
|
|
class SomeChildDirective extends SomeDirective {
|
|
|
|
}
|
|
|
|
|
2015-09-30 23:59:23 -04:00
|
|
|
@Directive({selector: 'someDirective', inputs: ['c']})
|
2015-10-02 19:21:49 -04:00
|
|
|
class SomeDirectiveWithInputs {
|
2016-07-11 18:27:57 -04:00
|
|
|
@Input() a: any;
|
|
|
|
@Input('renamed') b: any;
|
|
|
|
c: any;
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
|
|
|
|
2015-09-30 23:59:23 -04:00
|
|
|
@Directive({selector: 'someDirective', outputs: ['c']})
|
|
|
|
class SomeDirectiveWithOutputs {
|
2016-07-11 18:27:57 -04:00
|
|
|
@Output() a: any;
|
|
|
|
@Output('renamed') b: any;
|
|
|
|
c: any;
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 04:08:51 -05:00
|
|
|
@Directive({selector: 'someDirective', outputs: ['a']})
|
|
|
|
class SomeDirectiveWithDuplicateOutputs {
|
2016-07-11 18:27:57 -04:00
|
|
|
@Output() a: any;
|
2015-11-04 04:08:51 -05:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:27:57 -04:00
|
|
|
@Directive({selector: 'someDirective', outputs: ['localA: a']})
|
|
|
|
class SomeDirectiveWithDuplicateRenamedOutputs {
|
|
|
|
@Output() a: any;
|
|
|
|
localA: any;
|
2015-10-02 19:21:49 -04:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:01:49 -04:00
|
|
|
@Directive({selector: 'someDirective', inputs: ['a']})
|
|
|
|
class SomeDirectiveWithDuplicateInputs {
|
|
|
|
@Input() a: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someDirective', inputs: ['localA: a']})
|
|
|
|
class SomeDirectiveWithDuplicateRenamedInputs {
|
|
|
|
@Input() a: any;
|
|
|
|
localA: any;
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:10:48 -04:00
|
|
|
@Directive({selector: 'someDirective'})
|
|
|
|
class SomeDirectiveWithSetterProps {
|
2016-06-08 19:38:52 -04:00
|
|
|
@Input('renamed')
|
2016-07-11 18:27:57 -04:00
|
|
|
set a(value: any) {}
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someDirective'})
|
2015-09-30 23:59:23 -04:00
|
|
|
class SomeDirectiveWithGetterOutputs {
|
2016-06-08 19:38:52 -04:00
|
|
|
@Output('renamed')
|
2016-07-11 18:27:57 -04:00
|
|
|
get a(): any { return null; }
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:07:16 -04:00
|
|
|
@Directive({selector: 'someDirective', host: {'[c]': 'c'}})
|
|
|
|
class SomeDirectiveWithHostBindings {
|
2016-07-11 18:27:57 -04:00
|
|
|
@HostBinding() a: any;
|
|
|
|
@HostBinding('renamed') b: any;
|
|
|
|
c: any;
|
2015-09-04 17:07:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someDirective', host: {'(c)': 'onC()'}})
|
|
|
|
class SomeDirectiveWithHostListeners {
|
|
|
|
@HostListener('a')
|
2016-06-08 19:38:52 -04:00
|
|
|
onA() {}
|
2015-09-04 17:07:16 -04:00
|
|
|
@HostListener('b', ['$event.value'])
|
2016-07-11 18:27:57 -04:00
|
|
|
onB(value: any) {}
|
2015-09-04 17:07:16 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {'cs': new ContentChildren('c')}})
|
2015-09-17 21:45:49 -04:00
|
|
|
class SomeDirectiveWithContentChildren {
|
2016-06-08 19:38:52 -04:00
|
|
|
@ContentChildren('a') as: any;
|
2016-07-11 18:27:57 -04:00
|
|
|
c: any;
|
2015-09-17 21:45:49 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {'cs': new ViewChildren('c')}})
|
2015-09-17 21:45:49 -04:00
|
|
|
class SomeDirectiveWithViewChildren {
|
2016-06-08 19:38:52 -04:00
|
|
|
@ViewChildren('a') as: any;
|
2016-07-11 18:27:57 -04:00
|
|
|
c: any;
|
2015-09-17 21:45:49 -04:00
|
|
|
}
|
2015-09-03 18:10:48 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {'c': new ContentChild('c')}})
|
2015-09-19 21:39:35 -04:00
|
|
|
class SomeDirectiveWithContentChild {
|
2016-06-08 19:38:52 -04:00
|
|
|
@ContentChild('a') a: any;
|
2016-07-11 18:27:57 -04:00
|
|
|
c: any;
|
2015-09-19 21:39:35 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {'c': new ViewChild('c')}})
|
2015-09-19 21:39:35 -04:00
|
|
|
class SomeDirectiveWithViewChild {
|
2016-06-08 19:38:52 -04:00
|
|
|
@ViewChild('a') a: any;
|
2016-07-11 18:27:57 -04:00
|
|
|
c: any;
|
2015-09-19 21:39:35 -04:00
|
|
|
}
|
|
|
|
|
2016-08-19 16:51:45 -04:00
|
|
|
@Component({selector: 'sample', template: 'some template', styles: ['some styles']})
|
2016-07-28 09:31:26 -04:00
|
|
|
class ComponentWithTemplate {
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:10:48 -04:00
|
|
|
class SomeDirectiveWithoutMetadata {}
|
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('DirectiveResolver', () => {
|
2016-02-19 14:49:31 -05:00
|
|
|
var resolver: DirectiveResolver;
|
2015-09-03 18:10:48 -04:00
|
|
|
|
|
|
|
beforeEach(() => { resolver = new DirectiveResolver(); });
|
|
|
|
|
|
|
|
it('should read out the Directive metadata', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirective);
|
|
|
|
expect(directiveMetadata)
|
2015-09-04 17:07:16 -04:00
|
|
|
.toEqual(new DirectiveMetadata(
|
2015-09-30 23:59:23 -04:00
|
|
|
{selector: 'someDirective', inputs: [], outputs: [], host: {}, queries: {}}));
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if not matching metadata is found', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
resolver.resolve(SomeDirectiveWithoutMetadata);
|
|
|
|
}).toThrowError('No Directive annotation found on SomeDirectiveWithoutMetadata');
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not read parent class Directive metadata', function() {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeChildDirective);
|
|
|
|
expect(directiveMetadata)
|
2015-09-04 17:07:16 -04:00
|
|
|
.toEqual(new DirectiveMetadata(
|
2015-09-30 23:59:23 -04:00
|
|
|
{selector: 'someChildDirective', inputs: [], outputs: [], host: {}, queries: {}}));
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
2015-09-30 23:59:23 -04:00
|
|
|
describe('inputs', () => {
|
|
|
|
it('should append directive inputs', () => {
|
2015-10-02 19:21:49 -04:00
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithInputs);
|
2015-09-30 23:59:23 -04:00
|
|
|
expect(directiveMetadata.inputs).toEqual(['c', 'a', 'b: renamed']);
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with getters and setters', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithSetterProps);
|
2015-09-30 23:59:23 -04:00
|
|
|
expect(directiveMetadata.inputs).toEqual(['a: renamed']);
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
2015-10-02 19:21:49 -04:00
|
|
|
|
2016-07-11 19:01:49 -04:00
|
|
|
it('should throw if duplicate inputs', () => {
|
|
|
|
expect(() => {
|
|
|
|
resolver.resolve(SomeDirectiveWithDuplicateInputs);
|
|
|
|
}).toThrowError(`Input 'a' defined multiple times in 'SomeDirectiveWithDuplicateInputs'`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if duplicate inputs (with rename)', () => {
|
|
|
|
expect(() => { resolver.resolve(SomeDirectiveWithDuplicateRenamedInputs); })
|
|
|
|
.toThrowError(
|
|
|
|
`Input 'a' defined multiple times in 'SomeDirectiveWithDuplicateRenamedInputs'`);
|
|
|
|
});
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
2015-09-30 23:59:23 -04:00
|
|
|
describe('outputs', () => {
|
|
|
|
it('should append directive outputs', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithOutputs);
|
|
|
|
expect(directiveMetadata.outputs).toEqual(['c', 'a', 'b: renamed']);
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with getters and setters', () => {
|
2015-09-30 23:59:23 -04:00
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithGetterOutputs);
|
|
|
|
expect(directiveMetadata.outputs).toEqual(['a: renamed']);
|
2015-09-04 17:07:16 -04:00
|
|
|
});
|
2015-11-04 04:08:51 -05:00
|
|
|
|
|
|
|
it('should throw if duplicate outputs', () => {
|
|
|
|
expect(() => { resolver.resolve(SomeDirectiveWithDuplicateOutputs); })
|
|
|
|
.toThrowError(
|
|
|
|
`Output event 'a' defined multiple times in 'SomeDirectiveWithDuplicateOutputs'`);
|
|
|
|
});
|
2016-07-11 19:01:49 -04:00
|
|
|
|
|
|
|
it('should throw if duplicate outputs (with rename)', () => {
|
|
|
|
expect(() => { resolver.resolve(SomeDirectiveWithDuplicateRenamedOutputs); })
|
|
|
|
.toThrowError(
|
|
|
|
`Output event 'a' defined multiple times in 'SomeDirectiveWithDuplicateRenamedOutputs'`);
|
|
|
|
});
|
2015-09-04 17:07:16 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('host', () => {
|
|
|
|
it('should append host bindings', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithHostBindings);
|
|
|
|
expect(directiveMetadata.host).toEqual({'[c]': 'c', '[a]': 'a', '[renamed]': 'b'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should append host listeners', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithHostListeners);
|
|
|
|
expect(directiveMetadata.host)
|
|
|
|
.toEqual({'(c)': 'onC()', '(a)': 'onA()', '(b)': 'onB($event.value)'});
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
});
|
2015-09-17 21:45:49 -04:00
|
|
|
|
|
|
|
describe('queries', () => {
|
|
|
|
it('should append ContentChildren', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithContentChildren);
|
|
|
|
expect(directiveMetadata.queries)
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual({'cs': new ContentChildren('c'), 'as': new ContentChildren('a')});
|
2015-09-17 21:45:49 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should append ViewChildren', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithViewChildren);
|
|
|
|
expect(directiveMetadata.queries)
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual({'cs': new ViewChildren('c'), 'as': new ViewChildren('a')});
|
2015-09-17 21:45:49 -04:00
|
|
|
});
|
2015-09-19 21:39:35 -04:00
|
|
|
|
|
|
|
it('should append ContentChild', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithContentChild);
|
|
|
|
expect(directiveMetadata.queries)
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual({'c': new ContentChild('c'), 'a': new ContentChild('a')});
|
2015-09-19 21:39:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should append ViewChild', () => {
|
|
|
|
var directiveMetadata = resolver.resolve(SomeDirectiveWithViewChild);
|
|
|
|
expect(directiveMetadata.queries)
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual({'c': new ViewChild('c'), 'a': new ViewChild('a')});
|
2015-09-19 21:39:35 -04:00
|
|
|
});
|
2015-09-17 21:45:49 -04:00
|
|
|
});
|
2016-07-28 09:31:26 -04:00
|
|
|
|
|
|
|
describe('view', () => {
|
|
|
|
it('should read out the template related metadata from the Component metadata', () => {
|
|
|
|
var compMetadata = <ComponentMetadata>resolver.resolve(ComponentWithTemplate);
|
|
|
|
expect(compMetadata.template).toEqual('some template');
|
|
|
|
expect(compMetadata.styles).toEqual(['some styles']);
|
|
|
|
});
|
|
|
|
});
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
}
|