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
|
|
|
|
*/
|
|
|
|
|
2018-10-18 03:23:18 -04:00
|
|
|
import {Component, ElementRef, InjectFlags, Injectable, InjectionToken, InjectorType, Provider, ViewEncapsulation, createInjector, defineInjectable, defineInjector, inject} from '../../src/core';
|
|
|
|
import {forwardRef} from '../../src/di/forward_ref';
|
2018-03-13 14:48:09 -04:00
|
|
|
import {getRenderedText} from '../../src/render3/component';
|
2018-10-18 03:23:18 -04:00
|
|
|
|
|
|
|
import {AttributeMarker, ComponentFactory, LifecycleHooksFeature, defineComponent, defineDirective, directiveInject, markDirty, template, ProvidersFeature, NgModuleType} from '../../src/render3/index';
|
|
|
|
import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, nextContext, text, textBinding, tick, projectionDef, projection} from '../../src/render3/instructions';
|
|
|
|
import {ComponentDef, RenderFlags} from '../../src/render3/interfaces/definition';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
import {NgIf} from './common_with_def';
|
2017-12-11 10:30:46 -05:00
|
|
|
import {getRendererFactory2} from './imported_renderer2';
|
2018-07-17 14:45:49 -04:00
|
|
|
import {ComponentFixture, containerEl, createComponent, renderComponent, renderToHtml, requestAnimationFrame, toHtml} from './render_util';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
describe('component', () => {
|
|
|
|
class CounterComponent {
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
increment() { this.count++; }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: CounterComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['counter']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: CounterComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.count));
|
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
},
|
|
|
|
factory: () => new CounterComponent,
|
|
|
|
inputs: {count: 'count'},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('renderComponent', () => {
|
|
|
|
it('should render on initial call', () => {
|
|
|
|
renderComponent(CounterComponent);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('0');
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should re-render on input change or method invocation', () => {
|
|
|
|
const component = renderComponent(CounterComponent);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('0');
|
2017-12-01 17:23:03 -05:00
|
|
|
component.count = 123;
|
2018-02-03 23:34:30 -05:00
|
|
|
markDirty(component);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('0');
|
2017-12-01 17:23:03 -05:00
|
|
|
requestAnimationFrame.flush();
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('123');
|
2017-12-01 17:23:03 -05:00
|
|
|
component.increment();
|
2018-02-03 23:34:30 -05:00
|
|
|
markDirty(component);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('123');
|
2017-12-01 17:23:03 -05:00
|
|
|
requestAnimationFrame.flush();
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('124');
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
2018-04-12 18:54:16 -04:00
|
|
|
class MyService {
|
|
|
|
constructor(public value: string) {}
|
|
|
|
static ngInjectableDef =
|
|
|
|
defineInjectable({providedIn: 'root', factory: () => new MyService('no-injector')});
|
|
|
|
}
|
|
|
|
class MyComponent {
|
|
|
|
constructor(public myService: MyService) {}
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: MyComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-04-12 18:54:16 -04:00
|
|
|
selectors: [['my-component']],
|
|
|
|
factory: () => new MyComponent(directiveInject(MyService)),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2018-04-12 18:54:16 -04:00
|
|
|
template: function(fs: RenderFlags, ctx: MyComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
text(0);
|
|
|
|
}
|
|
|
|
if (fs & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.myService.value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyModule {
|
|
|
|
static ngInjectorDef = defineInjector({
|
|
|
|
factory: () => new MyModule(),
|
|
|
|
providers: [{provide: MyService, useValue: new MyService('injector')}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should support bootstrapping without injector', () => {
|
|
|
|
const fixture = new ComponentFixture(MyComponent);
|
|
|
|
expect(fixture.html).toEqual('no-injector');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support bootstrapping with injector', () => {
|
|
|
|
const fixture = new ComponentFixture(MyComponent, {injector: createInjector(MyModule)});
|
|
|
|
expect(fixture.html).toEqual('injector');
|
|
|
|
});
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-12-11 10:30:46 -05:00
|
|
|
|
2018-01-25 09:32:21 -05:00
|
|
|
describe('component with a container', () => {
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
function showItems(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
for (const item of ctx.items) {
|
2018-08-18 14:14:50 -04:00
|
|
|
const rf0 = embeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (rf0 & RenderFlags.Create) {
|
|
|
|
text(0);
|
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(item));
|
|
|
|
}
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
embeddedViewEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WrapperComponent {
|
2018-06-18 19:38:33 -04:00
|
|
|
// TODO(issue/24571): remove '!'.
|
|
|
|
items !: string[];
|
2018-01-25 09:32:21 -05:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: WrapperComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['wrapper']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function ChildComponentTemplate(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-18 14:14:50 -04:00
|
|
|
const rf0 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{ showItems(rf0, {items: ctx.items}); }
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new WrapperComponent,
|
|
|
|
inputs: {items: 'items'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
function template(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'wrapper');
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'items', bind(ctx.items));
|
|
|
|
}
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const defs = [WrapperComponent];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2018-01-25 09:32:21 -05:00
|
|
|
it('should re-render on input change', () => {
|
|
|
|
const ctx: {items: string[]} = {items: ['a']};
|
2018-08-18 14:14:50 -04:00
|
|
|
expect(renderToHtml(template, ctx, 1, 1, defs)).toEqual('<wrapper>a</wrapper>');
|
2018-01-25 09:32:21 -05:00
|
|
|
|
|
|
|
ctx.items = [...ctx.items, 'b'];
|
2018-08-18 14:14:50 -04:00
|
|
|
expect(renderToHtml(template, ctx, 1, 1, defs)).toEqual('<wrapper>ab</wrapper>');
|
2018-01-25 09:32:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
// TODO: add tests with Native once tests are run in real browser (domino doesn't support shadow
|
|
|
|
// root)
|
|
|
|
describe('encapsulation', () => {
|
|
|
|
class WrapperComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: WrapperComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['wrapper']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: WrapperComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'encapsulated');
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new WrapperComponent,
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: () => [EncapsulatedComponent]
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class EncapsulatedComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: EncapsulatedComponent,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['encapsulated']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: EncapsulatedComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0, 'foo');
|
2018-07-27 17:28:22 -04:00
|
|
|
element(1, 'leaf');
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new EncapsulatedComponent,
|
2018-08-08 16:22:52 -04:00
|
|
|
encapsulation: ViewEncapsulation.Emulated,
|
|
|
|
styles: [],
|
|
|
|
data: {},
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: () => [LeafComponent]
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeafComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: LeafComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['leaf']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: LeafComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, 'span');
|
|
|
|
{ text(1, 'bar'); }
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new LeafComponent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should encapsulate children, but not host nor grand children', () => {
|
2018-03-06 13:13:49 -05:00
|
|
|
renderComponent(WrapperComponent, {rendererFactory: getRendererFactory2(document)});
|
2017-12-11 10:30:46 -05:00
|
|
|
expect(containerEl.outerHTML)
|
2018-01-03 04:45:09 -05:00
|
|
|
.toMatch(
|
|
|
|
/<div host=""><encapsulated _nghost-c(\d+)="">foo<leaf _ngcontent-c\1=""><span>bar<\/span><\/leaf><\/encapsulated><\/div>/);
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should encapsulate host', () => {
|
2018-03-06 13:13:49 -05:00
|
|
|
renderComponent(EncapsulatedComponent, {rendererFactory: getRendererFactory2(document)});
|
2017-12-11 10:30:46 -05:00
|
|
|
expect(containerEl.outerHTML)
|
2018-01-03 04:45:09 -05:00
|
|
|
.toMatch(
|
|
|
|
/<div host="" _nghost-c(\d+)="">foo<leaf _ngcontent-c\1=""><span>bar<\/span><\/leaf><\/div>/);
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should encapsulate host and children with different attributes', () => {
|
|
|
|
class WrapperComponentWith {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: WrapperComponentWith,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['wrapper']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: WrapperComponentWith) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'leaf');
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new WrapperComponentWith,
|
2018-08-08 16:22:52 -04:00
|
|
|
encapsulation: ViewEncapsulation.Emulated,
|
|
|
|
styles: [],
|
|
|
|
data: {},
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: () => [LeafComponentwith]
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeafComponentwith {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: LeafComponentwith,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['leaf']],
|
2018-08-21 03:03:21 -04:00
|
|
|
consts: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: LeafComponentwith) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, 'span');
|
|
|
|
{ text(1, 'bar'); }
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new LeafComponentwith,
|
2018-08-08 16:22:52 -04:00
|
|
|
encapsulation: ViewEncapsulation.Emulated,
|
|
|
|
styles: [],
|
|
|
|
data: {},
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
renderComponent(WrapperComponentWith, {rendererFactory: getRendererFactory2(document)});
|
2017-12-11 10:30:46 -05:00
|
|
|
expect(containerEl.outerHTML)
|
2018-01-03 04:45:09 -05:00
|
|
|
.toMatch(
|
|
|
|
/<div host="" _nghost-c(\d+)=""><leaf _ngcontent-c\1="" _nghost-c(\d+)=""><span _ngcontent-c\2="">bar<\/span><\/leaf><\/div>/);
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
2018-02-03 23:34:30 -05:00
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
2018-03-13 14:48:09 -04:00
|
|
|
|
|
|
|
describe('recursive components', () => {
|
2018-07-17 14:45:49 -04:00
|
|
|
let events: string[];
|
|
|
|
let count: number;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
events = [];
|
|
|
|
count = 0;
|
|
|
|
});
|
2018-03-13 14:48:09 -04:00
|
|
|
|
|
|
|
class TreeNode {
|
|
|
|
constructor(
|
|
|
|
public value: number, public depth: number, public left: TreeNode|null,
|
|
|
|
public right: TreeNode|null) {}
|
|
|
|
}
|
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
/**
|
|
|
|
* {{ data.value }}
|
|
|
|
*
|
|
|
|
* % if (data.left != null) {
|
|
|
|
* <tree-comp [data]="data.left"></tree-comp>
|
|
|
|
* % }
|
|
|
|
* % if (data.right != null) {
|
|
|
|
* <tree-comp [data]="data.right"></tree-comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-03-13 14:48:09 -04:00
|
|
|
class TreeComponent {
|
|
|
|
data: TreeNode = _buildTree(0);
|
|
|
|
|
|
|
|
ngDoCheck() { events.push('check' + this.data.value); }
|
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
ngOnDestroy() { events.push('destroy' + this.data.value); }
|
|
|
|
|
2018-03-13 14:48:09 -04:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: TreeComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['tree-comp']],
|
2018-03-13 14:48:09 -04:00
|
|
|
factory: () => new TreeComponent(),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 3,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: (rf: RenderFlags, ctx: TreeComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-13 14:48:09 -04:00
|
|
|
text(0);
|
|
|
|
container(1);
|
|
|
|
container(2);
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.data.value));
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
if (ctx.data.left != null) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf0 = embeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'tree-comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'data', bind(ctx.data.left));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
|
|
|
containerRefreshStart(2);
|
|
|
|
{
|
|
|
|
if (ctx.data.right != null) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf0 = embeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'tree-comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'data', bind(ctx.data.right));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
inputs: {data: 'data'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:12:06 -04:00
|
|
|
(TreeComponent.ngComponentDef as ComponentDef<TreeComponent>).directiveDefs =
|
2018-04-14 12:18:38 -04:00
|
|
|
() => [TreeComponent.ngComponentDef];
|
2018-03-13 14:48:09 -04:00
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
/**
|
|
|
|
* {{ data.value }}
|
|
|
|
* <ng-if-tree [data]="data.left" *ngIf="data.left"></ng-if-tree>
|
|
|
|
* <ng-if-tree [data]="data.right" *ngIf="data.right"></ng-if-tree>
|
|
|
|
*/
|
|
|
|
class NgIfTree {
|
|
|
|
data: TreeNode = _buildTree(0);
|
|
|
|
|
|
|
|
ngOnDestroy() { events.push('destroy' + this.data.value); }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: NgIfTree,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-07-17 14:45:49 -04:00
|
|
|
selectors: [['ng-if-tree']],
|
|
|
|
factory: () => new NgIfTree(),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 3,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 3,
|
2018-07-17 14:45:49 -04:00
|
|
|
template: (rf: RenderFlags, ctx: NgIfTree) => {
|
|
|
|
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
text(0);
|
2018-08-18 14:14:50 -04:00
|
|
|
template(1, IfTemplate, 1, 1, '', [AttributeMarker.SelectOnly, 'ngIf']);
|
|
|
|
template(2, IfTemplate2, 1, 1, '', [AttributeMarker.SelectOnly, 'ngIf']);
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.data.value));
|
|
|
|
elementProperty(1, 'ngIf', bind(ctx.data.left));
|
|
|
|
elementProperty(2, 'ngIf', bind(ctx.data.right));
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
inputs: {data: 'data'},
|
|
|
|
});
|
|
|
|
}
|
2018-07-17 21:59:49 -04:00
|
|
|
|
2018-07-25 20:25:22 -04:00
|
|
|
function IfTemplate(rf: RenderFlags, left: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-17 21:59:49 -04:00
|
|
|
elementStart(0, 'ng-if-tree');
|
|
|
|
elementEnd();
|
|
|
|
}
|
2018-07-25 20:25:22 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
const parent = nextContext();
|
2018-07-17 21:59:49 -04:00
|
|
|
elementProperty(0, 'data', bind(parent.data.left));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 20:25:22 -04:00
|
|
|
function IfTemplate2(rf: RenderFlags, right: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-17 21:59:49 -04:00
|
|
|
elementStart(0, 'ng-if-tree');
|
|
|
|
elementEnd();
|
|
|
|
}
|
2018-07-25 20:25:22 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
const parent = nextContext();
|
2018-07-17 21:59:49 -04:00
|
|
|
elementProperty(0, 'data', bind(parent.data.right));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:12:06 -04:00
|
|
|
(NgIfTree.ngComponentDef as ComponentDef<NgIfTree>).directiveDefs =
|
2018-07-17 14:45:49 -04:00
|
|
|
() => [NgIfTree.ngComponentDef, NgIf.ngDirectiveDef];
|
|
|
|
|
2018-03-13 14:48:09 -04:00
|
|
|
function _buildTree(currDepth: number): TreeNode {
|
|
|
|
const children = currDepth < 2 ? _buildTree(currDepth + 1) : null;
|
|
|
|
const children2 = currDepth < 2 ? _buildTree(currDepth + 1) : null;
|
|
|
|
return new TreeNode(count++, currDepth, children, children2);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should check each component just once', () => {
|
|
|
|
const comp = renderComponent(TreeComponent, {hostFeatures: [LifecycleHooksFeature]});
|
|
|
|
expect(getRenderedText(comp)).toEqual('6201534');
|
|
|
|
expect(events).toEqual(['check6', 'check2', 'check0', 'check1', 'check5', 'check3', 'check4']);
|
|
|
|
|
|
|
|
events = [];
|
|
|
|
tick(comp);
|
|
|
|
expect(events).toEqual(['check6', 'check2', 'check0', 'check1', 'check5', 'check3', 'check4']);
|
|
|
|
});
|
2018-05-09 19:49:39 -04:00
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
// This tests that the view tree is set up properly for recursive components
|
|
|
|
it('should call onDestroys properly', () => {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* % if (!skipContent) {
|
|
|
|
* <tree-comp></tree-comp>
|
|
|
|
* % }
|
|
|
|
*/
|
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
container(0);
|
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
if (!ctx.skipContent) {
|
2018-08-18 14:14:50 -04:00
|
|
|
const rf0 = embeddedViewStart(0, 1, 0);
|
2018-07-17 14:45:49 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'tree-comp');
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, [TreeComponent]);
|
2018-07-17 14:45:49 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
expect(getRenderedText(fixture.component)).toEqual('6201534');
|
|
|
|
|
|
|
|
events = [];
|
|
|
|
fixture.component.skipContent = true;
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(
|
|
|
|
['destroy0', 'destroy1', 'destroy2', 'destroy3', 'destroy4', 'destroy5', 'destroy6']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onDestroys properly with ngIf', () => {
|
|
|
|
/**
|
|
|
|
* % if (!skipContent) {
|
|
|
|
* <ng-if-tree></ng-if-tree>
|
|
|
|
* % }
|
|
|
|
*/
|
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
container(0);
|
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
if (!ctx.skipContent) {
|
2018-08-18 14:14:50 -04:00
|
|
|
const rf0 = embeddedViewStart(0, 1, 0);
|
2018-07-17 14:45:49 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'ng-if-tree');
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, [NgIfTree]);
|
2018-07-17 14:45:49 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
expect(getRenderedText(fixture.component)).toEqual('6201534');
|
|
|
|
|
|
|
|
events = [];
|
|
|
|
fixture.component.skipContent = true;
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(
|
|
|
|
['destroy0', 'destroy1', 'destroy2', 'destroy3', 'destroy4', 'destroy5', 'destroy6']);
|
|
|
|
});
|
|
|
|
|
2018-05-09 19:49:39 -04:00
|
|
|
it('should map inputs minified & unminified names', async() => {
|
|
|
|
class TestInputsComponent {
|
2018-06-18 19:38:33 -04:00
|
|
|
// TODO(issue/24571): remove '!'.
|
|
|
|
minifiedName !: string;
|
2018-05-09 19:49:39 -04:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: TestInputsComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-05-09 19:49:39 -04:00
|
|
|
selectors: [['test-inputs']],
|
|
|
|
inputs: {minifiedName: 'unminifiedName'},
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 0,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-05-09 19:49:39 -04:00
|
|
|
factory: () => new TestInputsComponent(),
|
|
|
|
template: function(rf: RenderFlags, ctx: TestInputsComponent): void {
|
|
|
|
// Template not needed for this test
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const testInputsComponentFactory = new ComponentFactory(TestInputsComponent.ngComponentDef);
|
|
|
|
|
|
|
|
expect([
|
|
|
|
{propName: 'minifiedName', templateName: 'unminifiedName'}
|
|
|
|
]).toEqual(testInputsComponentFactory.inputs);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-03-13 14:48:09 -04:00
|
|
|
});
|
2018-10-18 03:23:18 -04:00
|
|
|
|
|
|
|
describe('providers', () => {
|
|
|
|
describe('should support all types of Provider:', () => {
|
|
|
|
abstract class Greeter { abstract greet: string; }
|
|
|
|
|
|
|
|
const GREETER = new InjectionToken<Greeter>('greeter');
|
|
|
|
|
|
|
|
class GreeterClass implements Greeter {
|
|
|
|
greet = 'Class';
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreeterDeps implements Greeter {
|
|
|
|
constructor(public greet: string) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreeterBuiltInDeps implements Greeter {
|
|
|
|
public greet: string;
|
|
|
|
constructor(private message: string, private elementRef: ElementRef) {
|
|
|
|
this.greet = this.message + ' from ' + this.elementRef.nativeElement.tagName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreeterProvider {
|
|
|
|
provide() { return 'Provided'; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class GreeterInj implements Greeter {
|
|
|
|
public greet: string;
|
|
|
|
constructor(private provider: GreeterProvider) { this.greet = this.provider.provide(); }
|
|
|
|
|
|
|
|
static ngInjectableDef =
|
|
|
|
defineInjectable({factory: () => new GreeterInj(inject(GreeterProvider as any))});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('TypeProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterClass],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(GreeterClass).greet).toEqual('Class'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ValueProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: GREETER, useValue: {greet: 'Value'}}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Value'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: GREETER, useClass: GreeterClass}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Class'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ExistingProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterClass, {provide: GREETER, useExisting: GreeterClass}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Class'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FactoryProvider', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterClass, {provide: GREETER, useFactory: () => new GreeterClass()}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Class'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const MESSAGE = new InjectionToken<string>('message');
|
|
|
|
|
|
|
|
it('ClassProvider with deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'},
|
|
|
|
{provide: GREETER, useClass: GreeterDeps, deps: [MESSAGE]}
|
|
|
|
],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Message'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider with built-in deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'},
|
|
|
|
{provide: GREETER, useClass: GreeterBuiltInDeps, deps: [MESSAGE, ElementRef]}
|
|
|
|
],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(GREETER).greet).toEqual('Message from PARENT'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FactoryProvider with deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'},
|
|
|
|
{provide: GREETER, useFactory: (msg: string) => new GreeterDeps(msg), deps: [MESSAGE]}
|
|
|
|
],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Message'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FactoryProvider with built-in deps', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [
|
|
|
|
{provide: MESSAGE, useValue: 'Message'}, {
|
|
|
|
provide: GREETER,
|
|
|
|
useFactory: (msg: string, elementRef: ElementRef) =>
|
|
|
|
new GreeterBuiltInDeps(msg, elementRef),
|
|
|
|
deps: [MESSAGE, ElementRef]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(GREETER).greet).toEqual('Message from PARENT'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ClassProvider with injectable', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [GreeterProvider, {provide: GREETER, useClass: GreeterInj}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Provided'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwardRef', (done) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [forwardRef(() => ForLater)],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(ForLater) instanceof ForLater).toBeTruthy(); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
class ForLater {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All tests below assume this structure:
|
|
|
|
* ```
|
|
|
|
* <parent>
|
|
|
|
* <#VIEW#>
|
|
|
|
* <view-child>
|
|
|
|
* </view-child>
|
|
|
|
* </#VIEW#>
|
|
|
|
* <content-child>
|
|
|
|
* </content-child>
|
|
|
|
* </parent>
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('override rules:', () => {
|
|
|
|
it('directiveProviders should override providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('viewProviders should override providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('viewProviders should override directiveProviders', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last declared directive should override other directives', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Message 1'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last provider should override previous one in component providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers:
|
|
|
|
[{provide: String, useValue: 'Message 1'}, {provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last provider should override previous one in component view providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders:
|
|
|
|
[{provide: String, useValue: 'Message 1'}, {provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('last provider should override previous one in directive providers', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders:
|
|
|
|
[{provide: String, useValue: 'Message 1'}, {provide: String, useValue: 'Message 2'}],
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('Message 2'); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('single', () => {
|
|
|
|
class MyModule {
|
|
|
|
static ngInjectorDef = defineInjector(
|
|
|
|
{factory: () => new MyModule(), providers: [{provide: String, useValue: 'From module'}]});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('without directives', () => {
|
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('From module'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From module'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('From module'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From module'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('From module'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From module'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From providers'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From providers'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From providers'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From providers'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From providers'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From providers'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From module'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual('From module'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From module'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From providers'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From providers'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From providers'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with directives (order in ngComponentDef.directives matters)', () => {
|
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders'}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive'}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'Never'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From viewProviders'); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual('From directive'); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual('From directive'); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('multi', () => {
|
|
|
|
class MyModule {
|
|
|
|
static ngInjectorDef = defineInjector({
|
|
|
|
factory: () => new MyModule(),
|
|
|
|
providers: [{provide: String, useValue: 'From module', multi: true}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('without directives', () => {
|
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From viewProviders']); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From viewProviders']); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From viewProviders']); }
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); },
|
|
|
|
directiveAssertion: () => { expect(directiveInject(String)).toEqual(['From module']); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From providers', 'From viewProviders']);
|
|
|
|
},
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); }
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From providers', 'From viewProviders']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From providers', 'From viewProviders']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); },
|
|
|
|
directiveAssertion:
|
|
|
|
() => { expect(directiveInject(String)).toEqual(['From providers']); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with directives (order in ngComponentDef.directives matters)', () => {
|
|
|
|
it('should work without providers nor viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only providers in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with only viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual(['From directive 2', 'From directive 1']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both providers and viewProviders in component', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [{provide: String, useValue: 'From providers', multi: true}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'From viewProviders', multi: true}],
|
|
|
|
directiveProviders: [{provide: String, useValue: 'From directive 1', multi: true}],
|
|
|
|
directive2Providers: [{provide: String, useValue: 'From directive 2', multi: true}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From viewProviders', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contentChild: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
directiveAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual([
|
|
|
|
'From providers', 'From directive 2', 'From directive 1'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('tree-shakable injectables', () => {
|
|
|
|
it('should work with root', () => {
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
class FooForRoot {
|
|
|
|
static ngInjectableDef =
|
|
|
|
defineInjectable({factory: () => new FooForRoot(), providedIn: 'root'});
|
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(FooForRoot) instanceof FooForRoot).toBeTruthy(); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with a module', () => {
|
|
|
|
class MyModule {
|
|
|
|
static ngInjectorDef = defineInjector({
|
|
|
|
factory: () => new MyModule(),
|
|
|
|
providers: [{provide: String, useValue: 'From module'}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({providedIn: MyModule})
|
|
|
|
class FooForModule {
|
|
|
|
static ngInjectableDef =
|
|
|
|
defineInjectable({factory: () => new FooForModule(), providedIn: MyModule});
|
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(FooForModule) instanceof FooForModule).toBeTruthy(); }
|
|
|
|
},
|
|
|
|
ngModule: MyModule
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('- embedded views', () => {
|
|
|
|
it('should have access to viewProviders of the host component', () => {
|
|
|
|
@Component({
|
|
|
|
template: '{{s}}{{n}}',
|
|
|
|
})
|
|
|
|
class Repeated {
|
|
|
|
constructor(private s: String, private n: Number) {}
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Repeated,
|
|
|
|
selectors: [['repeated']],
|
|
|
|
factory: () => new Repeated(directiveInject(String), directiveInject(Number)),
|
|
|
|
consts: 2,
|
|
|
|
vars: 2,
|
|
|
|
template: function(fs: RenderFlags, ctx: Repeated) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
text(0);
|
|
|
|
text(1);
|
|
|
|
}
|
|
|
|
if (fs & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.s));
|
|
|
|
textBinding(1, bind(ctx.n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `<div>
|
|
|
|
% for (let i = 0; i < 3; i++) {
|
|
|
|
<repeated></repeated>
|
|
|
|
% }
|
|
|
|
</div>`,
|
|
|
|
providers: [{provide: Number, useValue: 1, multi: true}],
|
|
|
|
viewProviders:
|
|
|
|
[{provide: String, useValue: 'foo'}, {provide: Number, useValue: 2, multi: true}],
|
|
|
|
})
|
|
|
|
class ComponentWithProviders {
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: ComponentWithProviders,
|
|
|
|
selectors: [['component-with-providers']],
|
|
|
|
factory: () => new ComponentWithProviders(),
|
|
|
|
consts: 2,
|
|
|
|
vars: 0,
|
|
|
|
template: function(fs: RenderFlags, ctx: ComponentWithProviders) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'div');
|
|
|
|
{ container(1); }
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (fs & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
let rf1 = embeddedViewStart(1, 1, 0);
|
|
|
|
{
|
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
element(0, 'repeated');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: [
|
|
|
|
ProvidersFeature(
|
|
|
|
[{provide: Number, useValue: 1, multi: true}],
|
|
|
|
[{provide: String, useValue: 'foo'}, {provide: Number, useValue: 2, multi: true}]),
|
|
|
|
],
|
|
|
|
directives: [Repeated]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(ComponentWithProviders);
|
|
|
|
expect(fixture.html)
|
|
|
|
.toEqual(
|
|
|
|
'<div><repeated>foo1,2</repeated><repeated>foo1,2</repeated><repeated>foo1,2</repeated></div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have access to viewProviders of the repeated component', () => {
|
|
|
|
@Component({
|
|
|
|
template: '{{s}}{{n}}',
|
|
|
|
providers: [{provide: Number, useValue: 1, multi: true}],
|
|
|
|
viewProviders:
|
|
|
|
[{provide: String, useValue: 'bar'}, {provide: Number, useValue: 2, multi: true}]
|
|
|
|
})
|
|
|
|
class Repeated {
|
|
|
|
constructor(private s: String, private n: Number) {}
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Repeated,
|
|
|
|
selectors: [['repeated']],
|
|
|
|
factory: () => new Repeated(directiveInject(String), directiveInject(Number)),
|
|
|
|
consts: 2,
|
|
|
|
vars: 2,
|
|
|
|
template: function(fs: RenderFlags, ctx: Repeated) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
text(0);
|
|
|
|
text(1);
|
|
|
|
}
|
|
|
|
if (fs & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.s));
|
|
|
|
textBinding(1, bind(ctx.n));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: [
|
|
|
|
ProvidersFeature(
|
|
|
|
[{provide: Number, useValue: 1, multi: true}],
|
|
|
|
[{provide: String, useValue: 'bar'}, {provide: Number, useValue: 2, multi: true}]),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `<div>
|
|
|
|
% for (let i = 0; i < 3; i++) {
|
|
|
|
<repeated></repeated>
|
|
|
|
% }
|
|
|
|
</div>`,
|
|
|
|
viewProviders: [{provide: toString, useValue: 'foo'}],
|
|
|
|
})
|
|
|
|
class ComponentWithProviders {
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: ComponentWithProviders,
|
|
|
|
selectors: [['component-with-providers']],
|
|
|
|
factory: () => new ComponentWithProviders(),
|
|
|
|
consts: 2,
|
|
|
|
vars: 0,
|
|
|
|
template: function(fs: RenderFlags, ctx: ComponentWithProviders) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'div');
|
|
|
|
{ container(1); }
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (fs & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
let rf1 = embeddedViewStart(1, 1, 0);
|
|
|
|
{
|
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
element(0, 'repeated');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: [ProvidersFeature([], [{provide: String, useValue: 'foo'}])],
|
|
|
|
directives: [Repeated]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(ComponentWithProviders);
|
|
|
|
expect(fixture.html)
|
|
|
|
.toEqual(
|
|
|
|
'<div><repeated>bar1,2</repeated><repeated>bar1,2</repeated><repeated>bar1,2</repeated></div>');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deps boundary:', () => {
|
|
|
|
it('the deps of a token declared in providers should not be resolved with tokens from viewProviders',
|
|
|
|
() => {
|
|
|
|
@Injectable()
|
|
|
|
class MyService {
|
|
|
|
constructor(public value: String) {}
|
|
|
|
|
|
|
|
static ngInjectableDef =
|
|
|
|
defineInjectable({factory: () => new MyService(inject(String))});
|
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [MyService, {provide: String, useValue: 'providers'}],
|
|
|
|
viewProviders: [{provide: String, useValue: 'viewProviders'}],
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual('viewProviders');
|
|
|
|
expect(directiveInject(MyService).value).toEqual('providers');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should make sure that parent service does not see overrides in child directives', () => {
|
|
|
|
class Greeter {
|
|
|
|
static ngInjectableDef = defineInjectable({factory: () => new Greeter(inject(String))});
|
|
|
|
constructor(public greeting: String) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectProvidersScenario({
|
|
|
|
parent: {
|
|
|
|
providers: [Greeter, {provide: String, useValue: 'parent'}],
|
|
|
|
},
|
|
|
|
viewChild: {
|
|
|
|
providers: [{provide: String, useValue: 'view'}],
|
|
|
|
componentAssertion:
|
|
|
|
() => { expect(directiveInject(Greeter).greeting).toEqual('parent'); },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('injection flags', () => {
|
|
|
|
class MyModule {
|
|
|
|
static ngInjectorDef = defineInjector(
|
|
|
|
{factory: () => new MyModule(), providers: [{provide: String, useValue: 'Module'}]});
|
|
|
|
}
|
|
|
|
it('should not fall through to ModuleInjector if flags limit the scope', () => {
|
|
|
|
expectProvidersScenario({
|
|
|
|
ngModule: MyModule,
|
|
|
|
parent: {
|
|
|
|
componentAssertion: () => {
|
|
|
|
expect(directiveInject(String)).toEqual('Module');
|
|
|
|
expect(directiveInject(String, InjectFlags.Optional | InjectFlags.Self))
|
|
|
|
.toEqual(undefined as any);
|
|
|
|
expect(directiveInject(String, InjectFlags.Optional | InjectFlags.Host))
|
|
|
|
.toEqual(undefined as any);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
interface ComponentTest {
|
|
|
|
providers?: Provider[];
|
|
|
|
viewProviders?: Provider[];
|
|
|
|
directiveProviders?: Provider[];
|
|
|
|
directive2Providers?: Provider[];
|
|
|
|
directiveAssertion?: () => void;
|
|
|
|
componentAssertion?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectProvidersScenario(defs: {
|
|
|
|
app?: ComponentTest,
|
|
|
|
parent?: ComponentTest,
|
|
|
|
viewChild?: ComponentTest,
|
|
|
|
contentChild?: ComponentTest,
|
|
|
|
ngModule?: InjectorType<any>,
|
|
|
|
}): void {
|
|
|
|
function testComponentInjection<T>(def: ComponentTest | undefined, instance: T): T {
|
|
|
|
if (def) {
|
|
|
|
def.componentAssertion && def.componentAssertion();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDirectiveInjection<T>(def: ComponentTest | undefined, instance: T): T {
|
|
|
|
if (def) {
|
|
|
|
def.directiveAssertion && def.directiveAssertion();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewChildComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: ViewChildComponent,
|
|
|
|
selectors: [['view-child']],
|
|
|
|
consts: 1,
|
|
|
|
vars: 0,
|
|
|
|
factory: () => testComponentInjection(defs.viewChild, new ViewChildComponent()),
|
|
|
|
template: function(fs: RenderFlags, ctx: ViewChildComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
text(0, 'view-child');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: defs.viewChild &&
|
|
|
|
[
|
|
|
|
ProvidersFeature(defs.viewChild.providers || [], defs.viewChild.viewProviders || []),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewChildDirective {
|
|
|
|
static ngComponentDef = defineDirective({
|
|
|
|
type: ViewChildDirective,
|
|
|
|
selectors: [['view-child']],
|
|
|
|
factory: () => testDirectiveInjection(defs.viewChild, new ViewChildDirective()),
|
|
|
|
features: defs.viewChild && [ProvidersFeature(defs.viewChild.directiveProviders || [])],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContentChildComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: ContentChildComponent,
|
|
|
|
selectors: [['content-child']],
|
|
|
|
consts: 1,
|
|
|
|
vars: 0,
|
|
|
|
factory: () => testComponentInjection(defs.contentChild, new ContentChildComponent()),
|
|
|
|
template: function(fs: RenderFlags, ctx: ParentComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
text(0, 'content-child');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: defs.contentChild &&
|
|
|
|
[ProvidersFeature(
|
|
|
|
defs.contentChild.providers || [], defs.contentChild.viewProviders || [])],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContentChildDirective {
|
|
|
|
static ngComponentDef = defineDirective({
|
|
|
|
type: ContentChildDirective,
|
|
|
|
selectors: [['content-child']],
|
|
|
|
factory: () => testDirectiveInjection(defs.contentChild, new ContentChildDirective()),
|
|
|
|
features: defs.contentChild && [ProvidersFeature(defs.contentChild.directiveProviders || [])],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ParentComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: ParentComponent,
|
|
|
|
selectors: [['parent']],
|
|
|
|
consts: 1,
|
|
|
|
vars: 0,
|
|
|
|
factory: () => testComponentInjection(defs.parent, new ParentComponent()),
|
|
|
|
template: function(fs: RenderFlags, ctx: ParentComponent) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
element(0, 'view-child');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features: defs.parent &&
|
|
|
|
[ProvidersFeature(defs.parent.providers || [], defs.parent.viewProviders || [])],
|
|
|
|
directives: [ViewChildComponent, ViewChildDirective]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentDirective {
|
|
|
|
static ngComponentDef = defineDirective({
|
|
|
|
type: ParentDirective,
|
|
|
|
selectors: [['parent']],
|
|
|
|
factory: () => testDirectiveInjection(defs.parent, new ParentDirective()),
|
|
|
|
features: defs.parent && [ProvidersFeature(defs.parent.directiveProviders || [])],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentDirective2 {
|
|
|
|
static ngComponentDef = defineDirective({
|
|
|
|
type: ParentDirective2,
|
|
|
|
selectors: [['parent']],
|
|
|
|
factory: () => testDirectiveInjection(defs.parent, new ParentDirective2()),
|
|
|
|
features: defs.parent && [ProvidersFeature(defs.parent.directive2Providers || [])],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class App {
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: App,
|
|
|
|
selectors: [['app']],
|
|
|
|
consts: 2,
|
|
|
|
vars: 0,
|
|
|
|
factory: () => testComponentInjection(defs.app, new App()),
|
|
|
|
template: function(fs: RenderFlags, ctx: App) {
|
|
|
|
if (fs & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'parent');
|
|
|
|
element(1, 'content-child');
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
features:
|
|
|
|
defs.app && [ProvidersFeature(defs.app.providers || [], defs.app.viewProviders || [])],
|
|
|
|
directives: [
|
|
|
|
ParentComponent, ParentDirective2, ParentDirective, ContentChildComponent,
|
|
|
|
ContentChildDirective
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(
|
|
|
|
App, {injector: defs.ngModule ? createInjector(defs.ngModule) : undefined});
|
|
|
|
expect(fixture.html).toEqual('<parent><view-child>view-child</view-child></parent>');
|
|
|
|
}
|