From 4fbc8096830f4fb6701872bd180340cab177e3f0 Mon Sep 17 00:00:00 2001 From: Matthew Griffin Date: Fri, 17 May 2024 15:33:38 +0100 Subject: [PATCH 1/5] Enable/disable monthly active users based on noDownstream changes --- .../carbon-estimator-form.component.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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..30cf007a 100644 --- a/src/app/carbon-estimator-form/carbon-estimator-form.component.ts +++ b/src/app/carbon-estimator-form/carbon-estimator-form.component.ts @@ -126,9 +126,15 @@ 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.estimatorForm.get('cloud.cloudPercentage')?.valueChanges.subscribe(cloudPercentage => { this.cloudPercentage = cloudPercentage; From 4c9bfe13b06d975e6119e4db2b84a9fafc848550 Mon Sep 17 00:00:00 2001 From: Matthew Griffin Date: Fri, 17 May 2024 15:34:23 +0100 Subject: [PATCH 2/5] Also validate that monthly active users is not empty --- .../carbon-estimator-form/carbon-estimator-form.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 30cf007a..47be6583 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], }), From 2551653743da2d39595a250081e2b7ca325f5c18 Mon Sep 17 00:00:00 2001 From: Matthew Griffin Date: Fri, 17 May 2024 15:41:06 +0100 Subject: [PATCH 3/5] Ensure monthly active users is always set --- .../carbon-estimator-form/carbon-estimator-form.component.ts | 3 +++ 1 file changed, 3 insertions(+) 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 47be6583..ed066f1c 100644 --- a/src/app/carbon-estimator-form/carbon-estimator-form.component.ts +++ b/src/app/carbon-estimator-form/carbon-estimator-form.component.ts @@ -161,6 +161,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); } From 124707987d9c022157d525dc28c63097a958c036 Mon Sep 17 00:00:00 2001 From: Matthew Griffin Date: Fri, 17 May 2024 16:26:22 +0100 Subject: [PATCH 4/5] Add unit tests around opt out behaviour --- .../carbon-estimator-form.component.spec.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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(); + }); }); }); From b3edd954c77d642b77f2eefdf6f0c7dc5d7e4041 Mon Sep 17 00:00:00 2001 From: Matthew Griffin Date: Mon, 20 May 2024 09:56:07 +0100 Subject: [PATCH 5/5] Detect changes after enabling/disabling monthly active users. --- src/app/carbon-estimator-form/carbon-estimator-form.component.ts | 1 + 1 file changed, 1 insertion(+) 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 ed066f1c..c877a668 100644 --- a/src/app/carbon-estimator-form/carbon-estimator-form.component.ts +++ b/src/app/carbon-estimator-form/carbon-estimator-form.component.ts @@ -134,6 +134,7 @@ export class CarbonEstimatorFormComponent implements OnInit { monthlyActiveUsers?.enable(); } this.noDownstream = noDownstream; + this.changeDetector.detectChanges(); }); this.estimatorForm.get('cloud.cloudPercentage')?.valueChanges.subscribe(cloudPercentage => {