-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54572af
commit 50016ce
Showing
1 changed file
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,182 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { FormStateService } from './form-state.service'; | ||
import { FormBuilder, FormGroup } from '@angular/forms'; | ||
import { EstimatorFormValues, EstimatorValues, WorldLocation } from '../types/carbon-estimator'; | ||
import { costRanges } from '../carbon-estimator-form/carbon-estimator-form.constants'; | ||
|
||
const defaultValues: Required<EstimatorValues> = { | ||
upstream: { | ||
headCount: 100, | ||
desktopPercentage: 50, | ||
employeeLocation: 'WORLD', | ||
}, | ||
onPremise: { | ||
estimateServerCount: false, | ||
serverLocation: 'WORLD', | ||
numberOfServers: 10, | ||
}, | ||
cloud: { | ||
noCloudServices: false, | ||
cloudLocation: 'WORLD', | ||
cloudPercentage: 50, | ||
monthlyCloudBill: costRanges[0], | ||
}, | ||
downstream: { | ||
noDownstream: false, | ||
customerLocation: 'WORLD', | ||
monthlyActiveUsers: 100, | ||
mobilePercentage: 50, | ||
purposeOfSite: 'average', | ||
}, | ||
}; | ||
|
||
const formBuilder = new FormBuilder(); | ||
|
||
describe('FormStateService', () => { | ||
let service: FormStateService; | ||
let estimatorForm: FormGroup<EstimatorFormValues>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(FormStateService); | ||
estimatorForm = formBuilder.nonNullable.group({ | ||
upstream: formBuilder.nonNullable.group({ | ||
headCount: [defaultValues.upstream.headCount], | ||
desktopPercentage: [defaultValues.upstream.desktopPercentage], | ||
employeeLocation: [defaultValues.upstream.employeeLocation], | ||
}), | ||
onPremise: formBuilder.nonNullable.group({ | ||
estimateServerCount: [defaultValues.onPremise.estimateServerCount], | ||
serverLocation: [defaultValues.onPremise.serverLocation as WorldLocation | 'unknown'], | ||
numberOfServers: [defaultValues.onPremise.numberOfServers], | ||
}), | ||
cloud: formBuilder.nonNullable.group({ | ||
noCloudServices: [false], | ||
cloudLocation: [defaultValues.cloud.cloudLocation as WorldLocation | 'unknown'], | ||
cloudPercentage: [defaultValues.cloud.cloudPercentage], | ||
monthlyCloudBill: [defaultValues.cloud.monthlyCloudBill], | ||
}), | ||
downstream: formBuilder.nonNullable.group({ | ||
noDownstream: [false], | ||
customerLocation: [defaultValues.downstream.customerLocation], | ||
monthlyActiveUsers: [defaultValues.downstream.monthlyActiveUsers], | ||
mobilePercentage: [defaultValues.downstream.mobilePercentage], | ||
purposeOfSite: [defaultValues.downstream.purposeOfSite], | ||
}), | ||
}); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
describe('getControlStates()', () => { | ||
it('should return the states of controls that are marked as dirty or touched', () => { | ||
estimatorForm.get('upstream.headCount')?.markAsDirty(); | ||
estimatorForm.get('upstream.headCount')?.markAsTouched(); | ||
estimatorForm.get('onPremise.serverLocation')?.markAsDirty(); | ||
estimatorForm.get('cloud.cloudLocation')?.markAsTouched(); | ||
|
||
const controlStates = service.getControlStates(estimatorForm); | ||
const expectedControlStates = { | ||
'upstream.headCount': { | ||
dirty: true, | ||
touched: true, | ||
}, | ||
'onPremise.serverLocation': { | ||
dirty: true, | ||
touched: false, | ||
}, | ||
'cloud.cloudLocation': { | ||
dirty: false, | ||
touched: true, | ||
}, | ||
}; | ||
|
||
expect(controlStates).toEqual(expectedControlStates); | ||
}); | ||
}); | ||
|
||
describe('setControlStates()', () => { | ||
const singleControlTestCases = [ | ||
{ | ||
controlName: 'upstream.headCount', | ||
dirty: true, | ||
touched: true, | ||
}, | ||
{ | ||
controlName: 'upstream.headCount', | ||
dirty: true, | ||
touched: false, | ||
}, | ||
{ | ||
controlName: 'upstream.headCount', | ||
dirty: false, | ||
touched: true, | ||
}, | ||
]; | ||
|
||
singleControlTestCases.forEach(testCase => { | ||
it(`should set the control state on the form when dirty = ${testCase.dirty} and touched = ${testCase.touched}`, () => { | ||
const controlStates = { | ||
[testCase.controlName]: { | ||
dirty: testCase.dirty, | ||
touched: testCase.touched, | ||
}, | ||
}; | ||
|
||
service.setControlStates(estimatorForm, controlStates); | ||
const control = estimatorForm.get(testCase.controlName); | ||
const controlState = { | ||
dirty: control?.dirty, | ||
touched: control?.touched, | ||
}; | ||
const expectedControlState = { | ||
dirty: testCase.dirty, | ||
touched: testCase.touched, | ||
}; | ||
|
||
expect(controlState).toEqual(expectedControlState); | ||
}); | ||
}); | ||
|
||
it('should set all the control states when passed multiple control states', () => { | ||
const controlStatesArg = { | ||
'upstream.headCount': { | ||
dirty: true, | ||
touched: true, | ||
}, | ||
'onPremise.serverLocation': { | ||
dirty: true, | ||
touched: false, | ||
}, | ||
'cloud.cloudLocation': { | ||
dirty: false, | ||
touched: true, | ||
}, | ||
}; | ||
|
||
service.setControlStates(estimatorForm, controlStatesArg); | ||
|
||
const controlStates = { | ||
'upstream.headCount': { | ||
dirty: estimatorForm.get('upstream.headCount')?.dirty, | ||
touched: estimatorForm.get('upstream.headCount')?.touched, | ||
}, | ||
'onPremise.serverLocation': { | ||
dirty: estimatorForm.get('onPremise.serverLocation')?.dirty, | ||
touched: estimatorForm.get('onPremise.serverLocation')?.touched, | ||
}, | ||
'cloud.cloudLocation': { | ||
dirty: estimatorForm.get('cloud.cloudLocation')?.dirty, | ||
touched: estimatorForm.get('cloud.cloudLocation')?.touched, | ||
}, | ||
}; | ||
|
||
const expectedControlStates = controlStatesArg; | ||
|
||
expect(controlStates).toEqual(expectedControlStates); | ||
}); | ||
}); | ||
}); |