2015-05-26 12:25:39 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
IS_DARTIUM,
|
|
|
|
it,
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
import {List, ListWrapper, Map, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-06-10 08:40:24 -04:00
|
|
|
import {Type, isBlank, stringify, isPresent, BaseException} from 'angular2/src/facade/lang';
|
2015-05-26 12:25:39 -04:00
|
|
|
import {PromiseWrapper, Promise} from 'angular2/src/facade/async';
|
|
|
|
|
|
|
|
import {DomCompiler} from 'angular2/src/render/dom/compiler/compiler';
|
2015-06-09 09:20:33 -04:00
|
|
|
import {ProtoViewDto, ViewDefinition, DirectiveMetadata, ViewType} from 'angular2/src/render/api';
|
2015-05-26 12:25:39 -04:00
|
|
|
import {CompileElement} from 'angular2/src/render/dom/compiler/compile_element';
|
|
|
|
import {CompileStep} from 'angular2/src/render/dom/compiler/compile_step';
|
|
|
|
import {CompileStepFactory} from 'angular2/src/render/dom/compiler/compile_step_factory';
|
|
|
|
import {CompileControl} from 'angular2/src/render/dom/compiler/compile_control';
|
2015-06-24 04:43:36 -04:00
|
|
|
import {ViewLoader} from 'angular2/src/render/dom/compiler/view_loader';
|
2015-05-26 12:25:39 -04:00
|
|
|
|
|
|
|
import {resolveInternalDomProtoView} from 'angular2/src/render/dom/view/proto_view';
|
|
|
|
|
|
|
|
export function runCompilerCommonTests() {
|
|
|
|
describe('DomCompiler', function() {
|
2015-06-17 14:17:21 -04:00
|
|
|
var mockStepFactory: MockStepFactory;
|
2015-05-26 12:25:39 -04:00
|
|
|
|
|
|
|
function createCompiler(processClosure, urlData = null) {
|
|
|
|
if (isBlank(urlData)) {
|
2015-06-17 19:21:40 -04:00
|
|
|
urlData = new Map();
|
2015-05-26 12:25:39 -04:00
|
|
|
}
|
2015-06-24 04:43:36 -04:00
|
|
|
var tplLoader = new FakeViewLoader(urlData);
|
2015-05-26 12:25:39 -04:00
|
|
|
mockStepFactory = new MockStepFactory([new MockStep(processClosure)]);
|
|
|
|
return new DomCompiler(mockStepFactory, tplLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('compile', () => {
|
|
|
|
|
|
|
|
it('should run the steps and build the AppProtoView of the root element',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var compiler = createCompiler((parent, current, control) => {
|
|
|
|
current.inheritedProtoView.bindVariable('b', 'a');
|
|
|
|
});
|
|
|
|
compiler.compile(
|
|
|
|
new ViewDefinition({componentId: 'someComponent', template: '<div></div>'}))
|
|
|
|
.then((protoView) => {
|
|
|
|
expect(protoView.variableBindings)
|
|
|
|
.toEqual(MapWrapper.createFromStringMap({'a': 'b'}));
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should run the steps and build the proto view', inject([AsyncTestCompleter], (async) => {
|
|
|
|
var compiler = createCompiler((parent, current, control) => {
|
|
|
|
current.inheritedProtoView.bindVariable('b', 'a');
|
|
|
|
});
|
|
|
|
|
2015-06-09 06:33:40 -04:00
|
|
|
var dirMetadata = DirectiveMetadata.create(
|
2015-05-26 12:25:39 -04:00
|
|
|
{id: 'id', selector: 'CUSTOM', type: DirectiveMetadata.COMPONENT_TYPE});
|
|
|
|
compiler.compileHost(dirMetadata)
|
|
|
|
.then((protoView) => {
|
|
|
|
expect(DOM.tagName(resolveInternalDomProtoView(protoView.render).element))
|
|
|
|
.toEqual('CUSTOM');
|
|
|
|
expect(mockStepFactory.viewDef.directives).toEqual([dirMetadata]);
|
|
|
|
expect(protoView.variableBindings)
|
|
|
|
.toEqual(MapWrapper.createFromStringMap({'a': 'b'}));
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use the inline template and compile in sync',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var compiler = createCompiler(EMPTY_STEP);
|
|
|
|
compiler.compile(
|
|
|
|
new ViewDefinition({componentId: 'someId', template: 'inline component'}))
|
|
|
|
.then((protoView) => {
|
|
|
|
expect(DOM.getInnerHTML(resolveInternalDomProtoView(protoView.render).element))
|
|
|
|
.toEqual('inline component');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should load url templates', inject([AsyncTestCompleter], (async) => {
|
|
|
|
var urlData = MapWrapper.createFromStringMap({'someUrl': 'url component'});
|
|
|
|
var compiler = createCompiler(EMPTY_STEP, urlData);
|
2015-06-10 08:40:24 -04:00
|
|
|
compiler.compile(new ViewDefinition({componentId: 'someId', templateAbsUrl: 'someUrl'}))
|
2015-05-26 12:25:39 -04:00
|
|
|
.then((protoView) => {
|
|
|
|
expect(DOM.getInnerHTML(resolveInternalDomProtoView(protoView.render).element))
|
|
|
|
.toEqual('url component');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should report loading errors', inject([AsyncTestCompleter], (async) => {
|
2015-06-17 19:21:40 -04:00
|
|
|
var compiler = createCompiler(EMPTY_STEP, new Map());
|
2015-05-26 12:25:39 -04:00
|
|
|
PromiseWrapper.catchError(
|
2015-06-10 08:40:24 -04:00
|
|
|
compiler.compile(
|
|
|
|
new ViewDefinition({componentId: 'someId', templateAbsUrl: 'someUrl'})),
|
2015-05-26 12:25:39 -04:00
|
|
|
(e) => {
|
2015-06-01 05:07:14 -04:00
|
|
|
expect(e.message).toEqual(
|
|
|
|
'Failed to load the template for "someId" : Failed to fetch url "someUrl"');
|
2015-05-26 12:25:39 -04:00
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should return ProtoViews of type COMPONENT_VIEW_TYPE',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var compiler = createCompiler(EMPTY_STEP);
|
|
|
|
compiler.compile(
|
|
|
|
new ViewDefinition({componentId: 'someId', template: 'inline component'}))
|
|
|
|
.then((protoView) => {
|
2015-06-09 09:20:33 -04:00
|
|
|
expect(protoView.type).toEqual(ViewType.COMPONENT);
|
2015-05-26 12:25:39 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('compileHost', () => {
|
|
|
|
|
|
|
|
it('should return ProtoViews of type HOST_VIEW_TYPE',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var compiler = createCompiler(EMPTY_STEP);
|
|
|
|
compiler.compileHost(someComponent)
|
|
|
|
.then((protoView) => {
|
2015-06-09 09:20:33 -04:00
|
|
|
expect(protoView.type).toEqual(ViewType.HOST);
|
2015-05-26 12:25:39 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockStepFactory extends CompileStepFactory {
|
|
|
|
steps: List<CompileStep>;
|
|
|
|
subTaskPromises: List<Promise<any>>;
|
|
|
|
viewDef: ViewDefinition;
|
|
|
|
|
|
|
|
constructor(steps) {
|
|
|
|
super();
|
|
|
|
this.steps = steps;
|
|
|
|
}
|
2015-06-15 09:57:42 -04:00
|
|
|
createSteps(viewDef): List<CompileStep> {
|
2015-05-26 12:25:39 -04:00
|
|
|
this.viewDef = viewDef;
|
|
|
|
return this.steps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockStep implements CompileStep {
|
|
|
|
processClosure: Function;
|
|
|
|
constructor(process) { this.processClosure = process; }
|
|
|
|
process(parent: CompileElement, current: CompileElement, control: CompileControl) {
|
|
|
|
this.processClosure(parent, current, control);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var EMPTY_STEP = (parent, current, control) => {
|
|
|
|
if (isPresent(parent)) {
|
|
|
|
current.inheritedProtoView = parent.inheritedProtoView;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-24 04:43:36 -04:00
|
|
|
class FakeViewLoader extends ViewLoader {
|
2015-05-26 12:25:39 -04:00
|
|
|
_urlData: Map<string, string>;
|
|
|
|
constructor(urlData) {
|
2015-06-15 09:57:42 -04:00
|
|
|
super(null, null, null);
|
2015-05-26 12:25:39 -04:00
|
|
|
this._urlData = urlData;
|
|
|
|
}
|
|
|
|
|
2015-06-15 09:57:42 -04:00
|
|
|
load(view: ViewDefinition): Promise<any> {
|
|
|
|
if (isPresent(view.template)) {
|
|
|
|
return PromiseWrapper.resolve(DOM.createTemplate(view.template));
|
2015-05-26 12:25:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 09:57:42 -04:00
|
|
|
if (isPresent(view.templateAbsUrl)) {
|
|
|
|
var content = this._urlData.get(view.templateAbsUrl);
|
2015-06-01 05:07:14 -04:00
|
|
|
return isPresent(content) ?
|
|
|
|
PromiseWrapper.resolve(DOM.createTemplate(content)) :
|
2015-06-15 09:57:42 -04:00
|
|
|
PromiseWrapper.reject(`Failed to fetch url "${view.templateAbsUrl}"`, null);
|
2015-05-26 12:25:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
throw new BaseException('View should have either the templateUrl or template property set');
|
2015-05-26 12:25:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 06:33:40 -04:00
|
|
|
var someComponent = DirectiveMetadata.create(
|
2015-05-26 12:25:39 -04:00
|
|
|
{selector: 'some-comp', id: 'someComponent', type: DirectiveMetadata.COMPONENT_TYPE});
|