From 894b098eb3f5fb9d9f234059daf6647a9ac01018 Mon Sep 17 00:00:00 2001 From: Marc Laval Date: Thu, 22 Feb 2018 11:58:29 +0100 Subject: [PATCH] test(ivy): add missing tests on directive lifecycle hooks (#22368) PR Close #22368 --- packages/core/test/render3/lifecycle_spec.ts | 108 ++++++++++++++----- 1 file changed, 83 insertions(+), 25 deletions(-) diff --git a/packages/core/test/render3/lifecycle_spec.ts b/packages/core/test/render3/lifecycle_spec.ts index f06931b659533..799ada0d99f3a 100644 --- a/packages/core/test/render3/lifecycle_spec.ts +++ b/packages/core/test/render3/lifecycle_spec.ts @@ -233,7 +233,9 @@ describe('lifecycles', () => { elementEnd(); } Comp.ngComponentDef.h(1, 0); + Directive.ngDirectiveDef.h(2, 0); componentRefresh(1, 0); + componentRefresh(2, 0); } renderToHtml(Template, {}); @@ -534,30 +536,6 @@ describe('lifecycles', () => { expect(events).toEqual(['comp', 'comp']); }); - it('should be called on directives after component', () => { - class Directive { - ngAfterContentInit() { events.push('dir'); } - - static ngDirectiveDef = defineDirective({type: Directive, factory: () => new Directive()}); - } - - function Template(ctx: any, cm: boolean) { - if (cm) { - elementStart(0, Comp, null, [Directive]); - elementEnd(); - } - Comp.ngComponentDef.h(1, 0); - componentRefresh(1, 0); - } - - renderToHtml(Template, {}); - expect(events).toEqual(['comp', 'dir']); - - renderToHtml(Template, {}); - expect(events).toEqual(['comp', 'dir']); - - }); - it('should be called in parents before children', () => { /** * content @@ -801,6 +779,47 @@ describe('lifecycles', () => { }); }); + + describe('directives', () => { + class Directive { + ngAfterContentInit() { events.push('init'); } + ngAfterContentChecked() { events.push('check'); } + + static ngDirectiveDef = defineDirective({type: Directive, factory: () => new Directive()}); + } + + it('should be called on directives after component', () => { + /** */ + function Template(ctx: any, cm: boolean) { + if (cm) { + elementStart(0, Comp, null, [Directive]); + elementEnd(); + } + Comp.ngComponentDef.h(1, 0); + Directive.ngDirectiveDef.h(2, 0); + componentRefresh(1, 0); + componentRefresh(2, 0); + } + + renderToHtml(Template, {}); + expect(events).toEqual(['comp', 'init', 'check']); + }); + + it('should be called on directives on an element', () => { + /**
*/ + function Template(ctx: any, cm: boolean) { + if (cm) { + elementStart(0, 'div', null, [Directive]); + elementEnd(); + } + Directive.ngDirectiveDef.h(1, 0); + componentRefresh(1, 0); + } + + renderToHtml(Template, {}); + expect(events).toEqual(['init', 'check']); + }); + }); }); describe('afterViewInit', () => { @@ -1147,7 +1166,6 @@ describe('lifecycles', () => { }); - describe('ngAfterViewChecked', () => { it('should call ngAfterViewChecked every update', () => { @@ -1236,6 +1254,46 @@ describe('lifecycles', () => { }); + describe('directives', () => { + class Directive { + ngAfterViewInit() { events.push('init'); } + ngAfterViewChecked() { events.push('check'); } + + static ngDirectiveDef = defineDirective({type: Directive, factory: () => new Directive()}); + } + + it('should be called on directives after component', () => { + /** */ + function Template(ctx: any, cm: boolean) { + if (cm) { + elementStart(0, Comp, null, [Directive]); + elementEnd(); + } + Comp.ngComponentDef.h(1, 0); + Directive.ngDirectiveDef.h(2, 0); + componentRefresh(1, 0); + componentRefresh(2, 0); + } + + renderToHtml(Template, {}); + expect(events).toEqual(['comp', 'init', 'check']); + }); + + it('should be called on directives on an element', () => { + /**
*/ + function Template(ctx: any, cm: boolean) { + if (cm) { + elementStart(0, 'div', null, [Directive]); + elementEnd(); + } + Directive.ngDirectiveDef.h(1, 0); + componentRefresh(1, 0); + } + + renderToHtml(Template, {}); + expect(events).toEqual(['init', 'check']); + }); + }); }); describe('onDestroy', () => {