Skip to content

Commit 4076f3e

Browse files
authored
fix(date-value-handler): add some null checks to ensure component has everything it needs before trying to use a property (#771)
1 parent 3947992 commit 4076f3e

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

‎angular.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"configurations": {
5757
"development": {
5858
"optimization": false,
59-
"sourceMap": false,
59+
"sourceMap": true,
6060
"namedChunks": true,
6161
"aot": true,
6262
"extractLicenses": false,

‎src/app/workspace/resource/values/date-value/date-value-handler/date-value-handler.component.html‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
<mat-icon *ngIf="isPeriodControl.value">remove</mat-icon>
1313
</button>
1414

15-
<div *ngIf="isPeriodControl.value" class="date-form-field">
16-
<app-date-picker class="end-date" [formControlName]="'endDate'" [disableCalendarSelector]="true" [calendar]="startDate.value.calendar">
15+
<div *ngIf="isPeriodControl.value && startDate.value" class="date-form-field">
16+
<app-date-picker class="end-date" [formControlName]="'endDate'" [disableCalendarSelector]="true"
17+
[calendar]="startDate.value.calendar">
1718
</app-date-picker>
1819
</div>
1920
</div>
@@ -30,4 +31,4 @@
3031
</mat-error>
3132
</div>
3233

33-
</div>
34+
</div>

‎src/app/workspace/resource/values/date-value/date-value-handler/date-value-handler.component.spec.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,25 @@ describe('DateValueHandlerComponent', () => {
286286
(startDateEditComponentDe.componentInstance as TestDatePickerComponent).writeValue(new KnoraDate('JULIAN', 'CE', 2019, 5, 19));
287287
(startDateEditComponentDe.componentInstance as TestDatePickerComponent)._handleInput();
288288

289+
testHostFixture.detectChanges();
290+
289291
const endDateEditComponentDe = hostCompDe.query(By.css('.end-date'));
290292

291293
(endDateEditComponentDe.componentInstance as TestDatePickerComponent).writeValue(new KnoraDate('JULIAN', 'CE', 2020, 5, 19));
292294
(endDateEditComponentDe.componentInstance as TestDatePickerComponent)._handleInput();
293295

296+
testHostFixture.detectChanges();
297+
294298
await testHostFixture.whenStable();
295299

296300
expect(testHostComponent.dateValueHandlerComponent.form.valid).toBe(true);
297301

298302
expect(testHostComponent.form.controls.date.value).toEqual(new KnoraPeriod(new KnoraDate('JULIAN', 'CE', 2019, 5, 19), new KnoraDate('JULIAN', 'CE', 2020, 5, 19)));
303+
304+
// access private _subscriptions property and ensure that each subscription is still open
305+
testHostComponent.dateValueHandlerComponent['_subscriptions'].forEach((sub) => {
306+
expect(sub.closed).toBeFalse();
307+
});
299308
});
300309

301310
it('should return "null" for an invalid user input (start date greater than end date)', async () => {
@@ -373,7 +382,44 @@ describe('DateValueHandlerComponent (no validator required)', () => {
373382
expect(testHostComponent).toBeTruthy();
374383
});
375384

376-
it('should receive the propagated valueRequiredValidator from the parent component', () => {
385+
it('should propagate changes made by the user for a period', async () => {
386+
expect(testHostComponent.dateValueHandlerComponent.isPeriodControl.value).toBeFalse();
387+
388+
testHostComponent.dateValueHandlerComponent.isPeriodControl.setValue(true);
389+
390+
expect(testHostComponent.dateValueHandlerComponent.isPeriodControl.value).toBeTrue();
391+
392+
testHostFixture.detectChanges();
393+
394+
const hostCompDe = testHostFixture.debugElement;
395+
396+
const startDateEditComponentDe = hostCompDe.query(By.css('.start-date'));
397+
398+
(startDateEditComponentDe.componentInstance as TestDatePickerComponent).writeValue(new KnoraDate('JULIAN', 'CE', 2019, 5, 19));
399+
(startDateEditComponentDe.componentInstance as TestDatePickerComponent)._handleInput();
400+
401+
testHostFixture.detectChanges();
402+
403+
const endDateEditComponentDe = hostCompDe.query(By.css('.end-date'));
404+
405+
(endDateEditComponentDe.componentInstance as TestDatePickerComponent).writeValue(new KnoraDate('JULIAN', 'CE', 2020, 5, 19));
406+
(endDateEditComponentDe.componentInstance as TestDatePickerComponent)._handleInput();
407+
408+
testHostFixture.detectChanges();
409+
410+
await testHostFixture.whenStable();
411+
412+
expect(testHostComponent.dateValueHandlerComponent.form.valid).toBe(true);
413+
414+
expect(testHostComponent.form.controls.date.value).toEqual(new KnoraPeriod(new KnoraDate('JULIAN', 'CE', 2019, 5, 19), new KnoraDate('JULIAN', 'CE', 2020, 5, 19)));
415+
416+
// access private _subscriptions property and ensure that each subscription is still open
417+
testHostComponent.dateValueHandlerComponent['_subscriptions'].forEach((sub) => {
418+
expect(sub.closed).toBeFalse();
419+
});
420+
});
421+
422+
it('should not receive the propagated valueRequiredValidator from the parent component', () => {
377423
expect(testHostComponent.dateValueHandlerComponent.valueRequiredValidator).toBe(false);
378424
});
379425

‎src/app/workspace/resource/values/date-value/date-value-handler/date-value-handler.component.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class DateValueHandlerComponent extends _MatInputMixinBase implements Con
100100
if (!this.isPeriodControl.value) {
101101
return this.startDate.value;
102102
} else {
103-
if (this.startDate.value.calendar !== this.endDate.value.calendar) {
103+
if (this.endDate.value && (this.startDate.value.calendar !== this.endDate.value.calendar)) {
104104
this.endDate.value.calendar = this.startDate.value.calendar;
105105
}
106106

@@ -109,7 +109,6 @@ export class DateValueHandlerComponent extends _MatInputMixinBase implements Con
109109
}
110110

111111
set value(date: KnoraDate | KnoraPeriod | null) {
112-
113112
if (date instanceof KnoraDate) {
114113
// single date
115114
this.calendarControl.setValue(date.calendar);

0 commit comments

Comments
 (0)