From fe7e4894a3e27968c82e4a4d9bb9b845b0b54dca Mon Sep 17 00:00:00 2001 From: lolismatic Date: Wed, 11 Sep 2019 14:08:58 +0300 Subject: [PATCH] fix(datepicker): make datepicker view child static (#5374) Fix a bug related to an undefined reference error when writing a value to the datepicker before it's ngAfterContentInit hook. Closes #5373 --- src/datepicker/datepicker.component.spec.ts | 43 +++++++++++++++++++++ src/datepicker/datepicker.component.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/datepicker/datepicker.component.spec.ts diff --git a/src/datepicker/datepicker.component.spec.ts b/src/datepicker/datepicker.component.spec.ts new file mode 100644 index 0000000000..d529b98753 --- /dev/null +++ b/src/datepicker/datepicker.component.spec.ts @@ -0,0 +1,43 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; +import { Component, ViewChild, OnInit } from '@angular/core'; + +import { DatepickerModule, DatePickerComponent } from '.'; + +@Component({ + selector: 'test-cmp', + template: `` +}) +class TestComponent implements OnInit { + @ViewChild(DatePickerComponent, { static: true }) datepicker: DatePickerComponent; + + ngOnInit(): void { + this.datepicker.writeValue(new Date()); + } +} + +type TestFixture = ComponentFixture; + +describe('datepicker:', () => { + let fixture: TestFixture; + + beforeEach( + async(() => TestBed.configureTestingModule({ + declarations: [TestComponent], + imports: [ + DatepickerModule.forRoot(), + BrowserAnimationsModule + ] + }).compileComponents()) + ); + + it('should not throw undefined reference error when initializing value before content init hook', + () => { + // tslint:disable-next-line: no-floating-promises + expect(() => { + fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); + }).not.toThrowError(/^.*undefined.*$/gm); + } + ); +}); diff --git a/src/datepicker/datepicker.component.ts b/src/datepicker/datepicker.component.ts index 5e87145d1c..41484ae69e 100644 --- a/src/datepicker/datepicker.component.ts +++ b/src/datepicker/datepicker.component.ts @@ -122,7 +122,7 @@ export class DatePickerComponent implements ControlValueAccessor { undefined ); - @ViewChild(DatePickerInnerComponent, { static: false }) + @ViewChild(DatePickerInnerComponent, { static: true }) _datePicker: DatePickerInnerComponent; /* tslint:disable-next-line: no-any*/