diff --git a/src/app/carbon-estimator-form/carbon-estimator-form.component.spec.ts b/src/app/carbon-estimator-form/carbon-estimator-form.component.spec.ts index a0af8a30..a81573d0 100644 --- a/src/app/carbon-estimator-form/carbon-estimator-form.component.spec.ts +++ b/src/app/carbon-estimator-form/carbon-estimator-form.component.spec.ts @@ -16,7 +16,26 @@ describe('CarbonEstimatorFormComponent', () => { fixture.detectChanges(); }); - it('should create', () => { + it('should create component form in a valid state', () => { expect(component).toBeTruthy(); + component.ngOnInit(); + expect(component.estimatorForm.valid).toBeTruthy(); + }); + + describe('Downstream', () => { + it('should invalidate form when monthly active users are zero', () => { + component.ngOnInit(); + component.estimatorForm.get('downstream.monthlyActiveUsers')?.setValue(0); + fixture.detectChanges(); + expect(component.estimatorForm.valid).toBeFalsy(); + }); + + it('should validate form when downstream is excluded and monthly active users are zero', () => { + component.ngOnInit(); + component.estimatorForm.get('downstream.monthlyActiveUsers')?.setValue(0); + component.estimatorForm.get('downstream.noDownstream')?.setValue(true); + fixture.detectChanges(); + expect(component.estimatorForm.valid).toBeTruthy(); + }); }); }); diff --git a/src/app/carbon-estimator-form/carbon-estimator-form.component.ts b/src/app/carbon-estimator-form/carbon-estimator-form.component.ts index 007887c0..c877a668 100644 --- a/src/app/carbon-estimator-form/carbon-estimator-form.component.ts +++ b/src/app/carbon-estimator-form/carbon-estimator-form.component.ts @@ -94,7 +94,7 @@ export class CarbonEstimatorFormComponent implements OnInit { downstream: this.formBuilder.nonNullable.group({ noDownstream: [false], customerLocation: [defaultValues.downstream.customerLocation], - monthlyActiveUsers: [defaultValues.downstream.monthlyActiveUsers], + monthlyActiveUsers: [defaultValues.downstream.monthlyActiveUsers, Validators.required], mobilePercentage: [defaultValues.downstream.mobilePercentage], purposeOfSite: [defaultValues.downstream.purposeOfSite], }), @@ -126,9 +126,16 @@ export class CarbonEstimatorFormComponent implements OnInit { this.changeDetector.detectChanges(); }); - this.estimatorForm - .get('downstream.noDownstream') - ?.valueChanges.subscribe(noDownstream => (this.noDownstream = noDownstream)); + this.estimatorForm.get('downstream.noDownstream')?.valueChanges.subscribe(noDownstream => { + const monthlyActiveUsers = this.estimatorForm.get('downstream.monthlyActiveUsers'); + if (noDownstream) { + monthlyActiveUsers?.disable(); + } else { + monthlyActiveUsers?.enable(); + } + this.noDownstream = noDownstream; + this.changeDetector.detectChanges(); + }); this.estimatorForm.get('cloud.cloudPercentage')?.valueChanges.subscribe(cloudPercentage => { this.cloudPercentage = cloudPercentage; @@ -155,6 +162,9 @@ export class CarbonEstimatorFormComponent implements OnInit { if (formValue.cloud.cloudLocation === 'unknown') { formValue.cloud.cloudLocation = 'WORLD'; } + if (!formValue.downstream.monthlyActiveUsers) { + formValue.downstream.monthlyActiveUsers = 0; + } this.formSubmit.emit(formValue as EstimatorValues); }