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
|
|
|
|
*/
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
import {ViewEncapsulation, ΔdefineInjectable, ΔdefineInjector} from '../../src/core';
|
2019-04-04 14:41:52 -04:00
|
|
|
import {createInjector} from '../../src/di/r3_injector';
|
2019-05-09 14:47:25 -04:00
|
|
|
import {AttributeMarker, ComponentFactory, LifecycleHooksFeature, getRenderedText, markDirty, ΔdefineComponent, ΔdirectiveInject, Δtemplate} from '../../src/render3/index';
|
|
|
|
import {tick, Δbind, Δcontainer, ΔcontainerRefreshEnd, ΔcontainerRefreshStart, Δelement, ΔelementEnd, ΔelementProperty, ΔelementStart, ΔembeddedViewEnd, ΔembeddedViewStart, ΔnextContext, Δtext, ΔtextBinding} from '../../src/render3/instructions/all';
|
2018-10-18 03:23:18 -04:00
|
|
|
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';
|
2018-12-11 02:40:19 -05:00
|
|
|
import {ComponentFixture, MockRendererFactory, 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++; }
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtext(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔtextBinding(0, Δbind(ctx.count));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
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 =
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔdefineInjectable({providedIn: 'root', factory: () => new MyService('no-injector')});
|
2018-04-12 18:54:16 -04:00
|
|
|
}
|
|
|
|
class MyComponent {
|
|
|
|
constructor(public myService: MyService) {}
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-04-12 18:54:16 -04:00
|
|
|
type: MyComponent,
|
2018-07-31 14:14:06 -04:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2018-04-12 18:54:16 -04:00
|
|
|
selectors: [['my-component']],
|
2019-05-09 14:47:25 -04:00
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtext(0);
|
2018-04-12 18:54:16 -04:00
|
|
|
}
|
|
|
|
if (fs & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔtextBinding(0, Δbind(ctx.myService.value));
|
2018-04-12 18:54:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyModule {
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngInjectorDef = ΔdefineInjector({
|
2018-04-12 18:54:16 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-11-26 17:57:45 -05:00
|
|
|
it('should instantiate components at high indices', () => {
|
|
|
|
|
|
|
|
// {{ name }}
|
|
|
|
class Comp {
|
|
|
|
// @Input
|
|
|
|
name = '';
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-11-26 17:57:45 -05:00
|
|
|
type: Comp,
|
|
|
|
selectors: [['comp']],
|
|
|
|
factory: () => new Comp(),
|
|
|
|
consts: 1,
|
|
|
|
vars: 1,
|
|
|
|
template: (rf: RenderFlags, ctx: Comp) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtext(0);
|
2018-11-26 17:57:45 -05:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔtextBinding(0, Δbind(ctx.name));
|
2018-11-26 17:57:45 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
inputs: {name: 'name'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Artificially inflating the slot IDs of this app component to mimic an app
|
|
|
|
// with a very large view
|
|
|
|
const App = createComponent('app', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δelement(4097, 'comp');
|
2018-11-26 17:57:45 -05:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(4097, 'name', Δbind(ctx.name));
|
2018-11-26 17:57:45 -05:00
|
|
|
}
|
|
|
|
}, 4098, 1, [Comp]);
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
expect(fixture.html).toEqual('<comp></comp>');
|
|
|
|
|
|
|
|
fixture.component.name = 'some name';
|
|
|
|
fixture.update();
|
|
|
|
expect(fixture.html).toEqual('<comp>some name</comp>');
|
|
|
|
});
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
2017-12-11 10:30:46 -05:00
|
|
|
|
2018-12-11 02:40:19 -05:00
|
|
|
it('should not invoke renderer destroy method for embedded views', () => {
|
|
|
|
let comp: Comp;
|
|
|
|
|
|
|
|
function MyComponent_div_Template_2(rf: any, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'div');
|
|
|
|
Δtext(1, 'Child view');
|
|
|
|
ΔelementEnd();
|
2018-12-11 02:40:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Comp {
|
|
|
|
visible = true;
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-12-11 02:40:19 -05:00
|
|
|
type: Comp,
|
|
|
|
selectors: [['comp']],
|
|
|
|
consts: 3,
|
|
|
|
vars: 1,
|
|
|
|
factory: () => {
|
|
|
|
comp = new Comp();
|
|
|
|
return comp;
|
|
|
|
},
|
|
|
|
directives: [NgIf],
|
|
|
|
/**
|
|
|
|
* <div>Root view</div>
|
|
|
|
* <div *ngIf="visible">Child view</div>
|
|
|
|
*/
|
|
|
|
template: function(rf: RenderFlags, ctx: Comp) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'div');
|
|
|
|
Δtext(1, 'Root view');
|
|
|
|
ΔelementEnd();
|
|
|
|
Δtemplate(2, MyComponent_div_Template_2, 2, 0, 'div', [AttributeMarker.Template, 'ngIf']);
|
2018-12-11 02:40:19 -05:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(2, 'ngIf', Δbind(ctx.visible));
|
2018-12-11 02:40:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const rendererFactory = new MockRendererFactory(['destroy']);
|
|
|
|
const fixture = new ComponentFixture(Comp, {rendererFactory});
|
|
|
|
|
|
|
|
comp !.visible = false;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
comp !.visible = true;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
const renderer = rendererFactory.lastRenderer !;
|
|
|
|
const destroySpy = renderer.spies['destroy'];
|
|
|
|
|
|
|
|
// we should never see `destroy` method being called
|
|
|
|
// in case child views are created/removed
|
|
|
|
expect(destroySpy.calls.count()).toBe(0);
|
|
|
|
});
|
|
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δcontainer(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
for (const item of ctx.items) {
|
2019-05-09 14:47:25 -04:00
|
|
|
const rf0 = ΔembeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (rf0 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtext(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔtextBinding(0, Δbind(item));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:25 -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[];
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-01-25 09:32: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 ChildComponentTemplate(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δcontainer(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-05-09 14:47:25 -04:00
|
|
|
const rf0 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{ showItems(rf0, {items: ctx.items}); }
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
Δ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) {
|
2019-05-09 14:47:25 -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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'items', Δbind(ctx.items));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
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
|
|
|
});
|
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); }
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-03-13 14:48:09 -04:00
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtext(0);
|
|
|
|
Δcontainer(1);
|
|
|
|
Δcontainer(2);
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔtextBinding(0, Δbind(ctx.data.value));
|
|
|
|
ΔcontainerRefreshStart(1);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.data.left != null) {
|
2019-05-09 14:47:25 -04:00
|
|
|
let rf0 = ΔembeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δelement(0, 'tree-comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'data', Δbind(ctx.data.left));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
|
|
|
ΔcontainerRefreshStart(2);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.data.right != null) {
|
2019-05-09 14:47:25 -04:00
|
|
|
let rf0 = ΔembeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δelement(0, 'tree-comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'data', Δbind(ctx.data.right));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:25 -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);
|
|
|
|
|
2018-12-18 19:58:51 -05:00
|
|
|
ngDoCheck() { events.push('check' + this.data.value); }
|
|
|
|
|
2018-07-17 14:45:49 -04:00
|
|
|
ngOnDestroy() { events.push('destroy' + this.data.value); }
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-07-17 14:45:49 -04:00
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtext(0);
|
|
|
|
Δtemplate(
|
2019-03-07 03:31:31 -05:00
|
|
|
1, IfTemplate, 1, 1, 'ng-if-tree',
|
|
|
|
[AttributeMarker.Bindings, 'data', AttributeMarker.Template, 'ngIf']);
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtemplate(
|
2019-03-07 03:31:31 -05:00
|
|
|
2, IfTemplate2, 1, 1, 'ng-if-tree',
|
|
|
|
[AttributeMarker.Bindings, 'data', AttributeMarker.Template, 'ngIf']);
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔtextBinding(0, Δbind(ctx.data.value));
|
|
|
|
ΔelementProperty(1, 'ngIf', Δbind(ctx.data.left));
|
|
|
|
ΔelementProperty(2, 'ngIf', Δbind(ctx.data.right));
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'ng-if-tree');
|
|
|
|
ΔelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
2018-07-25 20:25:22 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
const parent = ΔnextContext();
|
|
|
|
ΔelementProperty(0, 'data', Δbind(parent.data.left));
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 20:25:22 -04:00
|
|
|
function IfTemplate2(rf: RenderFlags, right: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'ng-if-tree');
|
|
|
|
ΔelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
2018-07-25 20:25:22 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
const parent = ΔnextContext();
|
|
|
|
ΔelementProperty(0, 'data', Δbind(parent.data.right));
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δcontainer(0);
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-07-17 14:45:49 -04:00
|
|
|
if (!ctx.skipContent) {
|
2019-05-09 14:47:25 -04:00
|
|
|
const rf0 = ΔembeddedViewStart(0, 1, 0);
|
2018-07-17 14:45:49 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'tree-comp');
|
|
|
|
ΔelementEnd();
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δcontainer(0);
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-07-17 14:45:49 -04:00
|
|
|
if (!ctx.skipContent) {
|
2019-05-09 14:47:25 -04:00
|
|
|
const rf0 = ΔembeddedViewStart(0, 1, 0);
|
2018-07-17 14:45:49 -04:00
|
|
|
if (rf0 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'ng-if-tree');
|
|
|
|
ΔelementEnd();
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
2018-07-17 14:45:49 -04:00
|
|
|
}
|
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');
|
2018-12-18 19:58:51 -05:00
|
|
|
expect(events).toEqual(['check6', 'check2', 'check0', 'check1', 'check5', 'check3', 'check4']);
|
2018-07-17 14:45:49 -04:00
|
|
|
|
|
|
|
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;
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-05-09 19:49:39 -04:00
|
|
|
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
|
|
|
});
|