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-02-03 23:34:30 -05:00
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
import {DoCheck, Input, TemplateRef, ViewContainerRef, ViewEncapsulation, createInjector, defineInjectable, defineInjector} from '../../src/core';
|
2018-03-13 14:48:09 -04:00
|
|
|
import {getRenderedText} from '../../src/render3/component';
|
2018-08-15 21:37:03 -04:00
|
|
|
import {AttributeMarker, ComponentFactory, LifecycleHooksFeature, defineComponent, directiveInject, markDirty, template} from '../../src/render3/index';
|
2018-07-25 20:25:22 -04:00
|
|
|
import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, nextContext, text, textBinding, tick} from '../../src/render3/instructions';
|
2018-05-31 18:50:02 -04:00
|
|
|
import {ComponentDefInternal, DirectiveDefInternal, RenderFlags} from '../../src/render3/interfaces/definition';
|
2018-01-03 04:45:09 -05:00
|
|
|
import {createRendererType2} from '../../src/view/index';
|
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-05-31 18:50:02 -04:00
|
|
|
(TreeComponent.ngComponentDef as ComponentDefInternal<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-07-17 14:45:49 -04:00
|
|
|
(NgIfTree.ngComponentDef as ComponentDefInternal<NgIfTree>).directiveDefs =
|
|
|
|
() => [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
|
|
|
});
|