Skip to content

Commit

Permalink
Merge pull request #58 from ScottLogic/opt-out-fixes
Browse files Browse the repository at this point in the history
Ensure that form can always be submitted when opting out of end user calculations
  • Loading branch information
mgriffin-scottlogic authored May 21, 2024
2 parents 6957afc + b3edd95 commit 446b9f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
18 changes: 14 additions & 4 deletions src/app/carbon-estimator-form/carbon-estimator-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}),
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit 446b9f9

Please sign in to comment.