import { ddescribe, describe, xdescribe, it, iit, xit, expect, beforeEach, afterEach, AsyncTestCompleter, inject, beforeEachBindings } from 'angular2/testing_internal'; import { CONST_EXPR, stringify, isType, Type, isBlank, serializeEnum, IS_DART } from 'angular2/src/facade/lang'; import {MapWrapper} from 'angular2/src/facade/collection'; import {PromiseWrapper, Promise} from 'angular2/src/facade/async'; import {TemplateParser} from 'angular2/src/compiler/template_parser'; import { CommandVisitor, TextCmd, NgContentCmd, BeginElementCmd, BeginComponentCmd, EmbeddedTemplateCmd, TemplateCmd, visitAllCommands, CompiledComponentTemplate } from 'angular2/src/core/linker/template_commands'; import {CommandCompiler} from 'angular2/src/compiler/command_compiler'; import { CompileDirectiveMetadata, CompileTypeMetadata, CompileTemplateMetadata } from 'angular2/src/compiler/directive_metadata'; import {SourceModule, SourceExpression, moduleRef} from 'angular2/src/compiler/source_module'; import {ViewEncapsulation} from 'angular2/src/core/metadata/view'; import {evalModule} from './eval_module'; import { escapeSingleQuoteString, codeGenValueFn, codeGenExportVariable, codeGenConstConstructorCall, MODULE_SUFFIX } from 'angular2/src/compiler/util'; import {TEST_PROVIDERS} from './test_bindings'; const BEGIN_ELEMENT = 'BEGIN_ELEMENT'; const END_ELEMENT = 'END_ELEMENT'; const BEGIN_COMPONENT = 'BEGIN_COMPONENT'; const END_COMPONENT = 'END_COMPONENT'; const TEXT = 'TEXT'; const NG_CONTENT = 'NG_CONTENT'; const EMBEDDED_TEMPLATE = 'EMBEDDED_TEMPLATE'; // Attention: These module names have to correspond to real modules! var THIS_MODULE_URL = `package:angular2/test/compiler/command_compiler_spec${MODULE_SUFFIX}`; var THIS_MODULE_REF = moduleRef(THIS_MODULE_URL); var TEMPLATE_COMMANDS_MODULE_REF = moduleRef(`package:angular2/src/core/linker/template_commands${MODULE_SUFFIX}`); // Attention: read by eval! export class RootComp {} export class SomeDir {} export class AComp {} var RootCompTypeMeta = new CompileTypeMetadata({name: 'RootComp', runtime: RootComp, moduleUrl: THIS_MODULE_URL}); var SomeDirTypeMeta = new CompileTypeMetadata({name: 'SomeDir', runtime: SomeDir, moduleUrl: THIS_MODULE_URL}); var ACompTypeMeta = new CompileTypeMetadata({name: 'AComp', runtime: AComp, moduleUrl: THIS_MODULE_URL}); var compTypeTemplateId: Map = MapWrapper.createFromPairs( [[RootCompTypeMeta, 'rootCompId'], [SomeDirTypeMeta, 'someDirId'], [ACompTypeMeta, 'aCompId']]); export function main() { describe('CommandCompiler', () => { beforeEachBindings(() => TEST_PROVIDERS); var parser: TemplateParser; var commandCompiler: CommandCompiler; var componentTemplateFactory: Function; beforeEach(inject([TemplateParser, CommandCompiler], (_templateParser, _commandCompiler) => { parser = _templateParser; commandCompiler = _commandCompiler; })); function createComp({type, selector, template, encapsulation, ngContentSelectors}: { type?: CompileTypeMetadata, selector?: string, template?: string, encapsulation?: ViewEncapsulation, ngContentSelectors?: string[] }): CompileDirectiveMetadata { if (isBlank(encapsulation)) { encapsulation = ViewEncapsulation.None; } if (isBlank(selector)) { selector = 'root'; } if (isBlank(ngContentSelectors)) { ngContentSelectors = []; } if (isBlank(template)) { template = ''; } return CompileDirectiveMetadata.create({ selector: selector, isComponent: true, type: type, template: new CompileTemplateMetadata({ template: template, ngContentSelectors: ngContentSelectors, encapsulation: encapsulation }) }); } function createDirective(type: CompileTypeMetadata, selector: string, exportAs: string = null): CompileDirectiveMetadata { return CompileDirectiveMetadata.create( {selector: selector, exportAs: exportAs, isComponent: false, type: type}); } function createTests(run: Function) { describe('text', () => { it('should create unbound text commands', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: 'a'}); run(rootComp, []) .then((data) => { expect(data).toEqual([[TEXT, 'a', false, null]]); async.done(); }); })); it('should create bound text commands', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: '{{a}}'}); run(rootComp, []) .then((data) => { expect(data).toEqual([[TEXT, null, true, null]]); async.done(); }); })); }); describe('elements', () => { it('should create unbound element commands', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: '
'}); run(rootComp, []) .then((data) => { expect(data).toEqual([ [BEGIN_ELEMENT, 'div', ['a', 'b'], [], [], [], false, null], [END_ELEMENT] ]); async.done(); }); })); it('should create bound element commands', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({ type: RootCompTypeMeta, template: '
' }); run(rootComp, []) .then((data) => { expect(data).toEqual([ [ BEGIN_ELEMENT, 'div', ['a', 'b'], [null, 'click', 'window', 'scroll'], ['someVar', null], [], true, null ], [END_ELEMENT] ]); async.done(); }); })); it('should create element commands with directives', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: '
'}); var dir = CompileDirectiveMetadata.create({ selector: '[a]', exportAs: 'someExport', isComponent: false, type: SomeDirTypeMeta, host: {'(click)': 'doIt()', '(window:scroll)': 'doIt()', 'role': 'button'} }); run(rootComp, [dir]) .then((data) => { expect(data).toEqual([ [ BEGIN_ELEMENT, 'div', ['a', '', 'role', 'button'], [null, 'click', 'window', 'scroll'], ['someVar', 0], ['SomeDirType'], true, null ], [END_ELEMENT] ]); async.done(); }); })); it('should merge element attributes with host attributes', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({ type: RootCompTypeMeta, template: '
' }); var dir = CompileDirectiveMetadata.create({ selector: 'div', isComponent: false, type: SomeDirTypeMeta, host: {'class': 'newclass', 'style': 'newstyle', 'role': 'newrole', 'attr2': ''} }); run(rootComp, [dir]) .then((data) => { expect(data).toEqual([ [ BEGIN_ELEMENT, 'div', [ 'attr1', '', 'attr2', '', 'class', 'origclass newclass', 'role', 'newrole', 'style', 'color: red; newstyle' ], [], [], ['SomeDirType'], true, null ], [END_ELEMENT] ]); async.done(); }); })); it('should create nested nodes', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: '
a
'}); run(rootComp, []) .then((data) => { expect(data).toEqual([ [BEGIN_ELEMENT, 'div', [], [], [], [], false, null], [TEXT, 'a', false, null], [END_ELEMENT] ]); async.done(); }); })); }); describe('components', () => { it('should create component commands', inject([AsyncTestCompleter], (async) => { var rootComp = createComp( {type: RootCompTypeMeta, template: ''}); var comp = createComp({type: ACompTypeMeta, selector: 'a'}); run(rootComp, [comp]) .then((data) => { expect(data).toEqual([ [ BEGIN_COMPONENT, 'a', ['a', 'b'], [null, 'click'], ['someVar', 0], ['ACompType'], serializeEnum(ViewEncapsulation.None), null, 'aCompId' ], [END_COMPONENT] ]); async.done(); }); })); it('should store viewEncapsulation', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: ''}); var comp = createComp( {type: ACompTypeMeta, selector: 'a', encapsulation: ViewEncapsulation.Native}); run(rootComp, [comp]) .then((data) => { expect(data).toEqual([ [ BEGIN_COMPONENT, 'a', [], [], [], ['ACompType'], serializeEnum(ViewEncapsulation.Native), null, 'aCompId' ], [END_COMPONENT] ]); async.done(); }); })); it('should create nested nodes and set ngContentIndex', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: 't'}); var comp = createComp({type: ACompTypeMeta, selector: 'a', ngContentSelectors: ['*']}); run(rootComp, [comp]) .then((data) => { expect(data).toEqual([ [ BEGIN_COMPONENT, 'a', [], [], [], ['ACompType'], serializeEnum(ViewEncapsulation.None), null, 'aCompId' ], [TEXT, 't', false, 0], [END_COMPONENT] ]); async.done(); }); })); }); describe('embedded templates', () => { it('should create embedded template commands', inject([AsyncTestCompleter], (async) => { var rootComp = createComp({type: RootCompTypeMeta, template: ''}); var dir = createDirective(SomeDirTypeMeta, '[a]'); run(rootComp, [dir], 1) .then((data) => { expect(data).toEqual([ [EMBEDDED_TEMPLATE, ['a', 'b'], [], ['SomeDirType'], false, null, 'cd1', []] ]); async.done(); }); })); it('should keep variable name and value for