mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
refactor add tests for validator
This commit is contained in:
37
src/app/shared/utils/ipV4.validator.spec.ts
Normal file
37
src/app/shared/utils/ipV4.validator.spec.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { MarkdownPipe } from './markdown.pipe';
|
||||||
|
import { IpV4Validator } from "./ipV4.validator";
|
||||||
|
import { TestBed } from "@angular/core/testing";
|
||||||
|
import { UntypedFormControl, UntypedFormGroup } from "@angular/forms";
|
||||||
|
|
||||||
|
|
||||||
|
describe('IpV4 validator', () => {
|
||||||
|
|
||||||
|
let ipV4Validator: IpV4Validator;
|
||||||
|
const validIp = '192.168.0.1';
|
||||||
|
const formGroup = new UntypedFormGroup({
|
||||||
|
ip: new UntypedFormControl(''),
|
||||||
|
});
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
IpV4Validator,
|
||||||
|
],
|
||||||
|
}).compileComponents();
|
||||||
|
|
||||||
|
ipV4Validator = TestBed.inject(IpV4Validator);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null for valid ipV4', () => {
|
||||||
|
formGroup.controls.ip.setValue(validIp);
|
||||||
|
expect(ipV4Validator.validate(formGroup.controls.ip as UntypedFormControl)).toBeNull()
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return {isValidIp: false} for invalid Ip', () => {
|
||||||
|
formGroup.controls.ip.setValue('100.260.45.1');
|
||||||
|
expect(ipV4Validator.validate(formGroup.controls.ip as UntypedFormControl)).toEqual({isValidIp: false})
|
||||||
|
formGroup.controls.ip.setValue('100');
|
||||||
|
expect(ipV4Validator.validate(formGroup.controls.ip as UntypedFormControl)).toEqual({isValidIp: false})
|
||||||
|
formGroup.controls.ip.setValue('testString');
|
||||||
|
expect(ipV4Validator.validate(formGroup.controls.ip as UntypedFormControl)).toEqual({isValidIp: false})
|
||||||
|
});
|
||||||
|
});
|
@@ -13,15 +13,14 @@ import {NG_VALIDATORS, Validator, UntypedFormControl} from '@angular/forms';
|
|||||||
*/
|
*/
|
||||||
export class IpV4Validator implements Validator {
|
export class IpV4Validator implements Validator {
|
||||||
validate(formControl: UntypedFormControl): {[key: string]: boolean} | null {
|
validate(formControl: UntypedFormControl): {[key: string]: boolean} | null {
|
||||||
const ipv4Regex = /^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
|
||||||
const ipValue = formControl.value;
|
const ipValue = formControl.value;
|
||||||
const ipParts = ipValue?.split('.');
|
const ipParts = ipValue?.split('.');
|
||||||
const numberOfParts = ipParts.length;
|
|
||||||
|
|
||||||
if (ipValue && (numberOfParts !== 4 || !ipv4Regex.test(ipValue))) {
|
|
||||||
return {isValidIp: false};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (ipv4Regex.test(ipValue) && ipParts.every(part => parseInt(part) <= 255)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {isValidIp: false}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user