2015-10-13 03:29:13 -04:00
|
|
|
import {ddescribe, describe, it, iit, expect, beforeEach} from 'angular2/testing_internal';
|
2016-01-06 17:13:44 -05:00
|
|
|
import {DirectiveResolver} from 'angular2/src/compiler/directive_resolver';
|
2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
DirectiveMetadata,
|
|
|
|
Directive,
|
|
|
|
Input,
|
|
|
|
Output,
|
|
|
|
HostBinding,
|
|
|
|
HostListener,
|
|
|
|
ContentChildren,
|
|
|
|
ContentChildrenMetadata,
|
|
|
|
ViewChildren,
|
|
|
|
ViewChildrenMetadata,
|
|
|
|
ContentChild,
|
|
|
|
ContentChildMetadata,
|
|
|
|
ViewChild,
|
|
|
|
ViewChildMetadata
|
|
|
|
} from 'angular2/src/core/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 {
|
2015-09-30 23:59:23 -04:00
|
|
|
@Input() a;
|
2016-04-12 12:40:37 -04:00
|
|
|
@Input("renamed") b;
|
2015-09-03 18:10:48 -04:00
|
|
|
c;
|
|
|
|
}
|
|
|
|
|
2015-09-30 23:59:23 -04:00
|
|
|
@Directive({selector: 'someDirective', outputs: ['c']})
|
|
|
|
class SomeDirectiveWithOutputs {
|
|
|
|
@Output() a;
|
2016-04-12 12:40:37 -04:00
|
|
|
@Output("renamed") b;
|
2015-09-03 18:10:48 -04:00
|
|
|
c;
|
|
|
|
}
|
|
|
|
|
2015-11-04 04:08:51 -05:00
|
|
|
|
|
|
|
@Directive({selector: 'someDirective', outputs: ['a']})
|
|
|
|
class SomeDirectiveWithDuplicateOutputs {
|
|
|
|
@Output() a;
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:21:49 -04:00
|
|
|
@Directive({selector: 'someDirective', properties: ['a']})
|
|
|
|
class SomeDirectiveWithProperties {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someDirective', events: ['a']})
|
|
|
|
class SomeDirectiveWithEvents {
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:10:48 -04:00
|
|
|
@Directive({selector: 'someDirective'})
|
|
|
|
class SomeDirectiveWithSetterProps {
|
2016-04-12 12:40:37 -04:00
|
|
|
@Input("renamed")
|
|
|
|
set a(value) {
|
|
|
|
}
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someDirective'})
|
2015-09-30 23:59:23 -04:00
|
|
|
class SomeDirectiveWithGetterOutputs {
|
2016-04-12 12:40:37 -04:00
|
|
|
@Output("renamed")
|
|
|
|
get a() {
|
|
|
|
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 {
|
|
|
|
@HostBinding() a;
|
2016-04-12 12:40:37 -04:00
|
|
|
@HostBinding("renamed") b;
|
2015-09-04 17:07:16 -04:00
|
|
|
c;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'someDirective', host: {'(c)': 'onC()'}})
|
|
|
|
class SomeDirectiveWithHostListeners {
|
|
|
|
@HostListener('a')
|
2016-04-12 12:40:37 -04:00
|
|
|
onA() {
|
|
|
|
}
|
2015-09-04 17:07:16 -04:00
|
|
|
@HostListener('b', ['$event.value'])
|
2016-04-12 12:40:37 -04:00
|
|
|
onB(value) {
|
|
|
|
}
|
2015-09-04 17:07:16 -04:00
|
|
|
}
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {"cs": new ContentChildren("c")}})
|
2015-09-17 21:45:49 -04:00
|
|
|
class SomeDirectiveWithContentChildren {
|
2016-04-12 12:40:37 -04:00
|
|
|
@ContentChildren("a") as: any;
|
2015-09-17 21:45:49 -04:00
|
|
|
c;
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {"cs": new ViewChildren("c")}})
|
2015-09-17 21:45:49 -04:00
|
|
|
class SomeDirectiveWithViewChildren {
|
2016-04-12 12:40:37 -04:00
|
|
|
@ViewChildren("a") as: any;
|
2015-09-17 21:45:49 -04:00
|
|
|
c;
|
|
|
|
}
|
2015-09-03 18:10:48 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {"c": new ContentChild("c")}})
|
2015-09-19 21:39:35 -04:00
|
|
|
class SomeDirectiveWithContentChild {
|
2016-04-12 12:40:37 -04:00
|
|
|
@ContentChild("a") a: any;
|
2015-09-19 21:39:35 -04:00
|
|
|
c;
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
@Directive({selector: 'someDirective', queries: {"c": new ViewChild("c")}})
|
2015-09-19 21:39:35 -04:00
|
|
|
class SomeDirectiveWithViewChild {
|
2016-04-12 12:40:37 -04:00
|
|
|
@ViewChild("a") a: any;
|
2015-09-19 21:39:35 -04:00
|
|
|
c;
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:10:48 -04:00
|
|
|
class SomeDirectiveWithoutMetadata {}
|
|
|
|
|
|
|
|
export function main() {
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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
|
|
|
|
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'`);
|
|
|
|
});
|
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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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
|
|
|
});
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
}
|