2015-03-23 17:10:55 -04:00
|
|
|
import {describe, beforeEach, it, xit, expect, iit, ddescribe, el} from 'angular2/test_lib';
|
2015-04-27 18:14:30 -04:00
|
|
|
import {isPresent, isBlank, assertionsEnabled} from 'angular2/src/facade/lang';
|
2015-03-23 17:10:55 -04:00
|
|
|
import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {DirectiveParser} from 'angular2/src/render/dom/compiler/directive_parser';
|
|
|
|
import {CompilePipeline} from 'angular2/src/render/dom/compiler/compile_pipeline';
|
|
|
|
import {CompileStep} from 'angular2/src/render/dom/compiler/compile_step';
|
|
|
|
import {CompileElement} from 'angular2/src/render/dom/compiler/compile_element';
|
|
|
|
import {CompileControl} from 'angular2/src/render/dom/compiler/compile_control';
|
2015-04-09 15:20:11 -04:00
|
|
|
import {ViewDefinition, DirectiveMetadata} from 'angular2/src/render/api';
|
2015-03-23 17:10:55 -04:00
|
|
|
import {Lexer, Parser} from 'angular2/change_detection';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('DirectiveParser', () => {
|
|
|
|
var parser, annotatedDirectives;
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
|
|
annotatedDirectives = [
|
|
|
|
someComponent,
|
|
|
|
someComponent2,
|
2015-04-30 16:38:40 -04:00
|
|
|
someDirective,
|
|
|
|
someDirectiveIgnoringChildren,
|
2015-04-27 18:14:30 -04:00
|
|
|
decoratorWithMultipleAttrs,
|
2015-04-30 16:38:40 -04:00
|
|
|
someDirectiveWithProps,
|
|
|
|
someDirectiveWithHostProperties,
|
|
|
|
someDirectiveWithEvents,
|
|
|
|
someDirectiveWithGlobalEvents
|
2015-03-23 17:10:55 -04:00
|
|
|
];
|
|
|
|
parser = new Parser(new Lexer());
|
|
|
|
});
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
function createPipeline(propertyBindings = null, directives = null) {
|
|
|
|
if (isBlank(directives)) directives = annotatedDirectives;
|
|
|
|
|
2015-03-23 17:10:55 -04:00
|
|
|
return new CompilePipeline([
|
|
|
|
new MockStep( (parent, current, control) => {
|
|
|
|
if (isPresent(propertyBindings)) {
|
|
|
|
StringMapWrapper.forEach(propertyBindings, (ast, name) => {
|
|
|
|
current.bindElement().bindProperty(name, ast);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
2015-04-27 18:14:30 -04:00
|
|
|
new DirectiveParser(parser, directives)
|
2015-03-23 17:10:55 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
function process(el, propertyBindings = null, directives = null) {
|
|
|
|
var pipeline = createPipeline(propertyBindings, directives);
|
2015-03-23 17:10:55 -04:00
|
|
|
return ListWrapper.map(pipeline.process(el), (ce) => ce.inheritedElementBinder );
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not add directives if they are not used', () => {
|
|
|
|
var results = process(el('<div></div>'));
|
|
|
|
expect(results[0]).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect directives in attributes', () => {
|
|
|
|
var results = process(el('<div some-decor></div>'));
|
|
|
|
expect(results[0].directives[0].directiveIndex).toBe(
|
2015-04-30 16:38:40 -04:00
|
|
|
annotatedDirectives.indexOf(someDirective)
|
2015-03-23 17:10:55 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-03-19 18:38:48 -04:00
|
|
|
it('should detect directives with multiple attributes', () => {
|
|
|
|
var results = process(el('<input type=text control=one></input>'));
|
|
|
|
expect(results[0].directives[0].directiveIndex).toBe(
|
2015-04-27 18:14:30 -04:00
|
|
|
annotatedDirectives.indexOf(decoratorWithMultipleAttrs)
|
2015-03-19 18:38:48 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-03-23 17:10:55 -04:00
|
|
|
it('should compile children by default', () => {
|
|
|
|
var results = createPipeline().process(el('<div some-decor></div>'));
|
|
|
|
expect(results[0].compileChildren).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should stop compiling children when specified in the directive config', () => {
|
|
|
|
var results = createPipeline().process(el('<div some-decor-ignoring-children></div>'));
|
|
|
|
expect(results[0].compileChildren).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should bind directive properties from bound properties', () => {
|
|
|
|
var results = process(
|
|
|
|
el('<div some-decor-props></div>'),
|
|
|
|
{
|
|
|
|
'elProp': parser.parseBinding('someExpr', '')
|
|
|
|
}
|
|
|
|
);
|
|
|
|
var directiveBinding = results[0].directives[0];
|
|
|
|
expect(MapWrapper.get(directiveBinding.propertyBindings, 'dirProp').source)
|
|
|
|
.toEqual('someExpr');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should bind directive properties with pipes', () => {
|
|
|
|
var results = process(
|
|
|
|
el('<div some-decor-props></div>'),
|
|
|
|
{
|
|
|
|
'elProp': parser.parseBinding('someExpr', '')
|
|
|
|
}
|
|
|
|
);
|
|
|
|
var directiveBinding = results[0].directives[0];
|
|
|
|
var pipedProp = MapWrapper.get(directiveBinding.propertyBindings, 'doubleProp');
|
|
|
|
var simpleProp = MapWrapper.get(directiveBinding.propertyBindings, 'dirProp');
|
|
|
|
expect(pipedProp.ast.name).toEqual('double');
|
|
|
|
expect(pipedProp.ast.exp).toEqual(simpleProp.ast);
|
|
|
|
expect(simpleProp.source).toEqual('someExpr');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should bind directive properties from attribute values', () => {
|
|
|
|
var results = process(
|
|
|
|
el('<div some-decor-props el-prop="someValue"></div>')
|
|
|
|
);
|
|
|
|
var directiveBinding = results[0].directives[0];
|
|
|
|
var simpleProp = MapWrapper.get(directiveBinding.propertyBindings, 'dirProp');
|
|
|
|
expect(simpleProp.source).toEqual('someValue');
|
|
|
|
});
|
|
|
|
|
2015-04-21 14:47:53 -04:00
|
|
|
it('should bind host directive properties', () => {
|
|
|
|
var element = el('<input some-decor-with-host-props>');
|
2015-03-23 17:10:55 -04:00
|
|
|
var results = process(element);
|
2015-04-21 14:47:53 -04:00
|
|
|
|
|
|
|
var directiveBinding = results[0].directives[0];
|
|
|
|
|
|
|
|
var ast = MapWrapper.get(directiveBinding.hostPropertyBindings, 'hostProperty');
|
|
|
|
expect(ast.source).toEqual('dirProp');
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
|
2015-04-02 17:40:49 -04:00
|
|
|
it('should read attribute values', () => {
|
|
|
|
var element = el('<input some-decor-props some-attr="someValue">');
|
|
|
|
var results = process(element);
|
|
|
|
expect(MapWrapper.get(results[0].readAttributes, 'some-attr')).toEqual('someValue');
|
|
|
|
});
|
|
|
|
|
2015-03-23 17:10:55 -04:00
|
|
|
it('should bind directive events', () => {
|
|
|
|
var results = process(
|
|
|
|
el('<div some-decor-events></div>')
|
|
|
|
);
|
|
|
|
var directiveBinding = results[0].directives[0];
|
2015-04-02 09:56:58 -04:00
|
|
|
expect(directiveBinding.eventBindings.length).toEqual(1);
|
|
|
|
var eventBinding = directiveBinding.eventBindings[0];
|
|
|
|
expect(eventBinding.fullName).toEqual('click');
|
|
|
|
expect(eventBinding.source.source).toEqual('doIt()');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should bind directive global events', () => {
|
|
|
|
var results = process(
|
|
|
|
el('<div some-decor-globalevents></div>')
|
|
|
|
);
|
|
|
|
var directiveBinding = results[0].directives[0];
|
|
|
|
expect(directiveBinding.eventBindings.length).toEqual(1);
|
|
|
|
var eventBinding = directiveBinding.eventBindings[0];
|
|
|
|
expect(eventBinding.fullName).toEqual('window:resize');
|
|
|
|
expect(eventBinding.source.source).toEqual('doItGlobal()');
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
|
2015-04-14 05:31:35 -04:00
|
|
|
//TODO: assertions should be enabled when running tests: https://github.com/angular/angular/issues/1340
|
2015-03-23 17:10:55 -04:00
|
|
|
describe('component directives', () => {
|
|
|
|
it('should save the component id', () => {
|
|
|
|
var results = process(
|
2015-04-27 18:14:30 -04:00
|
|
|
el('<some-comp></some-comp>')
|
2015-03-23 17:10:55 -04:00
|
|
|
);
|
|
|
|
expect(results[0].componentId).toEqual('someComponent');
|
|
|
|
});
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
it('should throw when the provided selector is not an element selector', () => {
|
|
|
|
expect( () => {
|
|
|
|
createPipeline(null, [componentWithNonElementSelector]);
|
|
|
|
}).toThrowError(`Component 'componentWithNonElementSelector' can only have an element selector, but had '[attr]'`);
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
it('should not allow multiple component directives on the same element', () => {
|
2015-03-23 17:10:55 -04:00
|
|
|
expect( () => {
|
|
|
|
process(
|
2015-04-27 18:14:30 -04:00
|
|
|
el('<some-comp></some-comp>'),
|
|
|
|
null,
|
|
|
|
[someComponent, someComponentDup]
|
2015-03-23 17:10:55 -04:00
|
|
|
);
|
2015-04-27 18:14:30 -04:00
|
|
|
}).toThrowError(new RegExp('Only one component directive is allowed per element' ));
|
|
|
|
});
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockStep extends CompileStep {
|
|
|
|
processClosure:Function;
|
|
|
|
constructor(process) {
|
|
|
|
super();
|
|
|
|
this.processClosure = process;
|
|
|
|
}
|
|
|
|
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
|
|
|
|
this.processClosure(parent, current, control);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var someComponent = new DirectiveMetadata({
|
2015-04-27 18:14:30 -04:00
|
|
|
selector: 'some-comp',
|
2015-03-23 17:10:55 -04:00
|
|
|
id: 'someComponent',
|
|
|
|
type: DirectiveMetadata.COMPONENT_TYPE
|
|
|
|
});
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
var someComponentDup = new DirectiveMetadata({
|
|
|
|
selector: 'some-comp',
|
|
|
|
id: 'someComponentDup',
|
2015-03-23 17:10:55 -04:00
|
|
|
type: DirectiveMetadata.COMPONENT_TYPE
|
|
|
|
});
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
var someComponent2 = new DirectiveMetadata({
|
|
|
|
selector: 'some-comp2',
|
|
|
|
id: 'someComponent2',
|
2015-03-19 18:38:48 -04:00
|
|
|
type: DirectiveMetadata.COMPONENT_TYPE
|
|
|
|
});
|
|
|
|
|
2015-04-30 16:38:40 -04:00
|
|
|
var someDirective = new DirectiveMetadata({
|
2015-04-27 18:14:30 -04:00
|
|
|
selector: '[some-decor]',
|
2015-04-30 16:38:40 -04:00
|
|
|
type: DirectiveMetadata.DIRECTIVE_TYPE
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
|
2015-04-30 16:38:40 -04:00
|
|
|
var someDirectiveIgnoringChildren = new DirectiveMetadata({
|
2015-03-23 17:10:55 -04:00
|
|
|
selector: '[some-decor-ignoring-children]',
|
2015-04-27 18:14:30 -04:00
|
|
|
compileChildren: false,
|
2015-04-30 16:38:40 -04:00
|
|
|
type: DirectiveMetadata.DIRECTIVE_TYPE
|
2015-04-27 18:14:30 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
var decoratorWithMultipleAttrs = new DirectiveMetadata({
|
|
|
|
selector: 'input[type=text][control]',
|
|
|
|
id: 'decoratorWithMultipleAttrs',
|
2015-04-30 16:38:40 -04:00
|
|
|
type: DirectiveMetadata.DIRECTIVE_TYPE
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
|
2015-04-30 16:38:40 -04:00
|
|
|
var someDirectiveWithProps = new DirectiveMetadata({
|
2015-03-23 17:10:55 -04:00
|
|
|
selector: '[some-decor-props]',
|
2015-04-09 15:20:11 -04:00
|
|
|
properties: MapWrapper.createFromStringMap({
|
2015-03-23 17:10:55 -04:00
|
|
|
'dirProp': 'elProp',
|
|
|
|
'doubleProp': 'elProp | double'
|
|
|
|
}),
|
2015-04-02 17:40:49 -04:00
|
|
|
readAttributes: ['some-attr']
|
2015-03-23 17:10:55 -04:00
|
|
|
});
|
|
|
|
|
2015-04-30 16:38:40 -04:00
|
|
|
var someDirectiveWithHostProperties = new DirectiveMetadata({
|
2015-04-21 14:47:53 -04:00
|
|
|
selector: '[some-decor-with-host-props]',
|
|
|
|
hostProperties: MapWrapper.createFromStringMap({
|
|
|
|
'dirProp': 'hostProperty'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2015-04-30 16:38:40 -04:00
|
|
|
var someDirectiveWithEvents = new DirectiveMetadata({
|
2015-03-23 17:10:55 -04:00
|
|
|
selector: '[some-decor-events]',
|
2015-04-09 15:20:11 -04:00
|
|
|
hostListeners: MapWrapper.createFromStringMap({
|
2015-03-23 17:10:55 -04:00
|
|
|
'click': 'doIt()'
|
|
|
|
})
|
|
|
|
});
|
2015-04-02 09:56:58 -04:00
|
|
|
|
2015-04-30 16:38:40 -04:00
|
|
|
var someDirectiveWithGlobalEvents = new DirectiveMetadata({
|
2015-04-02 09:56:58 -04:00
|
|
|
selector: '[some-decor-globalevents]',
|
|
|
|
hostListeners: MapWrapper.createFromStringMap({
|
|
|
|
'window:resize': 'doItGlobal()'
|
|
|
|
})
|
|
|
|
});
|
2015-04-27 18:14:30 -04:00
|
|
|
|
|
|
|
var componentWithNonElementSelector = new DirectiveMetadata({
|
|
|
|
id: 'componentWithNonElementSelector',
|
|
|
|
selector: '[attr]',
|
|
|
|
type: DirectiveMetadata.COMPONENT_TYPE
|
|
|
|
});
|