2017-12-01 14:23:03 -08:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-06-18 17:58:15 +02:00
|
|
|
import {ComponentTemplate, ɵɵdefineComponent, ɵɵdefineDirective, ɵɵproperty} from '../../src/render3/index';
|
2019-09-06 23:43:16 +02:00
|
|
|
import {ɵɵcontainer, ɵɵcontainerRefreshEnd, ɵɵcontainerRefreshStart, ɵɵelement, ɵɵelementEnd, ɵɵelementStart, ɵɵembeddedViewEnd, ɵɵembeddedViewStart, ɵɵprojection, ɵɵprojectionDef, ɵɵtext} from '../../src/render3/instructions/all';
|
2018-04-10 20:57:09 -07:00
|
|
|
import {RenderFlags} from '../../src/render3/interfaces/definition';
|
2018-07-16 14:21:54 +02:00
|
|
|
import {NgIf} from './common_with_def';
|
2019-05-15 11:52:38 -07:00
|
|
|
import {ComponentFixture, createComponent} from './render_util';
|
2017-12-01 14:23:03 -08:00
|
|
|
|
|
|
|
describe('lifecycles', () => {
|
|
|
|
|
2018-03-25 21:32:39 -07:00
|
|
|
function getParentTemplate(name: string) {
|
2018-04-10 20:57:09 -07:00
|
|
|
return (rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵelement(0, name);
|
2017-12-22 16:41:34 -08:00
|
|
|
}
|
2018-04-10 20:57:09 -07:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-24 14:20:41 -07:00
|
|
|
ɵɵproperty('val', ctx.val);
|
2018-04-10 20:57:09 -07:00
|
|
|
}
|
2017-12-22 16:41:34 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-20 16:26:07 -08:00
|
|
|
describe('onInit', () => {
|
|
|
|
let events: string[];
|
|
|
|
|
|
|
|
beforeEach(() => { events = []; });
|
|
|
|
|
2019-05-15 11:52:38 -07:00
|
|
|
let Comp = createOnInitComponent('comp', (rf: RenderFlags) => {
|
2018-04-10 20:57:09 -07:00
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵprojectionDef();
|
|
|
|
ɵɵelementStart(0, 'div');
|
|
|
|
{ ɵɵprojection(1); }
|
|
|
|
ɵɵelementEnd();
|
2018-01-18 14:13:23 -08:00
|
|
|
}
|
2018-08-16 18:53:21 -07:00
|
|
|
}, 2);
|
2018-08-21 00:03:21 -07:00
|
|
|
let Parent = createOnInitComponent('parent', getParentTemplate('comp'), 1, 1, [Comp]);
|
2019-05-15 11:52:38 -07:00
|
|
|
let ProjectedComp = createOnInitComponent('projected', (rf: RenderFlags) => {
|
2018-04-10 20:57:09 -07:00
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵtext(0, 'content');
|
2018-01-18 14:13:23 -08:00
|
|
|
}
|
2018-08-16 18:53:21 -07:00
|
|
|
}, 1);
|
2017-12-20 16:26:07 -08:00
|
|
|
|
2018-03-25 21:32:39 -07:00
|
|
|
function createOnInitComponent(
|
2019-09-23 20:08:51 +02:00
|
|
|
name: string, template: ComponentTemplate<any>, decls: number, vars: number = 0,
|
2018-08-21 00:03:21 -07:00
|
|
|
directives: any[] = []) {
|
2017-12-20 16:26:07 -08:00
|
|
|
return class Component {
|
|
|
|
val: string = '';
|
2018-08-16 18:53:21 -07:00
|
|
|
ngOnInit() {
|
|
|
|
if (!this.val) this.val = '';
|
|
|
|
events.push(`${name}${this.val}`);
|
|
|
|
}
|
2017-12-20 16:26:07 -08:00
|
|
|
|
2019-10-11 14:18:45 -07:00
|
|
|
static ɵfac = () => new Component();
|
2019-10-10 14:57:15 -07:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-01-22 15:27:21 -08:00
|
|
|
type: Component,
|
2018-03-29 16:41:45 -07:00
|
|
|
selectors: [[name]],
|
2019-09-23 20:08:51 +02:00
|
|
|
decls: decls,
|
2018-08-21 00:03:21 -07:00
|
|
|
vars: vars,
|
2018-03-25 21:32:39 -07:00
|
|
|
inputs: {val: 'val'}, template,
|
2018-03-29 12:58:41 -07:00
|
|
|
directives: directives
|
2017-12-20 16:26:07 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-21 15:08:18 +01:00
|
|
|
class Directive {
|
|
|
|
ngOnInit() { events.push('dir'); }
|
|
|
|
|
2019-10-11 14:18:45 -07:00
|
|
|
static ɵfac = () => new Directive();
|
2019-10-11 12:28:12 -07:00
|
|
|
static ɵdir = ɵɵdefineDirective({type: Directive, selectors: [['', 'dir', '']]});
|
2018-02-21 15:08:18 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 22:10:23 -07:00
|
|
|
const directives = [Comp, Parent, ProjectedComp, Directive, NgIf];
|
2018-03-25 21:32:39 -07:00
|
|
|
|
2017-12-20 16:26:07 -08:00
|
|
|
it('should call onInit every time a new view is created (if block)', () => {
|
|
|
|
/**
|
2018-08-16 18:53:21 -07:00
|
|
|
* % if (!skip) {
|
2017-12-20 16:26:07 -08:00
|
|
|
* <comp></comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-08-16 18:53:21 -07:00
|
|
|
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
|
2018-04-10 20:57:09 -07:00
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵcontainer(0);
|
2017-12-20 16:26:07 -08:00
|
|
|
}
|
2018-04-10 20:57:09 -07:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵcontainerRefreshStart(0);
|
2018-04-10 20:57:09 -07:00
|
|
|
{
|
2018-08-16 18:53:21 -07:00
|
|
|
if (!ctx.skip) {
|
2019-05-17 18:49:21 -07:00
|
|
|
let rf1 = ɵɵembeddedViewStart(0, 1, 0);
|
2018-04-10 20:57:09 -07:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵelement(0, 'comp');
|
2018-04-10 20:57:09 -07:00
|
|
|
}
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵembeddedViewEnd();
|
2017-12-20 16:26:07 -08:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 18:49:21 -07:00
|
|
|
ɵɵcontainerRefreshEnd();
|
2017-12-20 16:26:07 -08:00
|
|
|
}
|
2018-08-18 11:14:50 -07:00
|
|
|
}, 1, 0, directives);
|
2017-12-20 16:26:07 -08:00
|
|
|
|
2018-08-16 18:53:21 -07:00
|
|
|
const fixture = new ComponentFixture(App);
|
2017-12-20 16:26:07 -08:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 18:53:21 -07:00
|
|
|
fixture.component.skip = true;
|
|
|
|
fixture.update();
|
2017-12-20 16:26:07 -08:00
|
|
|
expect(events).toEqual(['comp']);
|
|
|
|
|
2018-08-16 18:53:21 -07:00
|
|
|
fixture.component.skip = false;
|
|
|
|
fixture.update();
|
2017-12-20 16:26:07 -08:00
|
|
|
expect(events).toEqual(['comp', 'comp']);
|
|
|
|
});
|
|
|
|
});
|
2017-12-01 14:23:03 -08:00
|
|
|
});
|