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-12-20 20:23:25 -05:00
|
|
|
import {ComponentFactoryResolver, OnDestroy, SimpleChange, SimpleChanges, ViewContainerRef} from '../../src/core';
|
2019-01-16 12:35:35 -05:00
|
|
|
import {AttributeMarker, ComponentTemplate, LifecycleHooksFeature, NO_CHANGE, NgOnChangesFeature, defineComponent, defineDirective, injectComponentFactoryResolver} from '../../src/render3/index';
|
2018-08-18 14:14:50 -04:00
|
|
|
|
2018-10-31 01:10:23 -04:00
|
|
|
import {bind, container, containerRefreshEnd, containerRefreshStart, directiveInject, element, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, listener, markDirty, projection, projectionDef, store, template, text} from '../../src/render3/instructions';
|
2018-04-10 23:57:09 -04:00
|
|
|
import {RenderFlags} from '../../src/render3/interfaces/definition';
|
2018-07-16 08:21:54 -04:00
|
|
|
|
|
|
|
import {NgIf} from './common_with_def';
|
|
|
|
import {ComponentFixture, containerEl, createComponent, renderComponent, renderToHtml, requestAnimationFrame} from './render_util';
|
2019-01-16 12:35:35 -05:00
|
|
|
import {fixmeIvy} from '@angular/private/testing';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
describe('lifecycles', () => {
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function getParentTemplate(name: string) {
|
2018-04-10 23:57:09 -04:00
|
|
|
return (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, name);
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(ctx.val));
|
|
|
|
}
|
2017-12-22 19:41:34 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
describe('onInit', () => {
|
|
|
|
let events: string[];
|
|
|
|
|
|
|
|
beforeEach(() => { events = []; });
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Comp = createOnInitComponent('comp', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
elementStart(0, 'div');
|
|
|
|
{ projection(1); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 2);
|
2018-08-21 03:03:21 -04:00
|
|
|
let Parent = createOnInitComponent('parent', getParentTemplate('comp'), 1, 1, [Comp]);
|
2018-04-10 23:57:09 -04:00
|
|
|
let ProjectedComp = createOnInitComponent('projected', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0, 'content');
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 1);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createOnInitComponent(
|
2018-08-21 03:03:21 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number, vars: number = 0,
|
|
|
|
directives: any[] = []) {
|
2017-12-20 19:26:07 -05:00
|
|
|
return class Component {
|
|
|
|
val: string = '';
|
2018-08-16 21:53:21 -04:00
|
|
|
ngOnInit() {
|
|
|
|
if (!this.val) this.val = '';
|
|
|
|
events.push(`${name}${this.val}`);
|
|
|
|
}
|
2017-12-20 19:26:07 -05:00
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-21 03:03:21 -04:00
|
|
|
vars: vars,
|
2017-12-20 19:26:07 -05:00
|
|
|
factory: () => new Component(),
|
2018-03-26 00:32:39 -04:00
|
|
|
inputs: {val: 'val'}, template,
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: directives
|
2017-12-20 19:26:07 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:08:18 -05:00
|
|
|
class Directive {
|
|
|
|
ngOnInit() { events.push('dir'); }
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
static ngDirectiveDef = defineDirective(
|
2018-03-29 19:41:45 -04:00
|
|
|
{type: Directive, selectors: [['', 'dir', '']], factory: () => new Directive()});
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
|
|
|
|
2018-10-31 01:10:23 -04:00
|
|
|
const directives = [Comp, Parent, ProjectedComp, Directive, NgIf];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
it('should call onInit method after inputs are set in creation mode (and not in update mode)',
|
|
|
|
() => {
|
|
|
|
/** <comp [val]="val"></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(ctx.val));
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 1, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(['comp']);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.val = '2';
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(['comp']);
|
2017-12-20 19:26:07 -05:00
|
|
|
});
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
it('should be called on root component in creation mode', () => {
|
2018-03-06 14:58:08 -05:00
|
|
|
const comp = renderComponent(Comp, {hostFeatures: [LifecycleHooksFeature]});
|
2018-03-06 13:13:49 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
|
|
|
markDirty(comp);
|
|
|
|
requestAnimationFrame.flush();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
it('should call parent onInit before child onInit', () => {
|
|
|
|
/**
|
|
|
|
* <parent></parent>
|
|
|
|
* parent temp: <comp></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['parent', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call all parent onInits across view before calling children onInits', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* <parent [val]="2"></parent>
|
|
|
|
*
|
|
|
|
* parent temp: <comp [val]="val"></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
|
|
|
element(1, 'parent');
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['parent1', 'parent2', 'comp1', 'comp2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call onInit every time a new view is created (if block)', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2017-12-20 19:26:07 -05:00
|
|
|
* <comp></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = false;
|
|
|
|
fixture.update();
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
});
|
|
|
|
|
2018-10-31 01:10:23 -04:00
|
|
|
|
|
|
|
it('should call onInit every time a new view is created (ngIf)', () => {
|
|
|
|
|
|
|
|
function IfTemplate(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
element(0, 'comp');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** <comp *ngIf="showing"></comp> */
|
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-12-12 18:23:12 -05:00
|
|
|
template(0, IfTemplate, 1, 0, 'comp', ['ngIf', '']);
|
2018-10-31 01:10:23 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'ngIf', bind(ctx.showing));
|
|
|
|
}
|
|
|
|
}, 1, 0, directives);
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
|
|
|
|
fixture.component.showing = true;
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
|
|
|
fixture.component.showing = false;
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
|
|
|
fixture.component.showing = true;
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onInit for children of dynamically created components', () => {
|
|
|
|
let viewContainerComp !: ViewContainerComp;
|
|
|
|
|
|
|
|
class ViewContainerComp {
|
|
|
|
constructor(public vcr: ViewContainerRef, public cfr: ComponentFactoryResolver) {}
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: ViewContainerComp,
|
|
|
|
selectors: [['view-container-comp']],
|
|
|
|
factory: () => viewContainerComp = new ViewContainerComp(
|
|
|
|
directiveInject(ViewContainerRef as any), injectComponentFactoryResolver()),
|
|
|
|
consts: 0,
|
|
|
|
vars: 0,
|
|
|
|
template: (rf: RenderFlags, ctx: ViewContainerComp) => {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const DynamicComp = createComponent('dynamic-comp', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
element(0, 'comp');
|
|
|
|
}
|
|
|
|
}, 1, 0, [Comp]);
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(ViewContainerComp);
|
|
|
|
expect(events).toEqual([]);
|
|
|
|
|
|
|
|
viewContainerComp.vcr.createComponent(
|
|
|
|
viewContainerComp.cfr.resolveComponentFactory(DynamicComp));
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:13:23 -05:00
|
|
|
it('should call onInit in hosts before their content children', () => {
|
|
|
|
/**
|
|
|
|
* <comp>
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ elementStart(1, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, directives);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:13:23 -05:00
|
|
|
expect(events).toEqual(['comp', 'projected']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onInit in host and its content children before next host', () => {
|
|
|
|
/**
|
|
|
|
* <comp [val]="1">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="1"></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
* <comp [val]="2">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="1"></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ elementStart(1, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(2, 'comp');
|
|
|
|
{ elementStart(3, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 2);
|
|
|
|
elementProperty(3, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 4, 0, directives);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:13:23 -05:00
|
|
|
expect(events).toEqual(['comp1', 'projected1', 'comp2', 'projected2']);
|
|
|
|
});
|
|
|
|
|
2018-01-22 20:43:52 -05:00
|
|
|
it('should be called on directives after component', () => {
|
2018-02-21 09:08:18 -05:00
|
|
|
/** <comp directive></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp', ['dir', '']);
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-01-22 20:43:52 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-22 20:43:52 -05:00
|
|
|
expect(events).toEqual(['comp', 'dir']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-01-22 20:43:52 -05:00
|
|
|
expect(events).toEqual(['comp', 'dir']);
|
|
|
|
});
|
|
|
|
|
2018-02-21 09:08:18 -05:00
|
|
|
it('should be called on directives on an element', () => {
|
|
|
|
/** <div directive></div> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'div', ['dir', '']);
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-02-21 09:08:18 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['dir']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['dir']);
|
|
|
|
});
|
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
it('should call onInit properly in for loop', () => {
|
|
|
|
/**
|
|
|
|
* <comp [val]="1"></comp>
|
|
|
|
* % for (let j = 2; j < 5; j++) {
|
|
|
|
* <comp [val]="j"></comp>
|
|
|
|
* % }
|
|
|
|
* <comp [val]="5"></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'comp');
|
2018-02-06 19:11:20 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 5);
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let j = 2; j < 5; j++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', j);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
// onInit is called top to bottom, so top level comps (1 and 5) are called
|
|
|
|
// before the comps inside the for loop's embedded view (2, 3, and 4)
|
|
|
|
expect(events).toEqual(['comp1', 'comp5', 'comp2', 'comp3', 'comp4']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onInit properly in for loop with children', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* % for (let j = 2; j < 5; j++) {
|
|
|
|
* <parent [val]="j"></parent>
|
|
|
|
* % }
|
|
|
|
* <parent [val]="5"></parent>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'parent');
|
2018-02-06 19:11:20 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 5);
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let j = 2; j < 5; j++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', j);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
// onInit is called top to bottom, so top level comps (1 and 5) are called
|
|
|
|
// before the comps inside the for loop's embedded view (2, 3, and 4)
|
|
|
|
expect(events).toEqual([
|
|
|
|
'parent1', 'parent5', 'parent2', 'comp2', 'parent3', 'comp3', 'parent4', 'comp4', 'comp1',
|
|
|
|
'comp5'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('doCheck', () => {
|
|
|
|
let events: string[];
|
|
|
|
let allEvents: string[];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
events = [];
|
|
|
|
allEvents = [];
|
|
|
|
});
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Comp = createDoCheckComponent('comp', (rf: RenderFlags, ctx: any) => {});
|
2018-08-18 14:14:50 -04:00
|
|
|
let Parent = createDoCheckComponent('parent', getParentTemplate('comp'), 1, 1, [Comp]);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createDoCheckComponent(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives: any[] = []) {
|
2017-12-20 19:26:07 -05:00
|
|
|
return class Component {
|
|
|
|
ngDoCheck() {
|
|
|
|
events.push(name);
|
2018-01-22 20:43:52 -05:00
|
|
|
allEvents.push('check ' + name);
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 20:43:52 -05:00
|
|
|
ngOnInit() { allEvents.push('init ' + name); }
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-03-26 00:32:39 -04:00
|
|
|
factory: () => new Component(), template,
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2019-02-05 00:42:55 -05:00
|
|
|
directives: directives,
|
|
|
|
inputs: {val: 'val'}
|
2018-03-26 00:32:39 -04:00
|
|
|
});
|
2017-12-20 19:26:07 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:08:18 -05:00
|
|
|
class Directive {
|
|
|
|
ngDoCheck() { events.push('dir'); }
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
static ngDirectiveDef = defineDirective(
|
2018-03-29 19:41:45 -04:00
|
|
|
{type: Directive, selectors: [['', 'dir', '']], factory: () => new Directive()});
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const directives = [Comp, Parent, Directive];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
it('should call doCheck on every refresh', () => {
|
|
|
|
/** <comp></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
});
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
it('should be called on root component', () => {
|
2018-03-06 14:58:08 -05:00
|
|
|
const comp = renderComponent(Comp, {hostFeatures: [LifecycleHooksFeature]});
|
2018-03-06 13:13:49 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
|
|
|
markDirty(comp);
|
|
|
|
requestAnimationFrame.flush();
|
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
});
|
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
it('should call parent doCheck before child doCheck', () => {
|
|
|
|
/**
|
|
|
|
* <parent></parent>
|
|
|
|
* parent temp: <comp></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 19:26:07 -05:00
|
|
|
expect(events).toEqual(['parent', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call ngOnInit before ngDoCheck if creation mode', () => {
|
|
|
|
/** <comp></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-22 20:43:52 -05:00
|
|
|
expect(allEvents).toEqual(['init comp', 'check comp']);
|
2017-12-20 19:26:07 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-01-22 20:43:52 -05:00
|
|
|
expect(allEvents).toEqual(['init comp', 'check comp', 'check comp']);
|
2017-12-20 19:26:07 -05:00
|
|
|
});
|
|
|
|
|
2018-02-21 09:08:18 -05:00
|
|
|
it('should be called on directives after component', () => {
|
|
|
|
/** <comp directive></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp', ['dir', '']);
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-02-21 09:08:18 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['comp', 'dir']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['comp', 'dir', 'comp', 'dir']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called on directives on an element', () => {
|
|
|
|
/** <div directive></div> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'div', ['dir', '']);
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-02-21 09:08:18 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['dir']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['dir', 'dir']);
|
|
|
|
});
|
|
|
|
|
2017-12-20 19:26:07 -05:00
|
|
|
});
|
|
|
|
|
2018-01-18 17:18:21 -05:00
|
|
|
describe('afterContentInit', () => {
|
|
|
|
let events: string[];
|
|
|
|
let allEvents: string[];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
events = [];
|
|
|
|
allEvents = [];
|
|
|
|
});
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Comp = createAfterContentInitComp('comp', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
projection(0);
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 1);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Parent = createAfterContentInitComp('parent', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ projection(1); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2018-07-03 23:04:36 -04:00
|
|
|
elementProperty(0, 'val', bind(ctx.val));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 1, [Comp]);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let ProjectedComp = createAfterContentInitComp('projected', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
projection(0);
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 1);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createAfterContentInitComp(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives: any[] = []) {
|
2018-01-18 17:18:21 -05:00
|
|
|
return class Component {
|
|
|
|
val: string = '';
|
|
|
|
ngAfterContentInit() {
|
|
|
|
events.push(`${name}${this.val}`);
|
|
|
|
allEvents.push(`${name}${this.val} init`);
|
|
|
|
}
|
|
|
|
ngAfterContentChecked() { allEvents.push(`${name}${this.val} check`); }
|
|
|
|
|
2018-01-22 18:27:21 -05:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-01-22 18:27:21 -05:00
|
|
|
factory: () => new Component(),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2018-01-22 18:27:21 -05:00
|
|
|
inputs: {val: 'val'},
|
|
|
|
template: template,
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: directives
|
2018-01-22 18:27:21 -05:00
|
|
|
});
|
2018-01-18 17:18:21 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
class Directive {
|
|
|
|
ngAfterContentInit() { events.push('init'); }
|
|
|
|
ngAfterContentChecked() { events.push('check'); }
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective(
|
2018-03-29 19:41:45 -04:00
|
|
|
{type: Directive, selectors: [['', 'dir', '']], factory: () => new Directive()});
|
2018-03-26 00:32:39 -04:00
|
|
|
}
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
function ForLoopWithChildrenTemplate(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'parent');
|
|
|
|
{ text(1, 'content'); }
|
|
|
|
elementEnd();
|
|
|
|
container(2);
|
|
|
|
elementStart(3, 'parent');
|
|
|
|
{ text(4, 'content'); }
|
|
|
|
elementEnd();
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(3, 'val', 4);
|
|
|
|
containerRefreshStart(2);
|
|
|
|
{
|
|
|
|
for (let i = 2; i < 4; i++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 2, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'parent');
|
|
|
|
{ text(1, 'content'); }
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', i);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const directives = [Comp, Parent, ProjectedComp, Directive];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2018-01-18 17:18:21 -05:00
|
|
|
it('should be called only in creation mode', () => {
|
|
|
|
/** <comp>content</comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(1, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
it('should be called on root component in creation mode', () => {
|
2018-03-06 14:58:08 -05:00
|
|
|
const comp = renderComponent(Comp, {hostFeatures: [LifecycleHooksFeature]});
|
2018-03-06 13:13:49 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
|
|
|
markDirty(comp);
|
|
|
|
requestAnimationFrame.flush();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:18:21 -05:00
|
|
|
it('should be called on every init (if blocks)', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2018-01-18 17:18:21 -05:00
|
|
|
* <comp>content</comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 2, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ text(1, 'content'); }
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = false;
|
|
|
|
fixture.update();
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in parents before children', () => {
|
|
|
|
/**
|
|
|
|
* <parent>content</parent>
|
|
|
|
*
|
|
|
|
* parent template: <comp><ng-content></ng-content></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(1, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['parent', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called breadth-first in entire parent subtree before any children', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1">content</parent>
|
|
|
|
* <parent [val]="2">content</parent>
|
|
|
|
*
|
|
|
|
* parent template: <comp [val]="val"><ng-content></ng-content></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(1, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(2, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(3, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 4, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['parent1', 'parent2', 'comp1', 'comp2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in projected components before their hosts', () => {
|
|
|
|
/**
|
|
|
|
* <parent>
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected>content</projected>
|
2018-01-18 17:18:21 -05:00
|
|
|
* </parent>
|
|
|
|
*
|
|
|
|
* parent template:
|
|
|
|
* <comp><ng-content></ng-content></comp>
|
|
|
|
*
|
|
|
|
* projected comp: <ng-content></ng-content>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'parent');
|
2018-01-18 17:18:21 -05:00
|
|
|
{
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(1, 'projected');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(2, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['projected', 'parent', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in projected components and hosts before children', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="1">content</projected>
|
2018-01-18 17:18:21 -05:00
|
|
|
* </parent>
|
|
|
|
* * <parent [val]="2">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="2">content</projected>
|
2018-01-18 17:18:21 -05:00
|
|
|
* </parent>
|
|
|
|
*
|
|
|
|
* parent template:
|
|
|
|
* <comp [val]="val"><ng-content></ng-content></comp>
|
|
|
|
*
|
|
|
|
* projected comp: <ng-content></ng-content>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'parent');
|
2018-01-18 17:18:21 -05:00
|
|
|
{
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(1, 'projected');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(2, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(3, 'parent');
|
2018-01-18 17:18:21 -05:00
|
|
|
{
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(4, 'projected');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(5, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 1);
|
|
|
|
elementProperty(3, 'val', 2);
|
|
|
|
elementProperty(4, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 6, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['projected1', 'parent1', 'projected2', 'parent2', 'comp1', 'comp2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in correct order in a for loop', () => {
|
|
|
|
/**
|
|
|
|
* <comp [val]="1">content</comp>
|
|
|
|
* % for(let i = 2; i < 4; i++) {
|
|
|
|
* <comp [val]="i">content</comp>
|
|
|
|
* % }
|
|
|
|
* <comp [val]="4">content</comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(1, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-03-21 18:10:34 -04:00
|
|
|
container(2);
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(3, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(4, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(3, 'val', 4);
|
|
|
|
containerRefreshStart(2);
|
|
|
|
{
|
|
|
|
for (let i = 2; i < 4; i++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 2, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ text(1, 'content'); }
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', i);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 5, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(events).toEqual(['comp2', 'comp3', 'comp1', 'comp4']);
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:22:52 -05:00
|
|
|
it('should be called in correct order in a for loop with children', () => {
|
2018-01-18 17:18:21 -05:00
|
|
|
/**
|
|
|
|
* <parent [val]="1">content</parent>
|
|
|
|
* % for(let i = 2; i < 4; i++) {
|
|
|
|
* <parent [val]="i">content</parent>
|
|
|
|
* % }
|
|
|
|
* <parent [val]="4">content</parent>
|
|
|
|
*/
|
|
|
|
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(ForLoopWithChildrenTemplate, {}, 5, 0, directives);
|
2018-01-18 17:22:52 -05:00
|
|
|
expect(events).toEqual(
|
|
|
|
['parent2', 'comp2', 'parent3', 'comp3', 'parent1', 'parent4', 'comp1', 'comp4']);
|
2018-01-18 17:18:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ngAfterContentChecked', () => {
|
|
|
|
|
|
|
|
it('should be called every change detection run after afterContentInit', () => {
|
|
|
|
/** <comp>content</comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
{ text(1, 'content'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:18:21 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, directives);
|
2018-01-18 17:18:21 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-01-18 17:18:21 -05:00
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check', 'comp check']);
|
|
|
|
});
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
it('should be called on root component', () => {
|
2018-03-06 14:58:08 -05:00
|
|
|
const comp = renderComponent(Comp, {hostFeatures: [LifecycleHooksFeature]});
|
2018-03-06 13:13:49 -05:00
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check']);
|
|
|
|
|
|
|
|
markDirty(comp);
|
|
|
|
requestAnimationFrame.flush();
|
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check', 'comp check']);
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:18:21 -05:00
|
|
|
});
|
2018-02-22 05:58:29 -05:00
|
|
|
|
|
|
|
describe('directives', () => {
|
|
|
|
it('should be called on directives after component', () => {
|
|
|
|
/** <comp directive></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp', ['dir', '']);
|
2018-02-22 05:58:29 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-02-22 05:58:29 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-22 05:58:29 -05:00
|
|
|
expect(events).toEqual(['comp', 'init', 'check']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called on directives on an element', () => {
|
|
|
|
/** <div directive></div> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'div', ['dir', '']);
|
2018-02-22 05:58:29 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, directives);
|
2018-02-22 05:58:29 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-22 05:58:29 -05:00
|
|
|
expect(events).toEqual(['init', 'check']);
|
|
|
|
});
|
|
|
|
});
|
2018-01-18 17:18:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('afterViewInit', () => {
|
2017-12-22 19:41:34 -05:00
|
|
|
let events: string[];
|
|
|
|
let allEvents: string[];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
events = [];
|
|
|
|
allEvents = [];
|
|
|
|
});
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Comp = createAfterViewInitComponent('comp', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
elementStart(0, 'div');
|
|
|
|
{ projection(1); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 2);
|
2018-08-18 14:14:50 -04:00
|
|
|
let Parent = createAfterViewInitComponent('parent', getParentTemplate('comp'), 1, 1, [Comp]);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let ProjectedComp = createAfterViewInitComponent('projected', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0, 'content');
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 1);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createAfterViewInitComponent(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number, vars: number = 0,
|
|
|
|
directives: any[] = []) {
|
2017-12-22 19:41:34 -05:00
|
|
|
return class Component {
|
|
|
|
val: string = '';
|
|
|
|
ngAfterViewInit() {
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!this.val) this.val = '';
|
2017-12-22 19:41:34 -05:00
|
|
|
events.push(`${name}${this.val}`);
|
|
|
|
allEvents.push(`${name}${this.val} init`);
|
|
|
|
}
|
|
|
|
ngAfterViewChecked() { allEvents.push(`${name}${this.val} check`); }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2017-12-22 19:41:34 -05:00
|
|
|
factory: () => new Component(),
|
|
|
|
inputs: {val: 'val'},
|
2018-03-26 00:32:39 -04:00
|
|
|
template: template,
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: directives
|
2017-12-22 19:41:34 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
class Directive {
|
|
|
|
ngAfterViewInit() { events.push('init'); }
|
|
|
|
ngAfterViewChecked() { events.push('check'); }
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective(
|
2018-03-29 19:41:45 -04:00
|
|
|
{type: Directive, selectors: [['', 'dir', '']], factory: () => new Directive()});
|
2018-03-26 00:32:39 -04:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const defs = [Comp, Parent, ProjectedComp, Directive];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2017-12-22 19:41:34 -05:00
|
|
|
it('should be called on init and not in update mode', () => {
|
|
|
|
/** <comp></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
it('should be called on root component in creation mode', () => {
|
2018-03-06 14:58:08 -05:00
|
|
|
const comp = renderComponent(Comp, {hostFeatures: [LifecycleHooksFeature]});
|
2018-03-06 13:13:49 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
|
|
|
markDirty(comp);
|
|
|
|
requestAnimationFrame.flush();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
2017-12-22 19:41:34 -05:00
|
|
|
it('should be called every time a view is initialized (if block)', () => {
|
|
|
|
/*
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2017-12-22 19:41:34 -05:00
|
|
|
* <comp></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = false;
|
|
|
|
fixture.update();
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in children before parents', () => {
|
|
|
|
/**
|
|
|
|
* <parent></parent>
|
|
|
|
*
|
|
|
|
* parent temp: <comp></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp', 'parent']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called for entire subtree before being called in any parent view comps', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* <parent [val]="2"></parent>
|
|
|
|
*
|
|
|
|
* parent temp: <comp [val]="val"></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
|
|
|
element(1, 'parent');
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, defs);
|
2018-08-16 21:53:21 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp1', 'comp2', 'parent1', 'parent2']);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:13:23 -05:00
|
|
|
it('should be called in projected components before their hosts', () => {
|
|
|
|
/**
|
|
|
|
* <comp>
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(1, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, defs);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:13:23 -05:00
|
|
|
expect(events).toEqual(['projected', 'comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call afterViewInit in content children and host before next host', () => {
|
|
|
|
/**
|
|
|
|
* <comp [val]="1">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="1"></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
* <comp [val]="2">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="2"></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(1, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(2, 'comp');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(3, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 2);
|
|
|
|
elementProperty(3, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 4, 0, defs);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:13:23 -05:00
|
|
|
expect(events).toEqual(['projected1', 'comp1', 'projected2', 'comp2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call afterViewInit in content children and hosts before parents', () => {
|
|
|
|
/*
|
|
|
|
* <comp [val]="val">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="val"></projected>
|
2018-01-18 17:13:23 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
const ParentComp = createAfterViewInitComponent('parent', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(1, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(ctx.val));
|
|
|
|
elementProperty(1, 'val', bind(ctx.val));
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 2, [Comp, ProjectedComp]);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* <parent [val]="2"></parent>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
|
|
|
element(1, 'parent');
|
2018-01-18 17:13:23 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, [ParentComp]);
|
2018-01-18 17:13:23 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:13:23 -05:00
|
|
|
expect(events).toEqual(['projected1', 'comp1', 'projected2', 'comp2', 'parent1', 'parent2']);
|
|
|
|
});
|
|
|
|
|
2017-12-22 19:41:34 -05:00
|
|
|
it('should be called in correct order with for loops', () => {
|
|
|
|
/**
|
|
|
|
* <comp [val]="1"></comp>
|
|
|
|
* % for (let i = 0; i < 4; i++) {
|
|
|
|
* <comp [val]="i"></comp>
|
|
|
|
* % }
|
|
|
|
* <comp [val]="4"></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'comp');
|
2018-02-06 19:11:20 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 4);
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let i = 2; i < 4; i++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', i);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(['comp2', 'comp3', 'comp1', 'comp4']);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in correct order with for loops with children', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* % for(let i = 0; i < 4; i++) {
|
|
|
|
* <parent [val]="i"></parent>
|
|
|
|
* % }
|
|
|
|
* <parent [val]="4"></parent>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'parent');
|
2018-02-06 19:11:20 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 4);
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let i = 2; i < 4; i++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', i);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(events).toEqual(
|
|
|
|
['comp2', 'parent2', 'comp3', 'parent3', 'comp1', 'comp4', 'parent1', 'parent4']);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ngAfterViewChecked', () => {
|
|
|
|
|
|
|
|
it('should call ngAfterViewChecked every update', () => {
|
|
|
|
/** <comp></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check']);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check', 'comp check']);
|
|
|
|
});
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
it('should be called on root component', () => {
|
2018-03-06 14:58:08 -05:00
|
|
|
const comp = renderComponent(Comp, {hostFeatures: [LifecycleHooksFeature]});
|
2018-03-06 13:13:49 -05:00
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check']);
|
|
|
|
|
|
|
|
markDirty(comp);
|
|
|
|
requestAnimationFrame.flush();
|
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check', 'comp check']);
|
|
|
|
});
|
|
|
|
|
2017-12-22 19:41:34 -05:00
|
|
|
it('should call ngAfterViewChecked with bindings', () => {
|
|
|
|
/** <comp [val]="myVal"></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(ctx.myVal));
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 1, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check']);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.myVal = 2;
|
|
|
|
fixture.update();
|
|
|
|
expect(allEvents).toEqual(['comp init', 'comp check', 'comp2 check']);
|
2017-12-22 19:41:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in correct order with for loops with children', () => {
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* % for(let i = 0; i < 4; i++) {
|
|
|
|
* <parent [val]="i"></parent>
|
|
|
|
* % }
|
|
|
|
* <parent [val]="4"></parent>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'parent');
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 4);
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let i = 2; i < 4; i++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', i);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-22 19:41:34 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 0, defs);
|
2017-12-22 19:41:34 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-22 19:41:34 -05:00
|
|
|
expect(allEvents).toEqual([
|
|
|
|
'comp2 init', 'comp2 check', 'parent2 init', 'parent2 check', 'comp3 init', 'comp3 check',
|
|
|
|
'parent3 init', 'parent3 check', 'comp1 init', 'comp1 check', 'comp4 init', 'comp4 check',
|
|
|
|
'parent1 init', 'parent1 check', 'parent4 init', 'parent4 check'
|
|
|
|
]);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-22 05:58:29 -05:00
|
|
|
describe('directives', () => {
|
|
|
|
it('should be called on directives after component', () => {
|
|
|
|
/** <comp directive></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp', ['dir', '']);
|
2018-02-22 05:58:29 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-02-22 05:58:29 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-22 05:58:29 -05:00
|
|
|
expect(events).toEqual(['comp', 'init', 'check']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called on directives on an element', () => {
|
|
|
|
/** <div directive></div> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'div', ['dir', '']);
|
2018-02-22 05:58:29 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-02-22 05:58:29 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-22 05:58:29 -05:00
|
|
|
expect(events).toEqual(['init', 'check']);
|
|
|
|
});
|
|
|
|
});
|
2017-12-22 19:41:34 -05:00
|
|
|
});
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
describe('onDestroy', () => {
|
|
|
|
let events: string[];
|
|
|
|
|
|
|
|
beforeEach(() => { events = []; });
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Comp = createOnDestroyComponent('comp', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
projection(0);
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 1);
|
2018-08-18 14:14:50 -04:00
|
|
|
let Parent = createOnDestroyComponent('parent', getParentTemplate('comp'), 1, 1, [Comp]);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createOnDestroyComponent(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives: any[] = []) {
|
2017-12-01 17:23:03 -05:00
|
|
|
return class Component {
|
|
|
|
val: string = '';
|
|
|
|
ngOnDestroy() { events.push(`${name}${this.val}`); }
|
|
|
|
|
2018-01-22 18:27:21 -05:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-01-22 18:27:21 -05:00
|
|
|
factory: () => new Component(),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2018-01-22 18:27:21 -05:00
|
|
|
inputs: {val: 'val'},
|
2018-03-26 00:32:39 -04:00
|
|
|
template: template,
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: directives
|
2018-01-22 18:27:21 -05:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
let Grandparent = createOnDestroyComponent('grandparent', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-03-26 00:32:39 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, [Parent]);
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
const ProjectedComp = createOnDestroyComponent('projected', (rf: RenderFlags, ctx: any) => {});
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2018-02-21 09:08:18 -05:00
|
|
|
class Directive {
|
|
|
|
ngOnDestroy() { events.push('dir'); }
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
static ngDirectiveDef = defineDirective(
|
2018-03-29 19:41:45 -04:00
|
|
|
{type: Directive, selectors: [['', 'dir', '']], factory: () => new Directive()});
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const defs = [Comp, Parent, Grandparent, ProjectedComp, Directive];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
it('should call destroy when view is removed', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2017-12-01 17:23:03 -05:00
|
|
|
* <comp></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-08-16 21:53:21 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call destroy when multiple views are removed', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2017-12-01 17:23:03 -05:00
|
|
|
* <comp [val]="1"></comp>
|
|
|
|
* <comp [val]="2"></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 2, 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
|
|
|
element(1, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind('1'));
|
|
|
|
elementProperty(1, 'val', bind('2'));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-08-16 21:53:21 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp1', 'comp2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in child components before parent components', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2017-12-01 17:23:03 -05:00
|
|
|
* <parent></parent>
|
|
|
|
* % }
|
|
|
|
*
|
|
|
|
* parent template: <comp></comp>
|
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
|
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp', 'parent']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called bottom up with children nested 2 levels deep', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2017-12-01 17:23:03 -05:00
|
|
|
* <grandparent></grandparent>
|
|
|
|
* % }
|
|
|
|
*
|
|
|
|
* grandparent template: <parent></parent>
|
|
|
|
* parent template: <comp></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'grandparent');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
|
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp', 'parent', 'grandparent']);
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:22:52 -05:00
|
|
|
it('should be called in projected components before their hosts', () => {
|
|
|
|
/**
|
2018-08-16 21:53:21 -04:00
|
|
|
* % if (!skip) {
|
2018-01-18 17:22:52 -05:00
|
|
|
* <comp [val]="1">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="1"></projected>
|
2018-01-18 17:22:52 -05:00
|
|
|
* </comp>
|
|
|
|
* <comp [val]="2">
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected [val]="2"></projected>
|
2018-01-18 17:22:52 -05:00
|
|
|
* </comp>
|
|
|
|
* }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 4, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'comp');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(1, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-04-10 23:57:09 -04:00
|
|
|
elementStart(2, 'comp');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(3, 'projected'); }
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 1);
|
|
|
|
elementProperty(2, 'val', 2);
|
|
|
|
elementProperty(3, 'val', 2);
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-01-18 17:22:52 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:22:52 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2018-01-18 17:22:52 -05:00
|
|
|
expect(events).toEqual(['projected1', 'comp1', 'projected2', 'comp2']);
|
|
|
|
});
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
it('should be called in consistent order if views are removed and re-added', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <comp [val]="1"></comp>
|
|
|
|
* % if (condition2) {
|
|
|
|
* <comp [val]="2"></comp>
|
|
|
|
* % }
|
|
|
|
* <comp [val]="3"></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 3, 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind('1'));
|
|
|
|
elementProperty(2, 'val', bind('3'));
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
if (ctx.condition2) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf2 = embeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf2 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf2 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind('2'));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.component.condition2 = true;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
// remove all views
|
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current angular will process in this same order (root is the top-level removed view):
|
|
|
|
*
|
|
|
|
* root.child (comp1 view) onDestroy: null
|
|
|
|
* root.child.next (container) -> embeddedView
|
|
|
|
* embeddedView.child (comp2 view) onDestroy: null
|
|
|
|
* embeddedView onDestroy: [comp2]
|
|
|
|
* root.child.next.next (comp3 view) onDestroy: null
|
|
|
|
* root onDestroy: [comp1, comp3]
|
|
|
|
*/
|
|
|
|
expect(events).toEqual(['comp2', 'comp1', 'comp3']);
|
|
|
|
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
// remove inner view
|
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.component.condition2 = false;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
// remove outer view
|
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp1', 'comp3']);
|
|
|
|
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
// restore both views
|
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.component.condition2 = true;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
// remove both views
|
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp2', 'comp1', 'comp3']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called in every iteration of a destroyed for loop', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <comp [val]="1"></comp>
|
|
|
|
* % for (let i = 2; i < len; i++) {
|
|
|
|
* <comp [val]="i"></comp>
|
|
|
|
* % }
|
|
|
|
* <comp [val]="5"></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 3, 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind('1'));
|
|
|
|
elementProperty(2, 'val', bind('5'));
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let j = 2; j < ctx.len; j++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf2 = embeddedViewStart(0, 1, 1);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf2 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf2 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(j));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-08-16 21:53:21 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.component.len = 5;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current angular will process in this same order (root is the top-level removed view):
|
|
|
|
*
|
|
|
|
* root.child (comp1 view) onDestroy: null
|
|
|
|
* root.child.next (container) -> embeddedView (children[0].data)
|
|
|
|
* embeddedView.child (comp2 view) onDestroy: null
|
|
|
|
* embeddedView onDestroy: [comp2]
|
|
|
|
* embeddedView.next.child (comp3 view) onDestroy: null
|
|
|
|
* embeddedView.next onDestroy: [comp3]
|
|
|
|
* embeddedView.next.next.child (comp4 view) onDestroy: null
|
|
|
|
* embeddedView.next.next onDestroy: [comp4]
|
|
|
|
* embeddedView.next.next -> container -> root
|
|
|
|
* root onDestroy: [comp1, comp5]
|
|
|
|
*/
|
|
|
|
expect(events).toEqual(['comp2', 'comp3', 'comp4', 'comp1', 'comp5']);
|
|
|
|
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.component.len = 4;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp2', 'comp3', 'comp1', 'comp5']);
|
|
|
|
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.component.len = 5;
|
|
|
|
fixture.update();
|
|
|
|
|
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(events).toEqual(['comp2', 'comp3', 'comp4', 'comp1', 'comp5']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call destroy properly if view also has listeners', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <button (click)="onClick()">Click me</button>
|
|
|
|
* <comp></comp>
|
|
|
|
* <button (click)="onClick()">Click me</button>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 5, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'button');
|
|
|
|
{
|
|
|
|
listener('click', ctx.onClick.bind(ctx));
|
|
|
|
text(1, 'Click me');
|
|
|
|
}
|
|
|
|
elementEnd();
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
elementStart(3, 'button');
|
|
|
|
{
|
|
|
|
listener('click', ctx.onClick.bind(ctx));
|
|
|
|
text(4, 'Click me');
|
|
|
|
}
|
|
|
|
elementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
embeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class App {
|
|
|
|
counter = 0;
|
|
|
|
condition = true;
|
|
|
|
onClick() { this.counter++; }
|
|
|
|
}
|
|
|
|
|
|
|
|
const ctx: {counter: number} = new App();
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, defs);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
const buttons = containerEl.querySelectorAll('button') !;
|
|
|
|
buttons[0].click();
|
|
|
|
expect(ctx.counter).toEqual(1);
|
|
|
|
buttons[1].click();
|
|
|
|
expect(ctx.counter).toEqual(2);
|
|
|
|
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, {condition: false}, 1, 0, defs);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttons[0].click();
|
|
|
|
buttons[1].click();
|
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
expect(ctx.counter).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2018-02-21 09:08:18 -05:00
|
|
|
it('should be called on directives after component', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <comp></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-21 09:08:18 -05:00
|
|
|
container(0);
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp', ['dir', '']);
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-02-21 09:08:18 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual([]);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['comp', 'dir']);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called on directives on an element', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <div directive></div>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-21 09:08:18 -05:00
|
|
|
container(0);
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'div', ['dir', '']);
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-02-21 09:08:18 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-02-21 09:08:18 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual([]);
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2018-02-21 09:08:18 -05:00
|
|
|
expect(events).toEqual(['dir']);
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-21 12:12:02 -05:00
|
|
|
describe('onChanges', () => {
|
2018-12-20 20:23:25 -05:00
|
|
|
let events: ({type: string, name: string, [key: string]: any})[];
|
2018-02-21 12:12:02 -05:00
|
|
|
|
|
|
|
beforeEach(() => { events = []; });
|
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
/**
|
|
|
|
* <div>
|
|
|
|
* <ng-content/>
|
|
|
|
* </div>
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
const Comp = createOnChangesComponent('comp', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
elementStart(0, 'div');
|
|
|
|
{ projection(1); }
|
2018-02-21 12:12:02 -05:00
|
|
|
elementEnd();
|
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 2);
|
2018-12-20 20:23:25 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <comp [val1]="a" [publicVal2]="b"/>
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
const Parent = createOnChangesComponent('parent', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(ctx.a));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(ctx.b));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 2, [Comp]);
|
2018-12-20 20:23:25 -05:00
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
const ProjectedComp = createOnChangesComponent('projected', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-21 12:12:02 -05:00
|
|
|
text(0, 'content');
|
|
|
|
}
|
2018-08-16 21:53:21 -04:00
|
|
|
}, 1);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createOnChangesComponent(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives: any[] = []) {
|
2018-02-21 12:12:02 -05:00
|
|
|
return class Component {
|
|
|
|
// @Input() val1: string;
|
2018-12-20 20:23:25 -05:00
|
|
|
// @Input('publicVal2') val2: string;
|
2018-02-21 12:12:02 -05:00
|
|
|
a: string = 'wasVal1BeforeMinification';
|
|
|
|
b: string = 'wasVal2BeforeMinification';
|
2018-12-20 20:23:25 -05:00
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
if (changes.a && this.a !== changes.a.currentValue) {
|
|
|
|
throw Error(
|
|
|
|
`SimpleChanges invalid expected this.a ${this.a} to equal currentValue ${changes.a.currentValue}`);
|
|
|
|
}
|
|
|
|
if (changes.b && this.b !== changes.b.currentValue) {
|
|
|
|
throw Error(
|
|
|
|
`SimpleChanges invalid expected this.b ${this.b} to equal currentValue ${changes.b.currentValue}`);
|
|
|
|
}
|
|
|
|
events.push({type: 'onChanges', name: 'comp - ' + name, changes});
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-02-21 12:12:02 -05:00
|
|
|
factory: () => new Component(),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2018-12-20 20:23:25 -05:00
|
|
|
inputs: {a: 'val1', b: ['publicVal2', 'val2']}, template,
|
2019-01-16 12:35:35 -05:00
|
|
|
directives: directives,
|
2019-01-22 14:17:13 -05:00
|
|
|
features: [NgOnChangesFeature()],
|
2018-02-21 12:12:02 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Directive {
|
|
|
|
// @Input() val1: string;
|
2018-12-20 20:23:25 -05:00
|
|
|
// @Input('publicVal2') val2: string;
|
2018-02-21 12:12:02 -05:00
|
|
|
a: string = 'wasVal1BeforeMinification';
|
|
|
|
b: string = 'wasVal2BeforeMinification';
|
2018-12-20 20:23:25 -05:00
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
events.push({type: 'onChanges', name: 'dir - dir', changes});
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: Directive,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', 'dir', '']],
|
2018-02-21 12:12:02 -05:00
|
|
|
factory: () => new Directive(),
|
2019-01-16 12:35:35 -05:00
|
|
|
inputs: {a: 'val1', b: ['publicVal2', 'val2']},
|
2019-01-22 14:17:13 -05:00
|
|
|
features: [NgOnChangesFeature()],
|
2018-02-21 12:12:02 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const defs = [Comp, Parent, Directive, ProjectedComp];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2018-02-21 12:12:02 -05:00
|
|
|
it('should call onChanges method after inputs are set in creation and update mode', () => {
|
2018-12-20 20:23:25 -05:00
|
|
|
/** <comp [val1]="val1" [publicVal2]="val2"></comp> */
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(ctx.val1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(ctx.val2));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 2, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
// First changes happen here.
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-12-20 20:23:25 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
events = [];
|
|
|
|
fixture.component.val1 = '1';
|
|
|
|
fixture.component.val2 = 'a';
|
|
|
|
fixture.update();
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(
|
|
|
|
undefined, '1', false), // we cleared `events` above, this is the second change
|
|
|
|
'val2': new SimpleChange(undefined, 'a', false),
|
|
|
|
}
|
|
|
|
}]);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
events = [];
|
|
|
|
fixture.component.val1 = '2';
|
|
|
|
fixture.component.val2 = 'b';
|
|
|
|
fixture.update();
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange('1', '2', false),
|
|
|
|
'val2': new SimpleChange('a', 'b', false),
|
|
|
|
}
|
|
|
|
}]);
|
2018-02-21 12:12:02 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call parent onChanges before child onChanges', () => {
|
|
|
|
/**
|
|
|
|
* <parent></parent>
|
|
|
|
* parent temp: <comp></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(ctx.val1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(ctx.val2));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 2, defs);
|
2018-08-16 21:53:21 -04:00
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
2018-12-20 20:23:25 -05:00
|
|
|
|
|
|
|
// We're clearing events after the first change here
|
2018-08-16 21:53:21 -04:00
|
|
|
events = [];
|
|
|
|
fixture.component.val1 = '1';
|
|
|
|
fixture.component.val2 = 'a';
|
|
|
|
fixture.update();
|
2018-02-21 12:12:02 -05:00
|
|
|
|
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, '1', false),
|
|
|
|
'val2': new SimpleChange(undefined, 'a', false),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, '1', false),
|
|
|
|
'val2': new SimpleChange(undefined, 'a', false),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call all parent onChanges across view before calling children onChanges', () => {
|
|
|
|
/**
|
2018-12-20 20:23:25 -05:00
|
|
|
* <parent [val1]="1"></parent>
|
|
|
|
* <parent [val1]="2"></parent>
|
2018-02-21 12:12:02 -05:00
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
|
|
|
element(1, 'parent');
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(1, 'val1', bind(2));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(1, 'publicVal2', bind(2));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 4, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 12:12:02 -05:00
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call onChanges every time a new view is created (if block)', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <comp></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-21 12:12:02 -05:00
|
|
|
container(0);
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 0, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-12-20 20:23:25 -05:00
|
|
|
|
|
|
|
// Show the `comp` component, causing it to initialize. (first change is true)
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.update();
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
}]);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
// Hide the `comp` component, no onChanges should fire
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = false;
|
|
|
|
fixture.update();
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
}]);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
// Show the `comp` component, it initializes again. (first change is true)
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.component.condition = true;
|
|
|
|
fixture.update();
|
2018-02-21 12:12:02 -05:00
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onChanges in hosts before their content children', () => {
|
|
|
|
/**
|
|
|
|
* <comp>
|
2018-03-26 00:32:39 -04:00
|
|
|
* <projected></projected>
|
2018-02-21 12:12:02 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ elementStart(1, 'projected'); }
|
2018-02-21 12:12:02 -05:00
|
|
|
elementEnd();
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(1, 'val1', bind(2));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(1, 'publicVal2', bind(2));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 4, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 12:12:02 -05:00
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - projected',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onChanges in host and its content children before next host', () => {
|
|
|
|
/**
|
2018-12-20 20:23:25 -05:00
|
|
|
* <comp [val1]="1" [publicVal2]="1">
|
|
|
|
* <projected [val1]="2" [publicVal2]="2"></projected>
|
2018-02-21 12:12:02 -05:00
|
|
|
* </comp>
|
2018-12-20 20:23:25 -05:00
|
|
|
* <comp [val1]="3" [publicVal2]="3">
|
|
|
|
* <projected [val1]="4" [publicVal2]="4"></projected>
|
2018-02-21 12:12:02 -05:00
|
|
|
* </comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'comp');
|
|
|
|
{ elementStart(1, 'projected'); }
|
2018-02-21 12:12:02 -05:00
|
|
|
elementEnd();
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(2, 'comp');
|
|
|
|
{ elementStart(3, 'projected'); }
|
2018-02-21 12:12:02 -05:00
|
|
|
elementEnd();
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(1, 'val1', bind(2));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(1, 'publicVal2', bind(2));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(2, 'val1', bind(3));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(2, 'publicVal2', bind(3));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(3, 'val1', bind(4));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(3, 'publicVal2', bind(4));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 4, 8, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 12:12:02 -05:00
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - projected',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 3, true),
|
|
|
|
'val2': new SimpleChange(undefined, 3, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - projected',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 4, true),
|
|
|
|
'val2': new SimpleChange(undefined, 4, true),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called on directives after component', () => {
|
2018-12-20 20:23:25 -05:00
|
|
|
/**
|
|
|
|
* <comp dir [val1]="1" [publicVal2]="1"></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp', ['dir', '']);
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 2, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 12:12:02 -05:00
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'dir - dir',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
// Update causes no changes to be fired, since the bindings didn't change.
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([]);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be called on directives on an element', () => {
|
2018-12-20 20:23:25 -05:00
|
|
|
/**
|
|
|
|
* <div dir [val]="1" [publicVal2]="1"></div>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'div', ['dir', '']);
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 2, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'dir - dir',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
}]);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-12-20 20:23:25 -05:00
|
|
|
expect(events).toEqual([]);
|
2018-02-21 12:12:02 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onChanges properly in for loop', () => {
|
|
|
|
/**
|
2018-12-20 20:23:25 -05:00
|
|
|
* <comp [val1]="1" [publicVal2]="1"></comp>
|
2018-02-21 12:12:02 -05:00
|
|
|
* % for (let j = 2; j < 5; j++) {
|
2018-12-20 20:23:25 -05:00
|
|
|
* <comp [val1]="j" [publicVal2]="j"></comp>
|
2018-02-21 12:12:02 -05:00
|
|
|
* % }
|
2018-12-20 20:23:25 -05:00
|
|
|
* <comp [val1]="5" [publicVal2]="5"></comp>
|
2018-02-21 12:12:02 -05:00
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'comp');
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(2, 'val1', bind(5));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(2, 'publicVal2', bind(5));
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let j = 2; j < 5; j++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(j));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(j));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 4, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
|
|
|
// onChanges is called top to bottom, so top level comps (1 and 5) are called
|
|
|
|
// before the comps inside the for loop's embedded view (2, 3, and 4)
|
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 5, true),
|
|
|
|
'val2': new SimpleChange(undefined, 5, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 3, true),
|
|
|
|
'val2': new SimpleChange(undefined, 3, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 4, true),
|
|
|
|
'val2': new SimpleChange(undefined, 4, true),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onChanges properly in for loop with children', () => {
|
|
|
|
/**
|
2018-12-20 20:23:25 -05:00
|
|
|
* <parent [val1]="1" [publicVal2]="1"></parent>
|
2018-02-21 12:12:02 -05:00
|
|
|
* % for (let j = 2; j < 5; j++) {
|
2018-12-20 20:23:25 -05:00
|
|
|
* <parent [val1]="j" [publicVal2]="j"></parent>
|
2018-02-21 12:12:02 -05:00
|
|
|
* % }
|
2018-12-20 20:23:25 -05:00
|
|
|
* <parent [val1]="5" [publicVal2]="5"></parent>
|
2018-02-21 12:12:02 -05:00
|
|
|
*/
|
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-03-21 18:10:34 -04:00
|
|
|
container(1);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(2, 'parent');
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(1));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(1));
|
2018-04-10 23:57:09 -04:00
|
|
|
elementProperty(2, 'val1', bind(5));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(2, 'publicVal2', bind(5));
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
for (let j = 2; j < 5; j++) {
|
2018-08-18 14:14:50 -04:00
|
|
|
let rf1 = embeddedViewStart(0, 1, 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
if (rf1 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val1', bind(j));
|
2018-12-20 20:23:25 -05:00
|
|
|
elementProperty(0, 'publicVal2', bind(j));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-02-21 12:12:02 -05:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 3, 4, defs);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-02-21 12:12:02 -05:00
|
|
|
|
|
|
|
// onChanges is called top to bottom, so top level comps (1 and 5) are called
|
|
|
|
// before the comps inside the for loop's embedded view (2, 3, and 4)
|
|
|
|
expect(events).toEqual([
|
2018-12-20 20:23:25 -05:00
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 5, true),
|
|
|
|
'val2': new SimpleChange(undefined, 5, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 2, true),
|
|
|
|
'val2': new SimpleChange(undefined, 2, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 3, true),
|
|
|
|
'val2': new SimpleChange(undefined, 3, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 3, true),
|
|
|
|
'val2': new SimpleChange(undefined, 3, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - parent',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 4, true),
|
|
|
|
'val2': new SimpleChange(undefined, 4, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 4, true),
|
|
|
|
'val2': new SimpleChange(undefined, 4, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 1, true),
|
|
|
|
'val2': new SimpleChange(undefined, 1, true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'onChanges',
|
|
|
|
name: 'comp - comp',
|
|
|
|
changes: {
|
|
|
|
'val1': new SimpleChange(undefined, 5, true),
|
|
|
|
'val2': new SimpleChange(undefined, 5, true),
|
|
|
|
}
|
|
|
|
},
|
2018-02-21 12:12:02 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
it('should not call onChanges if props are set directly', () => {
|
|
|
|
let events: SimpleChanges[] = [];
|
|
|
|
let compInstance: MyComp;
|
|
|
|
class MyComp {
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) { events.push(changes); }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: MyComp,
|
|
|
|
factory: () => {
|
|
|
|
// Capture the instance so we can test setting the property directly
|
|
|
|
compInstance = new MyComp();
|
|
|
|
return compInstance;
|
|
|
|
},
|
|
|
|
template: (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
element(0, 'div');
|
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-02-05 00:42:55 -05:00
|
|
|
elementProperty(0, 'id', bind(ctx.a));
|
2018-12-20 20:23:25 -05:00
|
|
|
}
|
|
|
|
},
|
2019-02-05 00:42:55 -05:00
|
|
|
selectors: [['my-comp']],
|
2018-12-20 20:23:25 -05:00
|
|
|
inputs: {
|
|
|
|
value: 'value',
|
|
|
|
},
|
|
|
|
consts: 1,
|
|
|
|
vars: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <my-comp [value]="1"></my-comp>
|
|
|
|
*/
|
|
|
|
|
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-02-05 00:42:55 -05:00
|
|
|
element(0, 'my-comp');
|
2018-12-20 20:23:25 -05:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'value', bind(1));
|
|
|
|
}
|
|
|
|
}, 1, 1, [MyComp]);
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(App);
|
|
|
|
events = [];
|
|
|
|
|
|
|
|
// Try setting the property directly
|
|
|
|
compInstance !.value = 2;
|
|
|
|
|
|
|
|
fixture.update();
|
|
|
|
expect(events).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2018-02-21 12:12:02 -05:00
|
|
|
});
|
|
|
|
|
2018-01-18 17:22:52 -05:00
|
|
|
describe('hook order', () => {
|
|
|
|
let events: string[];
|
|
|
|
|
|
|
|
beforeEach(() => { events = []; });
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
function createAllHooksComponent(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives: any[] = []) {
|
2018-01-18 17:22:52 -05:00
|
|
|
return class Component {
|
|
|
|
val: string = '';
|
|
|
|
|
2018-02-21 12:12:02 -05:00
|
|
|
ngOnChanges() { events.push(`changes ${name}${this.val}`); }
|
|
|
|
|
2018-01-18 17:22:52 -05:00
|
|
|
ngOnInit() { events.push(`init ${name}${this.val}`); }
|
|
|
|
ngDoCheck() { events.push(`check ${name}${this.val}`); }
|
|
|
|
|
|
|
|
ngAfterContentInit() { events.push(`contentInit ${name}${this.val}`); }
|
|
|
|
ngAfterContentChecked() { events.push(`contentCheck ${name}${this.val}`); }
|
|
|
|
|
|
|
|
ngAfterViewInit() { events.push(`viewInit ${name}${this.val}`); }
|
|
|
|
ngAfterViewChecked() { events.push(`viewCheck ${name}${this.val}`); }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-01-18 17:22:52 -05:00
|
|
|
factory: () => new Component(),
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2018-02-21 12:12:02 -05:00
|
|
|
inputs: {val: 'val'}, template,
|
2019-01-16 12:35:35 -05:00
|
|
|
directives: directives,
|
2019-01-22 14:17:13 -05:00
|
|
|
features: [NgOnChangesFeature()],
|
2018-01-18 17:22:52 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should call all hooks in correct order', () => {
|
2018-04-10 23:57:09 -04:00
|
|
|
const Comp = createAllHooksComponent('comp', (rf: RenderFlags, ctx: any) => {});
|
2018-01-18 17:22:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <comp [val]="1"></comp>
|
|
|
|
* <comp [val]="2"></comp>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
|
|
|
element(1, 'comp');
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-12-20 20:23:25 -05:00
|
|
|
// This template function is a little weird in that the `elementProperty` calls
|
|
|
|
// below are directly setting values `1` and `2`, where normally there would be
|
|
|
|
// a call to `bind()` that would do the work of seeing if something changed.
|
|
|
|
// This means when `fixture.update()` is called below, ngOnChanges should fire,
|
|
|
|
// even though the *value* itself never changed.
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, [Comp]);
|
2018-01-18 17:22:52 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:22:52 -05:00
|
|
|
expect(events).toEqual([
|
2018-02-21 12:12:02 -05:00
|
|
|
'changes comp1', 'init comp1', 'check comp1', 'changes comp2', 'init comp2', 'check comp2',
|
|
|
|
'contentInit comp1', 'contentCheck comp1', 'contentInit comp2', 'contentCheck comp2',
|
|
|
|
'viewInit comp1', 'viewCheck comp1', 'viewInit comp2', 'viewCheck comp2'
|
2018-01-18 17:22:52 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
events = [];
|
2018-12-20 20:23:25 -05:00
|
|
|
fixture.update(); // Changes are made due to lack of `bind()` call in template fn.
|
2018-01-18 17:22:52 -05:00
|
|
|
expect(events).toEqual([
|
2018-02-21 12:12:02 -05:00
|
|
|
'changes comp1', 'check comp1', 'changes comp2', 'check comp2', 'contentCheck comp1',
|
|
|
|
'contentCheck comp2', 'viewCheck comp1', 'viewCheck comp2'
|
2018-01-18 17:22:52 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call all hooks in correct order with children', () => {
|
2018-04-10 23:57:09 -04:00
|
|
|
const Comp = createAllHooksComponent('comp', (rf: RenderFlags, ctx: any) => {});
|
2018-01-18 17:22:52 -05:00
|
|
|
|
|
|
|
/** <comp [val]="val"></comp> */
|
2018-04-10 23:57:09 -04:00
|
|
|
const Parent = createAllHooksComponent('parent', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'comp');
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(ctx.val));
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 1, 1, [Comp]);
|
2018-01-18 17:22:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <parent [val]="1"></parent>
|
|
|
|
* <parent [val]="2"></parent>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-27 17:28:22 -04:00
|
|
|
element(0, 'parent');
|
|
|
|
element(1, 'parent');
|
2018-01-18 17:22:52 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', 1);
|
|
|
|
elementProperty(1, 'val', 2);
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 0, [Parent]);
|
2018-01-18 17:22:52 -05:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-01-18 17:22:52 -05:00
|
|
|
expect(events).toEqual([
|
2018-02-21 12:12:02 -05:00
|
|
|
'changes parent1', 'init parent1', 'check parent1',
|
|
|
|
'changes parent2', 'init parent2', 'check parent2',
|
|
|
|
'contentInit parent1', 'contentCheck parent1', 'contentInit parent2',
|
|
|
|
'contentCheck parent2', 'changes comp1', 'init comp1',
|
|
|
|
'check comp1', 'contentInit comp1', 'contentCheck comp1',
|
|
|
|
'viewInit comp1', 'viewCheck comp1', 'changes comp2',
|
|
|
|
'init comp2', 'check comp2', 'contentInit comp2',
|
|
|
|
'contentCheck comp2', 'viewInit comp2', 'viewCheck comp2',
|
|
|
|
'viewInit parent1', 'viewCheck parent1', 'viewInit parent2',
|
|
|
|
'viewCheck parent2'
|
2018-01-18 17:22:52 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-01-18 17:22:52 -05:00
|
|
|
expect(events).toEqual([
|
2018-02-21 12:12:02 -05:00
|
|
|
'changes parent1', 'check parent1', 'changes parent2', 'check parent2',
|
|
|
|
'contentCheck parent1', 'contentCheck parent2', 'check comp1', 'contentCheck comp1',
|
|
|
|
'viewCheck comp1', 'check comp2', 'contentCheck comp2', 'viewCheck comp2',
|
|
|
|
'viewCheck parent1', 'viewCheck parent2'
|
2018-01-18 17:22:52 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-04-12 07:49:37 -04:00
|
|
|
// Angular 5 reference: https://stackblitz.com/edit/lifecycle-hooks-ng
|
|
|
|
it('should call all hooks in correct order with view and content', () => {
|
|
|
|
const Content = createAllHooksComponent('content', (rf: RenderFlags, ctx: any) => {});
|
|
|
|
|
|
|
|
const View = createAllHooksComponent('view', (rf: RenderFlags, ctx: any) => {});
|
|
|
|
|
|
|
|
/** <ng-content></ng-content><view [val]="val"></view> */
|
|
|
|
const Parent = createAllHooksComponent('parent', (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-07-03 23:04:36 -04:00
|
|
|
projectionDef();
|
|
|
|
projection(0);
|
2018-07-27 17:28:22 -04:00
|
|
|
element(1, 'view');
|
2018-04-12 07:49:37 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2018-07-03 23:04:36 -04:00
|
|
|
elementProperty(1, 'val', bind(ctx.val));
|
2018-04-12 07:49:37 -04:00
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 2, 1, [View]);
|
2018-04-12 07:49:37 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <parent [val]="1">
|
|
|
|
* <content [val]="1"></content>
|
|
|
|
* </parent>
|
|
|
|
* <parent [val]="2">
|
|
|
|
* <content [val]="2"></content>
|
|
|
|
* </parent>
|
|
|
|
*/
|
2018-08-16 21:53:21 -04:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-12 07:49:37 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'parent');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(1, 'content'); }
|
2018-04-12 07:49:37 -04:00
|
|
|
elementEnd();
|
|
|
|
elementStart(2, 'parent');
|
2018-07-27 17:28:22 -04:00
|
|
|
{ element(3, 'content'); }
|
2018-04-12 07:49:37 -04:00
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'val', bind(1));
|
|
|
|
elementProperty(1, 'val', bind(1));
|
|
|
|
elementProperty(2, 'val', bind(2));
|
|
|
|
elementProperty(3, 'val', bind(2));
|
|
|
|
}
|
2018-08-18 14:14:50 -04:00
|
|
|
}, 4, 4, [Parent, Content]);
|
2018-04-12 07:49:37 -04:00
|
|
|
|
2018-08-16 21:53:21 -04:00
|
|
|
const fixture = new ComponentFixture(App);
|
2018-04-12 07:49:37 -04:00
|
|
|
expect(events).toEqual([
|
|
|
|
'changes parent1', 'init parent1',
|
|
|
|
'check parent1', 'changes content1',
|
|
|
|
'init content1', 'check content1',
|
|
|
|
'changes parent2', 'init parent2',
|
|
|
|
'check parent2', 'changes content2',
|
|
|
|
'init content2', 'check content2',
|
|
|
|
'contentInit content1', 'contentCheck content1',
|
|
|
|
'contentInit parent1', 'contentCheck parent1',
|
|
|
|
'contentInit content2', 'contentCheck content2',
|
|
|
|
'contentInit parent2', 'contentCheck parent2',
|
|
|
|
'changes view1', 'init view1',
|
|
|
|
'check view1', 'contentInit view1',
|
|
|
|
'contentCheck view1', 'viewInit view1',
|
|
|
|
'viewCheck view1', 'changes view2',
|
|
|
|
'init view2', 'check view2',
|
|
|
|
'contentInit view2', 'contentCheck view2',
|
|
|
|
'viewInit view2', 'viewCheck view2',
|
|
|
|
'viewInit content1', 'viewCheck content1',
|
|
|
|
'viewInit parent1', 'viewCheck parent1',
|
|
|
|
'viewInit content2', 'viewCheck content2',
|
|
|
|
'viewInit parent2', 'viewCheck parent2'
|
|
|
|
]);
|
|
|
|
|
|
|
|
events = [];
|
2018-08-16 21:53:21 -04:00
|
|
|
fixture.update();
|
2018-04-12 07:49:37 -04:00
|
|
|
expect(events).toEqual([
|
|
|
|
'check parent1', 'check content1', 'check parent2', 'check content2',
|
|
|
|
'contentCheck content1', 'contentCheck parent1', 'contentCheck content2',
|
|
|
|
'contentCheck parent2', 'check view1', 'contentCheck view1', 'viewCheck view1',
|
|
|
|
'check view2', 'contentCheck view2', 'viewCheck view2', 'viewCheck content1',
|
|
|
|
'viewCheck parent1', 'viewCheck content2', 'viewCheck parent2'
|
|
|
|
]);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-01-18 17:22:52 -05:00
|
|
|
});
|
|
|
|
|
2018-07-16 08:21:54 -04:00
|
|
|
describe('non-regression', () => {
|
|
|
|
|
|
|
|
it('should call lifecycle hooks for directives active on <ng-template>', () => {
|
|
|
|
let destroyed = false;
|
|
|
|
|
|
|
|
class OnDestroyDirective implements OnDestroy {
|
|
|
|
ngOnDestroy() { destroyed = true; }
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: OnDestroyDirective,
|
|
|
|
selectors: [['', 'onDestroyDirective', '']],
|
|
|
|
factory: () => new OnDestroyDirective()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function conditionTpl(rf: RenderFlags, ctx: Cmpt) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-12-12 18:23:12 -05:00
|
|
|
template(
|
|
|
|
0, null, 0, 1, 'ng-template', [AttributeMarker.SelectOnly, 'onDestroyDirective']);
|
2018-07-16 08:21:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <ng-template [ngIf]="condition">
|
|
|
|
* <ng-template onDestroyDirective></ng-template>
|
|
|
|
* </ng-template>
|
|
|
|
*/
|
|
|
|
function cmptTpl(rf: RenderFlags, cmpt: Cmpt) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-12-12 18:23:12 -05:00
|
|
|
template(0, conditionTpl, 1, 1, 'ng-template', [AttributeMarker.SelectOnly, 'ngIf']);
|
2018-07-16 08:21:54 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'ngIf', bind(cmpt.showing));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Cmpt {
|
|
|
|
showing = true;
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Cmpt,
|
|
|
|
factory: () => new Cmpt(),
|
|
|
|
selectors: [['cmpt']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2018-07-16 08:21:54 -04:00
|
|
|
template: cmptTpl,
|
|
|
|
directives: [NgIf, OnDestroyDirective]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const fixture = new ComponentFixture(Cmpt);
|
|
|
|
expect(destroyed).toBeFalsy();
|
|
|
|
|
|
|
|
fixture.component.showing = false;
|
|
|
|
fixture.update();
|
|
|
|
expect(destroyed).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|