Skip to content

Commit

Permalink
fix(datepicker): make datepicker view child static (#5374)
Browse files Browse the repository at this point in the history
Fix a bug related to an undefined reference error when writing a
value to the datepicker before it's ngAfterContentInit hook.

Closes #5373
  • Loading branch information
lolismatic authored and Domainv committed Sep 11, 2019
1 parent 91bda67 commit fe7e489
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/datepicker/datepicker.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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: `<datepicker></datepicker>`
})
class TestComponent implements OnInit {
@ViewChild(DatePickerComponent, { static: true }) datepicker: DatePickerComponent;

ngOnInit(): void {
this.datepicker.writeValue(new Date());
}
}

type TestFixture = ComponentFixture<TestComponent>;

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);
}
);
});
2 changes: 1 addition & 1 deletion src/datepicker/datepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down

0 comments on commit fe7e489

Please sign in to comment.