[英]Unit test angular case switch formControl
I am trying to test some function with unit test, something like this我正在尝试使用单元测试来测试一些 function,就像这样
public setFormControlsValue(formControl: AbstractControl, buildingType: buildingTypeOption, value?: string): void {
switch (buildingType) {
case buildingTypeOption.twoToFourFacades:
case buildingTypeOption.additionalConstruction:
case buildingTypeOption.industrialBuilding:
formControl.setValidators([Validators.required]);
formControl.updateValueAndValidity();
formControl.enable({ onlySelf: true });
break;
case buildingTypeOption.lightConstruction:
debugger
formControl.setValidators(null);
formControl.setValue(null);
formControl.clearValidators();
formControl.updateValueAndValidity();
formControl.disable({ onlySelf: true });
break;
case null:
formControl.setValidators([Validators.required]);
formControl.setValue(null);
formControl.updateValueAndValidity();
formControl.enable({ onlySelf: true });
break;
default:
formControl.setValue(value);
formControl.updateValueAndValidity();
formControl.disable({ onlySelf: true });
break;
}
}
And in unit test something like this在单元测试中是这样的
describe('has called setFormControlsValue ', () => {
let formControlsValue = {};
let formBuilder = new FormBuilder();
let formControls: AbstractControl;
beforeEach(() => {
formControlsValue = {
destinationEquipment: destinationEquipment.nonEquippedDestination
};
formControls = formBuilder.group(formControlsValue);
});
it('formControls should be twoToFourFacades and enabled', () => {
// GIVEN
// WHEN
service.setFormControlsValue(formControls, buildingTypeOption.twoToFourFacades);
// THEN
expect(formControls.disabled).toBeFalsy();
});
});
Problem I have is how to test this line of code formControl.setValidators([Validators.required]);我遇到的问题是如何测试这行代码formControl.setValidators([Validators.required]); or maybe I am not going in correct direction of setting test?
或者我可能没有按照正确的方向设置测试?
how to test this line of code formControl.setValidators([Validators.required]);
如何测试这行代码 formControl.setValidators([Validators.required]);
In prepared conditions, make relevant code execute and then, check if formControl is invalid and has proper {required:true}
error.在准备好的条件下,执行相关代码,然后检查formControl是否无效,是否有正确的
{required:true}
错误。
expectThat(formControl.invalid).toBeTrue();
expectThat(formControl.errors).toEqual({required:true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.