Skip to content

Commit

Permalink
test(ivy): add missing tests on directive lifecycle hooks (#22368)
Browse files Browse the repository at this point in the history
PR Close #22368
  • Loading branch information
marclaval authored and vicb committed Feb 22, 2018
1 parent 022ad4a commit 894b098
Showing 1 changed file with 83 additions and 25 deletions.
108 changes: 83 additions & 25 deletions packages/core/test/render3/lifecycle_spec.ts
Expand Up @@ -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, {});
Expand Down Expand Up @@ -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', () => {
/**
* <parent>content</parent>
Expand Down Expand Up @@ -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', () => {
/** <comp directive></comp> */
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', () => {
/** <div directive></div> */
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', () => {
Expand Down Expand Up @@ -1147,7 +1166,6 @@ describe('lifecycles', () => {

});


describe('ngAfterViewChecked', () => {

it('should call ngAfterViewChecked every update', () => {
Expand Down Expand Up @@ -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', () => {
/** <comp directive></comp> */
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', () => {
/** <div directive></div> */
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', () => {
Expand Down

0 comments on commit 894b098

Please sign in to comment.