diff --git a/packages/core/test/acceptance/lifecycle_spec.ts b/packages/core/test/acceptance/lifecycle_spec.ts index 55330ec033..264f75e59d 100644 --- a/packages/core/test/acceptance/lifecycle_spec.ts +++ b/packages/core/test/acceptance/lifecycle_spec.ts @@ -662,3 +662,151 @@ describe('onInit', () => { ]); }); }); + +describe('doCheck', () => { + it('should call doCheck on every refresh', () => { + let doCheckCalled = 0; + + @Component({template: ``}) + class App { + ngDoCheck() { doCheckCalled++; } + } + + TestBed.configureTestingModule({ + declarations: [App], + }); + const fixture = TestBed.createComponent(App); + fixture.detectChanges(); + + expect(doCheckCalled).toBe(1); + + fixture.detectChanges(); + + expect(doCheckCalled).toBe(2); + }); + + it('should call parent doCheck before child doCheck', () => { + const doChecks: string[] = []; + + @Component({ + selector: 'parent', + template: ``, + }) + class Parent { + ngDoCheck() { doChecks.push('parent'); } + } + + @Component({ + selector: 'child', + template: ``, + }) + class Child { + ngDoCheck() { doChecks.push('child'); } + } + + @Component({template: ``}) + class App { + ngDoCheck() { doChecks.push('app'); } + } + + TestBed.configureTestingModule({ + declarations: [App, Parent, Child], + }); + const fixture = TestBed.createComponent(App); + fixture.detectChanges(); + + expect(doChecks).toEqual(['app', 'parent', 'child']); + }); + + it('should call ngOnInit before ngDoCheck if creation mode', () => { + const events: string[] = []; + @Component({template: ``}) + class App { + ngOnInit() { events.push('onInit'); } + + ngDoCheck() { events.push('doCheck'); } + } + + TestBed.configureTestingModule({ + declarations: [App], + }); + const fixture = TestBed.createComponent(App); + fixture.detectChanges(); + + expect(events).toEqual(['onInit', 'doCheck']); + }); + + it('should be called on directives after component', () => { + const doChecks: string[] = []; + @Directive({ + selector: '[dir]', + }) + class Dir { + @Input('dir') + name = ''; + + ngDoCheck() { doChecks.push('dir ' + this.name); } + } + + @Component({ + selector: 'comp', + template: `

test

`, + }) + class Comp { + @Input() + name = ''; + + ngDoCheck() { doChecks.push('comp ' + this.name); } + } + + @Component({ + template: ` + + + ` + }) + class App { + ngDoCheck() { doChecks.push('app'); } + } + + TestBed.configureTestingModule({ + declarations: [App, Comp, Dir], + }); + const fixture = TestBed.createComponent(App); + fixture.detectChanges(); + + expect(doChecks).toEqual(['app', 'comp 1', 'dir 1', 'comp 2', 'dir 2']); + }); + + it('should be called on directives on an element', () => { + const doChecks: string[] = []; + + @Directive({ + selector: '[dir]', + }) + class Dir { + @Input('dir') + name = ''; + + ngDoCheck() { doChecks.push('dir ' + this.name); } + } + + @Component({ + template: ` +

+

+ ` + }) + class App { + ngDoCheck() { doChecks.push('app'); } + } + + TestBed.configureTestingModule({ + declarations: [App, Dir], + }); + const fixture = TestBed.createComponent(App); + fixture.detectChanges(); + + expect(doChecks).toEqual(['app', 'dir 1', 'dir 2']); + }); +}); diff --git a/packages/core/test/render3/lifecycle_spec.ts b/packages/core/test/render3/lifecycle_spec.ts index 868f185bb0..9b20c9bdf6 100644 --- a/packages/core/test/render3/lifecycle_spec.ts +++ b/packages/core/test/render3/lifecycle_spec.ts @@ -116,6 +116,7 @@ describe('lifecycles', () => { }); }); +<<<<<<< HEAD describe('doCheck', () => { let events: string[]; let allEvents: string[]; @@ -246,6 +247,8 @@ describe('lifecycles', () => { }); +======= +>>>>>>> test(ivy): doCheck lifecycle acceptance tests describe('afterContentInit', () => { let events: string[]; let allEvents: string[];