Skip to content

Commit

Permalink
Add validationEngine validate form specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdan committed Aug 27, 2018
1 parent e8123ea commit ac81742
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/src/spec/validationEngineValidateForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,82 @@ describe('ValidationEngine Validate Form', () => {
done();
});
});

it('Spec #9 => should pass validation when feeding viewModel with nested properties, fullname.firstName equals "john" and is validation required', (done) => {
// Arrange
const validationEngine: ValidationEngine = new ValidationEngine();
const viewModel = {
fullname: {
firstName: 'john',
},
};

// Act
validationEngine.addFieldValidation('fullname.firstName',
(value, vm): Promise<FieldValidationResult> => {
let isFieldInformed: boolean = (value != null && value.length > 0);
let errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";

const validationResult: FieldValidationResult = new FieldValidationResult();
validationResult.type = "REQUIRED";
validationResult.succeeded = isFieldInformed;
validationResult.errorMessage = errorInfo;

return Promise.resolve(validationResult);
}
);

validationEngine.validateForm(viewModel)
.then((formValidationResult: FormValidationResult) => {
expect(formValidationResult.succeeded).to.be.true;

expect(formValidationResult.fieldErrors).to.have.length(1);

expect(formValidationResult.fieldErrors[0].succeeded).to.be.true;
expect(formValidationResult.fieldErrors[0].key).to.be.equal('fullname.firstName');
expect(formValidationResult.fieldErrors[0].type).to.equal('REQUIRED');
expect(formValidationResult.fieldErrors[0].errorMessage).to.equal('');
done();
});
});

it('Spec #10 => should fail validation when feeding viewModel with nested properties, fullname.firstName equals "" and is validation required', (done) => {
// Arrange
const validationEngine: ValidationEngine = new ValidationEngine();
const viewModel = {
fullname: {
firstName: '',
},
};

// Act
validationEngine.addFieldValidation('fullname.firstName',
(value, vm): Promise<FieldValidationResult> => {
let isFieldInformed: boolean = (value != null && value.length > 0);
let errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";

const validationResult: FieldValidationResult = new FieldValidationResult();
validationResult.type = "REQUIRED";
validationResult.succeeded = isFieldInformed;
validationResult.errorMessage = errorInfo;

return Promise.resolve(validationResult);
}
);

validationEngine.validateForm(viewModel)
.then((formValidationResult: FormValidationResult) => {
expect(formValidationResult.succeeded).to.be.false;

expect(formValidationResult.fieldErrors).to.have.length(1);

expect(formValidationResult.fieldErrors[0].succeeded).to.be.false;
expect(formValidationResult.fieldErrors[0].key).to.be.equal('fullname.firstName');
expect(formValidationResult.fieldErrors[0].type).to.equal('REQUIRED');
expect(formValidationResult.fieldErrors[0].errorMessage).to.equal('Mandatory field');
done();
});
});
});

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

0 comments on commit ac81742

Please sign in to comment.