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-05-09 14:47:25 -04:00
|
|
|
import {AttributeMarker, ComponentTemplate, LifecycleHooksFeature, injectComponentFactoryResolver, ΔNgOnChangesFeature, ΔdefineComponent, ΔdefineDirective} from '../../src/render3/index';
|
|
|
|
import {markDirty, Δbind, Δcontainer, ΔcontainerRefreshEnd, ΔcontainerRefreshStart, ΔdirectiveInject, Δelement, ΔelementEnd, ΔelementProperty, ΔelementStart, ΔembeddedViewEnd, ΔembeddedViewStart, Δlistener, Δprojection, ΔprojectionDef, Δselect, Δtemplate, Δtext} from '../../src/render3/instructions/all';
|
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';
|
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) {
|
2019-05-09 14:47:25 -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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'val', Δbind(ctx.val));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔprojectionDef();
|
|
|
|
ΔelementStart(0, 'div');
|
|
|
|
{ Δprojection(1); }
|
|
|
|
Δ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) {
|
2019-05-09 14:47:25 -04: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
|
|
|
|
2019-05-09 14:47:25 -04: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'); }
|
|
|
|
|
2019-05-09 14:47:25 -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 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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δcontainer(0);
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2018-08-16 21:53:21 -04:00
|
|
|
if (!ctx.skip) {
|
2019-05-09 14:47:25 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δelement(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:25 -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-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}`); }
|
|
|
|
|
2019-05-09 14:47:25 -04: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-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-05-09 14:47:25 -04: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) {
|
2019-05-09 14:47:25 -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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'val', 1);
|
|
|
|
Δselect(1);
|
|
|
|
ΔelementProperty(1, 'val', 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
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) {
|
2019-05-09 14:47:25 -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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'val', Δbind(ctx.val));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
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) {
|
2019-05-09 14:47:25 -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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'val', 1);
|
|
|
|
Δselect(1);
|
|
|
|
ΔelementProperty(1, 'val', 2);
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔprojectionDef();
|
|
|
|
Δprojection(0);
|
|
|
|
Δelement(1, 'view');
|
2018-04-12 07:49:37 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δselect(1);
|
|
|
|
Δ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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'parent');
|
|
|
|
{ Δelement(1, 'content'); }
|
|
|
|
ΔelementEnd();
|
|
|
|
ΔelementStart(2, 'parent');
|
|
|
|
{ Δelement(3, 'content'); }
|
|
|
|
ΔelementEnd();
|
2018-04-12 07:49:37 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'val', Δbind(1));
|
|
|
|
Δselect(1);
|
|
|
|
ΔelementProperty(1, 'val', Δbind(1));
|
|
|
|
Δselect(2);
|
|
|
|
ΔelementProperty(2, 'val', Δbind(2));
|
|
|
|
Δselect(3);
|
|
|
|
ΔelementProperty(3, 'val', Δbind(2));
|
2018-04-12 07:49:37 -04:00
|
|
|
}
|
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; }
|
|
|
|
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngDirectiveDef = ΔdefineDirective({
|
2018-07-16 08:21:54 -04:00
|
|
|
type: OnDestroyDirective,
|
|
|
|
selectors: [['', 'onDestroyDirective', '']],
|
|
|
|
factory: () => new OnDestroyDirective()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function conditionTpl(rf: RenderFlags, ctx: Cmpt) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtemplate(0, null, 0, 1, 'ng-template', [AttributeMarker.Bindings, '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) {
|
2019-05-09 14:47:25 -04:00
|
|
|
Δtemplate(0, conditionTpl, 1, 1, 'ng-template', [AttributeMarker.Bindings, 'ngIf']);
|
2018-07-16 08:21:54 -04:00
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementProperty(0, 'ngIf', Δbind(cmpt.showing));
|
2018-07-16 08:21:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Cmpt {
|
|
|
|
showing = true;
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-07-16 08:21:54 -04:00
|
|
|
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
|
|
|
});
|