2017-12-01 17:23:03 -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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {ElementRef, TemplateRef, ViewContainerRef} from '@angular/core';
|
|
|
|
|
2017-12-15 08:59:17 -05:00
|
|
|
import {bloomAdd, bloomFindPossibleInjector} from '../../src/render3/di';
|
2017-12-14 18:03:46 -05:00
|
|
|
import {C, D, E, PublicFeature, T, V, b, b2, c, cR, cr, defineDirective, e, inject, injectElementRef, injectTemplateRef, injectViewContainerRef, t, v} from '../../src/render3/index';
|
2018-01-08 23:17:13 -05:00
|
|
|
import {createLNode, createLView, enterView, getOrCreateNodeInjector, leaveView} from '../../src/render3/instructions';
|
2018-01-09 21:38:17 -05:00
|
|
|
import {LInjector} from '../../src/render3/interfaces/injector';
|
|
|
|
import {LNodeFlags} from '../../src/render3/interfaces/node';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
import {renderToHtml} from './render_util';
|
|
|
|
|
|
|
|
describe('di', () => {
|
|
|
|
describe('no dependencies', () => {
|
|
|
|
it('should create directive with no deps', () => {
|
|
|
|
class Directive {
|
|
|
|
value: string = 'Created';
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({type: Directive, factory: () => new Directive});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Template(ctx: any, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-01-09 00:57:50 -05:00
|
|
|
E(0, 'div', null, [Directive]);
|
|
|
|
{ T(2); }
|
2017-12-01 17:23:03 -05:00
|
|
|
e();
|
|
|
|
}
|
2017-12-11 17:08:52 -05:00
|
|
|
t(2, b(D<Directive>(1).value));
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(renderToHtml(Template, {})).toEqual('<div>Created</div>');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('view dependencies', () => {
|
|
|
|
it('should create directive with inter view dependencies', () => {
|
|
|
|
class DirectiveA {
|
|
|
|
value: string = 'A';
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective(
|
|
|
|
{type: DirectiveA, factory: () => new DirectiveA, features: [PublicFeature]});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveB {
|
|
|
|
value: string = 'B';
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective(
|
|
|
|
{type: DirectiveB, factory: () => new DirectiveB, features: [PublicFeature]});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveC {
|
|
|
|
value: string;
|
|
|
|
constructor(a: DirectiveA, b: DirectiveB) { this.value = a.value + b.value; }
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: DirectiveC,
|
|
|
|
factory: () => new DirectiveC(inject(DirectiveA), inject(DirectiveB))
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Template(ctx: any, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-01-09 00:57:50 -05:00
|
|
|
E(0, 'div', null, [DirectiveA]);
|
2017-12-01 17:23:03 -05:00
|
|
|
{
|
2018-01-09 00:57:50 -05:00
|
|
|
E(2, 'span', null, [DirectiveB, DirectiveC]);
|
|
|
|
{ T(5); }
|
2017-12-01 17:23:03 -05:00
|
|
|
e();
|
|
|
|
}
|
|
|
|
e();
|
|
|
|
}
|
2017-12-11 17:08:52 -05:00
|
|
|
t(5, b(D<DirectiveC>(4).value));
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(renderToHtml(Template, {})).toEqual('<div><span>AB</span></div>');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ElementRef', () => {
|
|
|
|
it('should create directive with ElementRef dependencies', () => {
|
|
|
|
class Directive {
|
|
|
|
value: string;
|
|
|
|
constructor(public elementRef: ElementRef) {
|
|
|
|
this.value = (elementRef.constructor as any).name;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: Directive,
|
|
|
|
factory: () => new Directive(injectElementRef()),
|
|
|
|
features: [PublicFeature]
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveSameInstance {
|
|
|
|
value: boolean;
|
|
|
|
constructor(elementRef: ElementRef, directive: Directive) {
|
|
|
|
this.value = elementRef === directive.elementRef;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: DirectiveSameInstance,
|
|
|
|
factory: () => new DirectiveSameInstance(injectElementRef(), inject(Directive))
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Template(ctx: any, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-01-09 00:57:50 -05:00
|
|
|
E(0, 'div', null, [Directive, DirectiveSameInstance]);
|
|
|
|
{ T(3); }
|
2017-12-01 17:23:03 -05:00
|
|
|
e();
|
|
|
|
}
|
2017-12-11 17:08:52 -05:00
|
|
|
t(3, b2('', D<Directive>(1).value, '-', D<DirectiveSameInstance>(2).value, ''));
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(renderToHtml(Template, {})).toEqual('<div>ElementRef-true</div>');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('TemplateRef', () => {
|
|
|
|
it('should create directive with TemplateRef dependencies', () => {
|
|
|
|
class Directive {
|
|
|
|
value: string;
|
|
|
|
constructor(public templateRef: TemplateRef<any>) {
|
|
|
|
this.value = (templateRef.constructor as any).name;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: Directive,
|
|
|
|
factory: () => new Directive(injectTemplateRef()),
|
|
|
|
features: [PublicFeature]
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveSameInstance {
|
|
|
|
value: boolean;
|
|
|
|
constructor(templateRef: TemplateRef<any>, directive: Directive) {
|
|
|
|
this.value = templateRef === directive.templateRef;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: DirectiveSameInstance,
|
|
|
|
factory: () => new DirectiveSameInstance(injectTemplateRef(), inject(Directive))
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Template(ctx: any, cm: any) {
|
|
|
|
if (cm) {
|
2018-01-09 00:57:50 -05:00
|
|
|
C(0, [Directive, DirectiveSameInstance], function() {});
|
2017-12-01 17:23:03 -05:00
|
|
|
c();
|
2017-12-11 17:08:52 -05:00
|
|
|
T(3);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2017-12-11 17:08:52 -05:00
|
|
|
t(3, b2('', D<Directive>(1).value, '-', D<DirectiveSameInstance>(2).value, ''));
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(renderToHtml(Template, {})).toEqual('TemplateRef-true');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ViewContainerRef', () => {
|
|
|
|
it('should create directive with ViewContainerRef dependencies', () => {
|
|
|
|
class Directive {
|
|
|
|
value: string;
|
|
|
|
constructor(public viewContainerRef: ViewContainerRef) {
|
|
|
|
this.value = (viewContainerRef.constructor as any).name;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: Directive,
|
|
|
|
factory: () => new Directive(injectViewContainerRef()),
|
|
|
|
features: [PublicFeature]
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveSameInstance {
|
|
|
|
value: boolean;
|
|
|
|
constructor(viewContainerRef: ViewContainerRef, directive: Directive) {
|
|
|
|
this.value = viewContainerRef === directive.viewContainerRef;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: DirectiveSameInstance,
|
|
|
|
factory: () => new DirectiveSameInstance(injectViewContainerRef(), inject(Directive))
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Template(ctx: any, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-01-09 00:57:50 -05:00
|
|
|
E(0, 'div', null, [Directive, DirectiveSameInstance]);
|
|
|
|
{ T(3); }
|
2017-12-01 17:23:03 -05:00
|
|
|
e();
|
|
|
|
}
|
2017-12-11 17:08:52 -05:00
|
|
|
t(3, b2('', D<Directive>(1).value, '-', D<DirectiveSameInstance>(2).value, ''));
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(renderToHtml(Template, {})).toEqual('<div>ViewContainerRef-true</div>');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('inject', () => {
|
|
|
|
describe('bloom filter', () => {
|
2018-01-08 23:17:13 -05:00
|
|
|
let di: LInjector;
|
2017-12-01 17:23:03 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
di = {} as any;
|
|
|
|
di.bf0 = 0;
|
|
|
|
di.bf1 = 0;
|
|
|
|
di.bf2 = 0;
|
|
|
|
di.bf3 = 0;
|
|
|
|
di.cbf0 = 0;
|
|
|
|
di.cbf1 = 0;
|
|
|
|
di.cbf2 = 0;
|
|
|
|
di.cbf3 = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
function bloomState() { return [di.bf3, di.bf2, di.bf1, di.bf0]; }
|
|
|
|
|
|
|
|
it('should add values', () => {
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 0 } as any);
|
|
|
|
expect(bloomState()).toEqual([0, 0, 0, 1]);
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 32 + 1 } as any);
|
|
|
|
expect(bloomState()).toEqual([0, 0, 2, 1]);
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 64 + 2 } as any);
|
|
|
|
expect(bloomState()).toEqual([0, 4, 2, 1]);
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 96 + 3 } as any);
|
|
|
|
expect(bloomState()).toEqual([8, 4, 2, 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should query values', () => {
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 0 } as any);
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 32 } as any);
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 64 } as any);
|
|
|
|
bloomAdd(di, { __NG_ELEMENT_ID__: 96 } as any);
|
|
|
|
|
|
|
|
expect(bloomFindPossibleInjector(di, 0)).toEqual(di);
|
|
|
|
expect(bloomFindPossibleInjector(di, 1)).toEqual(null);
|
|
|
|
expect(bloomFindPossibleInjector(di, 32)).toEqual(di);
|
|
|
|
expect(bloomFindPossibleInjector(di, 64)).toEqual(di);
|
|
|
|
expect(bloomFindPossibleInjector(di, 96)).toEqual(di);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should inject from parent view', () => {
|
2018-01-09 00:57:50 -05:00
|
|
|
class ParentDirective {
|
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: ParentDirective,
|
|
|
|
factory: () => new ParentDirective(),
|
|
|
|
features: [PublicFeature]
|
|
|
|
});
|
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
class ChildDirective {
|
|
|
|
value: string;
|
|
|
|
constructor(public parent: ParentDirective) {
|
|
|
|
this.value = (parent.constructor as any).name;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: ChildDirective,
|
|
|
|
factory: () => new ChildDirective(inject(ParentDirective)),
|
|
|
|
features: [PublicFeature]
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class Child2Directive {
|
|
|
|
value: boolean;
|
|
|
|
constructor(parent: ParentDirective, child: ChildDirective) {
|
|
|
|
this.value = parent === child.parent;
|
|
|
|
}
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: Child2Directive,
|
|
|
|
factory: () => new Child2Directive(inject(ParentDirective), inject(ChildDirective))
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Template(ctx: any, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-01-09 00:57:50 -05:00
|
|
|
E(0, 'div', null, [ParentDirective]);
|
2017-12-01 17:23:03 -05:00
|
|
|
{
|
2017-12-11 17:08:52 -05:00
|
|
|
C(2);
|
2017-12-01 17:23:03 -05:00
|
|
|
c();
|
|
|
|
}
|
|
|
|
e();
|
|
|
|
}
|
2017-12-14 19:26:28 -05:00
|
|
|
cR(2);
|
2017-12-01 17:23:03 -05:00
|
|
|
{
|
|
|
|
if (V(0)) {
|
2018-01-09 00:57:50 -05:00
|
|
|
E(0, 'span', null, [ChildDirective, Child2Directive]);
|
|
|
|
{ T(3); }
|
2017-12-01 17:23:03 -05:00
|
|
|
e();
|
|
|
|
}
|
2017-12-11 17:08:52 -05:00
|
|
|
t(3, b2('', D<ChildDirective>(1).value, '-', D<Child2Directive>(2).value, ''));
|
2017-12-01 17:23:03 -05:00
|
|
|
v();
|
|
|
|
}
|
2017-12-14 19:26:28 -05:00
|
|
|
cr();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(renderToHtml(Template, {})).toEqual('<div><span>ParentDirective-true</span></div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should inject from module Injector', () => {
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getOrCreateNodeInjector', () => {
|
|
|
|
it('should handle initial undefined state', () => {
|
2018-01-08 23:17:13 -05:00
|
|
|
const contentView = createLView(-1, null !, []);
|
2017-12-01 17:23:03 -05:00
|
|
|
const oldView = enterView(contentView, null !);
|
|
|
|
try {
|
2017-12-08 14:48:54 -05:00
|
|
|
const parent = createLNode(0, LNodeFlags.Element, null, null);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
// Simulate the situation where the previous parent is not initialized.
|
|
|
|
// This happens on first bootstrap because we don't init existing values
|
|
|
|
// so that we have smaller HelloWorld.
|
|
|
|
(parent as{parent: any}).parent = undefined;
|
|
|
|
|
|
|
|
const injector = getOrCreateNodeInjector();
|
|
|
|
expect(injector).not.toBe(null);
|
|
|
|
} finally {
|
|
|
|
leaveView(oldView);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|