Skip to content

Commit

Permalink
fix(directives): fire ngDoCheck hook for dynamic directives
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Malkevich committed Dec 13, 2018
1 parent 28ba4b5 commit d3e5888
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/dynamic/dynamic-directives.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ class MockDirective
AfterContentChecked {
static INSTANCES = new Set<MockDirective>();
@Input()
in: any;
set in(val: any) {
this.logHook('inputSet:' + val)();
this._in = val;
}
get in(): any {
return this._in;
}
private _in: any;
@Input()
in2: any;
@Output()
Expand All @@ -69,6 +76,7 @@ class MockDirective
.mockImplementation(this.logHook('ngAfterViewInit'));
ngOnChanges = jest.fn().mockImplementation(this.logHook('ngOnChanges'));
ngOnInit = jest.fn().mockImplementation(this.logHook('ngOnInit'));
ngDoCheck = jest.fn().mockImplementation(this.logHook('ngDoCheck'));
ngOnDestroy = jest.fn().mockImplementation(() => {
this.hooksOrder.push('ngOnDestroy');
MockDirective.INSTANCES.delete(this);
Expand Down Expand Up @@ -156,13 +164,26 @@ describe('Directive: DynamicDirectives', () => {
// Verify order
expect(dir.hooksOrder).toEqual([
'ngOnInit',
'ngDoCheck',
'ngAfterContentInit',
'ngAfterContentChecked',
'ngAfterViewInit',
'ngAfterViewChecked',
]);
});

it('should set inputs before ngOnInit hook called', () => {
hostComp.dirs = [dynamicDirectiveDef(MockDirective, { in: true })];

fixture.detectChanges();

const dir = getFirstDir();

expect(dir.hooksOrder[0]).toBe('inputSet:true');
expect(dir.hooksOrder[1]).toBe('ngOnChanges');
expect(dir.hooksOrder[2]).toBe('ngOnInit');
});

it('should not init directives of same type', () => {
hostComp.dirs = [
dynamicDirectiveDef(MockDirective),
Expand Down
3 changes: 2 additions & 1 deletion src/dynamic/dynamic-directives.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
onDestroy: this.componentRef.onDestroy,
};

this.callInitHooks(instance);
this.initDirIO(dir, dirDef.inputs, dirDef.outputs);
this.callInitHooks(instance);

this.dirRef.set(dir.type, dir);

Expand Down Expand Up @@ -264,6 +264,7 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {

private callInitHooks(obj: any) {
this.callHook(obj, 'ngOnInit');
this.callHook(obj, 'ngDoCheck');
this.callHook(obj, 'ngAfterContentInit');
this.callHook(obj, 'ngAfterContentChecked');
this.callHook(obj, 'ngAfterViewInit');
Expand Down

0 comments on commit d3e5888

Please sign in to comment.