Unit test for a custom validator and AbstractControl value

5,008 views
Skip to first unread message

louis745

unread,
Mar 8, 2018, 3:15:20 PM3/8/18
to Angular and AngularJS discussion

Hello everyone,

I try to unit test this validator and I wonder to to give a value to the AbstractControl.  Thanks for your precious help.

My validator

export class VerifierNombresValidator {

    static plage(min: number, max: number): ValidatorFn {
        return (c: AbstractControl): { [key: string]: boolean } | null => {

             if ((c.value) && c.value >= min && c.value <= max) {
                return null;
            }
            return { 'plage': true };        
        };
    }
}

My unit test (but not working) :


  it('plage pour la valeur 3', () => {
    let validatorFn = VerifierNombresValidator.apply(88);
    let control = { value: '6' }
   
    VerifierNombresValidator.arguments(control as AbstractControl);
    let result =  VerifierNombresValidator.plage(1,5);
   
    expect(result['plage']).toBeUndefined();
  });

Lalan Kumar

unread,
Apr 22, 2020, 2:35:27 AM4/22/20
to Angular and AngularJS discussion
solution: 
 Just pass the AbstractControl param as inner function

it('plage pour la valeur 3', () => {
let control = { value: '6' }
    let result =  VerifierNombresValidator.plage(1,5)(control as AbstractControl);
    expect(result['plage']).toBeUndefined();
 });
 
Reply all
Reply to author
Forward
0 new messages