2017-01-23 19:59:20 -05: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
|
|
|
|
*/
|
|
|
|
|
2017-02-01 14:32:27 -05:00
|
|
|
import {ElementRef, Injector, QueryList, RenderComponentType, RootRenderer, Sanitizer, SecurityContext, TemplateRef, ViewContainerRef, ViewEncapsulation, getDebugNode} from '@angular/core';
|
2017-01-27 16:19:00 -05:00
|
|
|
import {getDebugContext} from '@angular/core/src/errors';
|
2017-03-17 12:23:28 -04:00
|
|
|
import {BindingFlags, DebugContext, NodeDef, NodeFlags, QueryBindingType, QueryValueType, RootData, Services, ViewData, ViewDefinition, ViewDefinitionFactory, ViewFlags, ViewHandleEventFn, ViewUpdateFn, anchorDef, asElementData, asProviderData, attachEmbeddedView, detachEmbeddedView, directiveDef, elementDef, queryDef, rootRenderNodes, textDef, viewDef} from '@angular/core/src/view/index';
|
2017-01-23 19:59:20 -05:00
|
|
|
import {inject} from '@angular/core/testing';
|
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
import {createRootView} from './helper';
|
2017-02-01 14:32:27 -05:00
|
|
|
|
2017-01-23 19:59:20 -05:00
|
|
|
export function main() {
|
|
|
|
describe(`Query Views`, () => {
|
|
|
|
function compViewDef(
|
2017-02-09 17:59:57 -05:00
|
|
|
nodes: NodeDef[], updateDirectives?: ViewUpdateFn, updateRenderer?: ViewUpdateFn,
|
2017-02-20 15:15:03 -05:00
|
|
|
viewFlags: ViewFlags = ViewFlags.None): ViewDefinition {
|
|
|
|
return viewDef(viewFlags, nodes, updateDirectives, updateRenderer);
|
2017-01-23 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
2017-02-02 18:01:35 -05:00
|
|
|
function embeddedViewDef(nodes: NodeDef[], update?: ViewUpdateFn): ViewDefinitionFactory {
|
|
|
|
return () => viewDef(ViewFlags.None, nodes, update);
|
2017-01-23 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function createAndGetRootNodes(
|
|
|
|
viewDef: ViewDefinition, context: any = null): {rootNodes: any[], view: ViewData} {
|
2017-02-03 18:20:50 -05:00
|
|
|
const view = createRootView(viewDef, context);
|
2017-01-23 19:59:20 -05:00
|
|
|
const rootNodes = rootRenderNodes(view);
|
|
|
|
return {rootNodes, view};
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:36:49 -05:00
|
|
|
const someQueryId = 1;
|
|
|
|
|
2017-01-23 19:59:20 -05:00
|
|
|
class AService {}
|
|
|
|
|
|
|
|
class QueryService {
|
|
|
|
a: QueryList<AService>;
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
function contentQueryProviders() {
|
|
|
|
return [
|
2017-03-29 12:34:45 -04:00
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.All})
|
2017-01-25 16:45:07 -05:00
|
|
|
];
|
2017-01-23 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 16:56:56 -05:00
|
|
|
function compViewQueryProviders(extraChildCount: number, nodes: NodeDef[]) {
|
2017-01-25 16:45:07 -05:00
|
|
|
return [
|
2017-02-21 16:56:56 -05:00
|
|
|
elementDef(
|
2017-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, null !, null !, 1 + extraChildCount, 'div', null !, null !, null !,
|
|
|
|
null !, () => compViewDef([
|
|
|
|
queryDef(
|
|
|
|
NodeFlags.TypeViewQuery | NodeFlags.DynamicQuery, someQueryId,
|
|
|
|
{'a': QueryBindingType.All}),
|
|
|
|
...nodes
|
|
|
|
])),
|
|
|
|
directiveDef(NodeFlags.Component, null !, 0, QueryService, [], null !, null !, ),
|
2017-01-25 16:45:07 -05:00
|
|
|
];
|
2017-01-23 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
function aServiceProvider() {
|
2017-02-15 11:36:49 -05:00
|
|
|
return directiveDef(
|
|
|
|
NodeFlags.None, [[someQueryId, QueryValueType.Provider]], 0, AService, []);
|
2017-01-23 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('content queries', () => {
|
|
|
|
|
|
|
|
it('should query providers on the same element and child elements', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 5, 'div'),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
]));
|
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs.a).toBeUndefined();
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
const as = qs.a.toArray();
|
|
|
|
expect(as.length).toBe(2);
|
2017-01-25 16:45:07 -05:00
|
|
|
expect(as[0]).toBe(asProviderData(view, 3).instance);
|
|
|
|
expect(as[1]).toBe(asProviderData(view, 5).instance);
|
2017-01-23 19:59:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not query providers on sibling or parent elements', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 6, 'div'),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 2, 'div'),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 3).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs.a.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('view queries', () => {
|
|
|
|
it('should query providers in the view', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-02-21 16:56:56 -05:00
|
|
|
...compViewQueryProviders(
|
|
|
|
0,
|
|
|
|
[
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'span'),
|
2017-02-21 16:56:56 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
]),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const comp: QueryService = asProviderData(view, 1).instance;
|
2017-02-21 16:56:56 -05:00
|
|
|
const compView = asElementData(view, 0).componentView;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(comp.a.length).toBe(1);
|
2017-02-17 11:56:36 -05:00
|
|
|
expect(comp.a.first).toBe(asProviderData(compView, 2).instance);
|
2017-01-23 19:59:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not query providers on the host element', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-02-21 16:56:56 -05:00
|
|
|
...compViewQueryProviders(
|
|
|
|
1,
|
|
|
|
[
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 0, 'span'),
|
2017-02-21 16:56:56 -05:00
|
|
|
]),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-25 16:45:07 -05:00
|
|
|
const comp: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(comp.a.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('embedded views', () => {
|
|
|
|
it('should query providers in embedded views', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 5, 'div'),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-03-29 12:34:45 -04:00
|
|
|
anchorDef(NodeFlags.EmbeddedViews, null !, null !, 2, null !, embeddedViewDef([
|
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
2017-02-02 18:01:35 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
])),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
const childView = Services.createEmbeddedView(view, view.def.nodes[3]);
|
2017-02-22 13:05:56 -05:00
|
|
|
attachEmbeddedView(view, asElementData(view, 3), 0, childView);
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
// queries on parent elements of anchors
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs1: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs1.a.length).toBe(1);
|
|
|
|
expect(qs1.a.first instanceof AService).toBe(true);
|
|
|
|
|
|
|
|
// queries on the anchor
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs2: QueryService = asProviderData(view, 4).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs2.a.length).toBe(1);
|
|
|
|
expect(qs2.a.first instanceof AService).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should query providers in embedded views only at the template declaration', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 3, 'div'),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-03-29 12:34:45 -04:00
|
|
|
anchorDef(NodeFlags.EmbeddedViews, null !, null !, 0, null !, embeddedViewDef([
|
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
2017-02-02 18:01:35 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
])),
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 3, 'div'),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-03-29 12:34:45 -04:00
|
|
|
anchorDef(NodeFlags.EmbeddedViews, null !, null !, 0),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
const childView = Services.createEmbeddedView(view, view.def.nodes[3]);
|
2017-01-23 19:59:20 -05:00
|
|
|
// attach at a different place than the one where the template was defined
|
2017-02-22 13:05:56 -05:00
|
|
|
attachEmbeddedView(view, asElementData(view, 7), 0, childView);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
// query on the declaration place
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs1: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs1.a.length).toBe(1);
|
|
|
|
expect(qs1.a.first instanceof AService).toBe(true);
|
|
|
|
|
|
|
|
// query on the attach place
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs2: QueryService = asProviderData(view, 5).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs2.a.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update content queries if embedded views are added or removed', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 3, 'div'),
|
2017-01-25 16:45:07 -05:00
|
|
|
...contentQueryProviders(),
|
2017-03-29 12:34:45 -04:00
|
|
|
anchorDef(NodeFlags.EmbeddedViews, null !, null !, 0, null !, embeddedViewDef([
|
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
2017-02-02 18:01:35 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
])),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs.a.length).toBe(0);
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
const childView = Services.createEmbeddedView(view, view.def.nodes[3]);
|
2017-02-22 13:05:56 -05:00
|
|
|
attachEmbeddedView(view, asElementData(view, 3), 0, childView);
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
expect(qs.a.length).toBe(1);
|
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
detachEmbeddedView(asElementData(view, 3), 0);
|
2017-02-22 13:05:56 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
expect(qs.a.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update view queries if embedded views are added or removed', () => {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-02-21 16:56:56 -05:00
|
|
|
...compViewQueryProviders(
|
|
|
|
0,
|
|
|
|
[
|
2017-03-29 12:34:45 -04:00
|
|
|
anchorDef(NodeFlags.EmbeddedViews, null !, null !, 0, null !, embeddedViewDef([
|
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
2017-02-21 16:56:56 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
])),
|
|
|
|
]),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const comp: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(comp.a.length).toBe(0);
|
|
|
|
|
2017-02-21 16:56:56 -05:00
|
|
|
const compView = asElementData(view, 0).componentView;
|
2017-02-17 11:56:36 -05:00
|
|
|
const childView = Services.createEmbeddedView(compView, compView.def.nodes[1]);
|
2017-02-22 13:05:56 -05:00
|
|
|
attachEmbeddedView(view, asElementData(compView, 1), 0, childView);
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
expect(comp.a.length).toBe(1);
|
|
|
|
|
2017-02-17 11:56:36 -05:00
|
|
|
detachEmbeddedView(asElementData(compView, 1), 0);
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
|
|
|
expect(comp.a.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('QueryBindingType', () => {
|
|
|
|
it('should query all matches', () => {
|
|
|
|
class QueryService {
|
|
|
|
a: QueryList<AService>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 4, 'div'),
|
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.All}),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
aServiceProvider(),
|
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs.a instanceof QueryList).toBeTruthy();
|
|
|
|
expect(qs.a.toArray()).toEqual([
|
2017-01-25 16:45:07 -05:00
|
|
|
asProviderData(view, 3).instance,
|
|
|
|
asProviderData(view, 4).instance,
|
2017-01-23 19:59:20 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should query the first match', () => {
|
|
|
|
class QueryService {
|
|
|
|
a: AService;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 4, 'div'),
|
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.First}),
|
2017-01-23 19:59:20 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
aServiceProvider(),
|
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
|
|
|
expect(qs.a).toBe(asProviderData(view, 3).instance);
|
2017-01-23 19:59:20 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('query builtins', () => {
|
|
|
|
it('should query ElementRef', () => {
|
|
|
|
class QueryService {
|
|
|
|
a: ElementRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, [[someQueryId, QueryValueType.ElementRef]], null !, 2, 'div'),
|
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.First}),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
|
|
|
expect(qs.a.nativeElement).toBe(asElementData(view, 0).renderElement);
|
2017-01-23 19:59:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should query TemplateRef', () => {
|
|
|
|
class QueryService {
|
|
|
|
a: TemplateRef<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
|
|
|
anchorDef(
|
2017-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, [[someQueryId, QueryValueType.TemplateRef]], null !, 2, null !,
|
|
|
|
embeddedViewDef([anchorDef(NodeFlags.None, null !, null !, 0)])),
|
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.First}),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs.a.createEmbeddedView).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should query ViewContainerRef', () => {
|
|
|
|
class QueryService {
|
|
|
|
a: ViewContainerRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-13 13:44:12 -04:00
|
|
|
anchorDef(
|
2017-03-29 12:34:45 -04:00
|
|
|
NodeFlags.EmbeddedViews, [[someQueryId, QueryValueType.ViewContainerRef]], null !, 2),
|
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.First}),
|
2017-01-23 19:59:20 -05:00
|
|
|
]));
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-23 19:59:20 -05:00
|
|
|
|
2017-01-25 16:45:07 -05:00
|
|
|
const qs: QueryService = asProviderData(view, 1).instance;
|
2017-01-23 19:59:20 -05:00
|
|
|
expect(qs.a.createEmbeddedView).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2017-01-26 20:07:37 -05:00
|
|
|
|
|
|
|
describe('general binding behavior', () => {
|
|
|
|
it('should report debug info on binding errors', () => {
|
|
|
|
class QueryService {
|
|
|
|
set a(value: any) { throw new Error('Test'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 3, 'div'),
|
|
|
|
directiveDef(NodeFlags.None, null !, 1, QueryService, []),
|
2017-02-15 11:36:49 -05:00
|
|
|
queryDef(
|
2017-02-27 12:14:18 -05:00
|
|
|
NodeFlags.TypeContentQuery | NodeFlags.DynamicQuery, someQueryId,
|
2017-02-15 11:36:49 -05:00
|
|
|
{'a': QueryBindingType.All}),
|
2017-01-26 20:07:37 -05:00
|
|
|
aServiceProvider(),
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
|
|
let err: any;
|
|
|
|
try {
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-26 20:07:37 -05:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
expect(err.message).toBe('Test');
|
2017-01-27 16:19:00 -05:00
|
|
|
const debugCtx = getDebugContext(err);
|
2017-01-26 20:07:37 -05:00
|
|
|
expect(debugCtx.view).toBe(view);
|
|
|
|
expect(debugCtx.nodeIndex).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
2017-01-23 19:59:20 -05:00
|
|
|
});
|
2017-01-27 16:19:00 -05:00
|
|
|
}
|