Skip to content

Commit ac81742

Browse files
committed
Add validationEngine validate form specs
1 parent e8123ea commit ac81742

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

lib/src/spec/validationEngineValidateForm.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,82 @@ describe('ValidationEngine Validate Form', () => {
489489
done();
490490
});
491491
});
492+
493+
it('Spec #9 => should pass validation when feeding viewModel with nested properties, fullname.firstName equals "john" and is validation required', (done) => {
494+
// Arrange
495+
const validationEngine: ValidationEngine = new ValidationEngine();
496+
const viewModel = {
497+
fullname: {
498+
firstName: 'john',
499+
},
500+
};
501+
502+
// Act
503+
validationEngine.addFieldValidation('fullname.firstName',
504+
(value, vm): Promise<FieldValidationResult> => {
505+
let isFieldInformed: boolean = (value != null && value.length > 0);
506+
let errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";
507+
508+
const validationResult: FieldValidationResult = new FieldValidationResult();
509+
validationResult.type = "REQUIRED";
510+
validationResult.succeeded = isFieldInformed;
511+
validationResult.errorMessage = errorInfo;
512+
513+
return Promise.resolve(validationResult);
514+
}
515+
);
516+
517+
validationEngine.validateForm(viewModel)
518+
.then((formValidationResult: FormValidationResult) => {
519+
expect(formValidationResult.succeeded).to.be.true;
520+
521+
expect(formValidationResult.fieldErrors).to.have.length(1);
522+
523+
expect(formValidationResult.fieldErrors[0].succeeded).to.be.true;
524+
expect(formValidationResult.fieldErrors[0].key).to.be.equal('fullname.firstName');
525+
expect(formValidationResult.fieldErrors[0].type).to.equal('REQUIRED');
526+
expect(formValidationResult.fieldErrors[0].errorMessage).to.equal('');
527+
done();
528+
});
529+
});
530+
531+
it('Spec #10 => should fail validation when feeding viewModel with nested properties, fullname.firstName equals "" and is validation required', (done) => {
532+
// Arrange
533+
const validationEngine: ValidationEngine = new ValidationEngine();
534+
const viewModel = {
535+
fullname: {
536+
firstName: '',
537+
},
538+
};
539+
540+
// Act
541+
validationEngine.addFieldValidation('fullname.firstName',
542+
(value, vm): Promise<FieldValidationResult> => {
543+
let isFieldInformed: boolean = (value != null && value.length > 0);
544+
let errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";
545+
546+
const validationResult: FieldValidationResult = new FieldValidationResult();
547+
validationResult.type = "REQUIRED";
548+
validationResult.succeeded = isFieldInformed;
549+
validationResult.errorMessage = errorInfo;
550+
551+
return Promise.resolve(validationResult);
552+
}
553+
);
554+
555+
validationEngine.validateForm(viewModel)
556+
.then((formValidationResult: FormValidationResult) => {
557+
expect(formValidationResult.succeeded).to.be.false;
558+
559+
expect(formValidationResult.fieldErrors).to.have.length(1);
560+
561+
expect(formValidationResult.fieldErrors[0].succeeded).to.be.false;
562+
expect(formValidationResult.fieldErrors[0].key).to.be.equal('fullname.firstName');
563+
expect(formValidationResult.fieldErrors[0].type).to.equal('REQUIRED');
564+
expect(formValidationResult.fieldErrors[0].errorMessage).to.equal('Mandatory field');
565+
done();
566+
});
567+
});
492568
});
493569

494570
describe('Group #2 => When calling validateForm and addFormValidation with async function', () => {

0 commit comments

Comments
 (0)