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
|
|
|
|
*/
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
import {asElementData, asTextData, DebugContext, directiveDef, elementDef, NodeFlags, QueryValueType, Services, textDef} from '@angular/core/src/view/index';
|
2017-01-26 20:07:37 -05:00
|
|
|
|
2017-09-22 17:29:16 -04:00
|
|
|
import {compViewDef, createAndGetRootNodes} from './helper';
|
2017-01-26 20:07:37 -05:00
|
|
|
|
2017-12-15 19:28:41 -05:00
|
|
|
{
|
2017-02-03 18:20:50 -05:00
|
|
|
describe('View Services', () => {
|
2017-01-26 20:07:37 -05:00
|
|
|
describe('DebugContext', () => {
|
|
|
|
class AComp {}
|
|
|
|
|
|
|
|
class AService {}
|
|
|
|
|
|
|
|
function createViewWithData() {
|
|
|
|
const {view} = createAndGetRootNodes(compViewDef([
|
2017-02-21 16:56:56 -05:00
|
|
|
elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 1, 'div', null, null, null, null,
|
2017-01-26 20:07:37 -05:00
|
|
|
() => compViewDef([
|
2017-09-22 17:29:16 -04:00
|
|
|
elementDef(
|
|
|
|
0, NodeFlags.None, [['ref', QueryValueType.ElementRef]], null, 2, 'span'),
|
|
|
|
directiveDef(1, NodeFlags.None, null, 0, AService, []), textDef(2, null, ['a'])
|
2017-01-26 20:07:37 -05:00
|
|
|
])),
|
2017-09-22 17:29:16 -04:00
|
|
|
directiveDef(1, 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|