2017-01-26 20:07:37 -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 {Injector, RenderComponentType, RootRenderer, Sanitizer, SecurityContext, ViewEncapsulation, getDebugNode} from '@angular/core';
|
2017-02-03 18:20:50 -05:00
|
|
|
import {DebugContext, NodeDef, NodeFlags, QueryValueType, RootData, Services, ViewData, ViewDefinition, ViewFlags, ViewHandleEventFn, ViewUpdateFn, anchorDef, asElementData, asProviderData, asTextData, directiveDef, elementDef, rootRenderNodes, textDef, viewDef} from '@angular/core/src/view/index';
|
2017-01-26 20:07:37 -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, isBrowser} from './helper';
|
2017-01-26 20:07:37 -05:00
|
|
|
|
|
|
|
export function main() {
|
2017-02-03 18:20:50 -05:00
|
|
|
describe('View Services', () => {
|
2017-01-26 20:07:37 -05:00
|
|
|
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-26 20:07:37 -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-26 20:07:37 -05:00
|
|
|
const rootNodes = rootRenderNodes(view);
|
|
|
|
return {rootNodes, view};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('DebugContext', () => {
|
|
|
|
class AComp {}
|
|
|
|
|
|
|
|
class AService {}
|
|
|
|
|
|
|
|
function createViewWithData() {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-02-21 16:56:56 -05:00
|
|
|
elementDef(
|
|
|
|
NodeFlags.None, null, null, 1, 'div', null, null, null, null,
|
2017-01-26 20:07:37 -05:00
|
|
|
() => compViewDef([
|
2017-02-15 11:36:49 -05:00
|
|
|
elementDef(NodeFlags.None, [['ref', QueryValueType.ElementRef]], null, 2, 'span'),
|
2017-02-01 10:27:38 -05:00
|
|
|
directiveDef(NodeFlags.None, null, 0, AService, []), textDef(null, ['a'])
|
2017-01-26 20:07:37 -05:00
|
|
|
])),
|
2017-02-27 12:14:18 -05:00
|
|
|
directiveDef(NodeFlags.Component, null, 0, AComp, []),
|
2017-01-26 20:07:37 -05:00
|
|
|
]));
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should provide data for elements', () => {
|
|
|
|
const view = createViewWithData();
|
2017-02-21 16:56:56 -05:00
|
|
|
const compView = asElementData(view, 0).componentView;
|
2017-01-26 20:07:37 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
const debugCtx = Services.createDebugContext(compView, 0);
|
2017-01-26 20:07:37 -05:00
|
|
|
|
|
|
|
expect(debugCtx.componentRenderElement).toBe(asElementData(view, 0).renderElement);
|
|
|
|
expect(debugCtx.renderNode).toBe(asElementData(compView, 0).renderElement);
|
|
|
|
expect(debugCtx.injector.get(AComp)).toBe(compView.component);
|
|
|
|
expect(debugCtx.component).toBe(compView.component);
|
|
|
|
expect(debugCtx.context).toBe(compView.context);
|
|
|
|
expect(debugCtx.providerTokens).toEqual([AService]);
|
|
|
|
expect(debugCtx.references['ref'].nativeElement)
|
|
|
|
.toBe(asElementData(compView, 0).renderElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should provide data for text nodes', () => {
|
|
|
|
const view = createViewWithData();
|
2017-02-21 16:56:56 -05:00
|
|
|
const compView = asElementData(view, 0).componentView;
|
2017-01-26 20:07:37 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
const debugCtx = Services.createDebugContext(compView, 2);
|
2017-01-26 20:07:37 -05:00
|
|
|
|
|
|
|
expect(debugCtx.componentRenderElement).toBe(asElementData(view, 0).renderElement);
|
|
|
|
expect(debugCtx.renderNode).toBe(asTextData(compView, 2).renderText);
|
|
|
|
expect(debugCtx.injector.get(AComp)).toBe(compView.component);
|
|
|
|
expect(debugCtx.component).toBe(compView.component);
|
|
|
|
expect(debugCtx.context).toBe(compView.context);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should provide data for other nodes based on the nearest element parent', () => {
|
|
|
|
const view = createViewWithData();
|
2017-02-21 16:56:56 -05:00
|
|
|
const compView = asElementData(view, 0).componentView;
|
2017-01-26 20:07:37 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
const debugCtx = Services.createDebugContext(compView, 1);
|
2017-01-26 20:07:37 -05:00
|
|
|
|
|
|
|
expect(debugCtx.renderNode).toBe(asElementData(compView, 0).renderElement);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|