2017-12-01 17:23:03 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-06-18 11:58:15 -04:00
|
|
|
import {ComponentTemplate, ɵɵdefineComponent, ɵɵdefineDirective, ɵɵproperty} from '../../src/render3/index';
|
2019-09-06 17:43:16 -04:00
|
|
|
import {ɵɵcontainer, ɵɵcontainerRefreshEnd, ɵɵcontainerRefreshStart, ɵɵelement, ɵɵelementEnd, ɵɵelementStart, ɵɵembeddedViewEnd, ɵɵembeddedViewStart, ɵɵprojection, ɵɵprojectionDef, ɵɵ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';
|
2019-05-15 14:52:38 -04:00
|
|
|
import {ComponentFixture, createComponent} 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-17 21:49:21 -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-24 17:20:41 -04:00
|
|
|
ɵɵproperty('val', 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 = []; });
|
|
|
|
|
2019-05-15 14:52:38 -04:00
|
|
|
let Comp = createOnInitComponent('comp', (rf: RenderFlags) => {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-17 21:49:21 -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]);
|
2019-05-15 14:52:38 -04:00
|
|
|
let ProjectedComp = createOnInitComponent('projected', (rf: RenderFlags) => {
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Create) {
|
2019-05-17 21:49:21 -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(
|
2019-09-23 14:08:51 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, decls: number, vars: number = 0,
|
2018-08-21 03:03:21 -04:00
|
|
|
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-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => new Component();
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵdefineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: decls,
|
2018-08-21 03:03:21 -04:00
|
|
|
vars: vars,
|
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-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => new Directive();
|
2019-10-11 15:28:12 -04:00
|
|
|
static ɵdir = ɵɵdefineDirective({type: Directive, selectors: [['', 'dir', '']]});
|
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-17 21:49:21 -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-17 21:49:21 -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-17 21:49:21 -04:00
|
|
|
let rf1 = ɵɵembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵelement(0, 'comp');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵembeddedViewEnd();
|
2017-12-20 19:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 21:49:21 -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']);
|
|
|
|
});
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|