Skip to content

Commit e8123ea

Browse files
committed
add validationEngine validate single field specs
1 parent 5c7f5c4 commit e8123ea

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

lib/src/spec/validationEngineValidateSingleField.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,89 @@ describe('lcFormValidation simple form', () => {
133133
expect(promise).to.eventually.be.rejected.and.notify(done);
134134
});
135135

136+
it('should pass validation when feeding viewModel with nested properties, value equals "john" and validation required',
137+
(done) => {
138+
// Arrange
139+
const formValidationBase: ValidationEngine = new ValidationEngine();
140+
const viewModel = {
141+
id: '1',
142+
fullname: {
143+
firstName: '',
144+
secondName: 'doe',
145+
},
146+
};
147+
148+
const value = 'john';
149+
150+
// Act
151+
formValidationBase.addFieldValidation('fullname.firstName',
152+
(value, vm): Promise<FieldValidationResult> => {
153+
let isFieldInformed: boolean = (value != null && value.length > 0);
154+
155+
let errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";
156+
157+
const validationResult: FieldValidationResult = new FieldValidationResult();
158+
validationResult.type = "REQUIRED";
159+
validationResult.succeeded = isFieldInformed;
160+
validationResult.errorMessage = errorInfo;
161+
162+
return Promise.resolve(validationResult);
163+
}
164+
);
165+
166+
formValidationBase
167+
.validateField(viewModel, 'fullname.firstName', value)
168+
.then((fieldValidationResult: FieldValidationResult) => {
169+
170+
// Assert
171+
expect(fieldValidationResult.key).to.be.equal('fullname.firstName');
172+
expect(fieldValidationResult.type).to.equal('REQUIRED');
173+
expect(fieldValidationResult.succeeded).to.be.true;
174+
expect(fieldValidationResult.errorMessage).to.be.empty;
175+
done();
176+
});
177+
});
178+
179+
it('should fail validation when feeding viewModel with nested properties, value equals "" and validation required',
180+
(done) => {
181+
// Arrange
182+
const formValidationBase: ValidationEngine = new ValidationEngine();
183+
const viewModel = {
184+
id: '1',
185+
fullname: {
186+
firstName: '',
187+
secondName: 'doe',
188+
},
189+
};
190+
191+
const value = '';
192+
193+
// Act
194+
formValidationBase.addFieldValidation('fullname.firstName',
195+
(value, vm): Promise<FieldValidationResult> => {
196+
let isFieldInformed: boolean = (value != null && value.length > 0);
197+
198+
let errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";
199+
200+
const validationResult: FieldValidationResult = new FieldValidationResult();
201+
validationResult.type = "REQUIRED";
202+
validationResult.succeeded = isFieldInformed;
203+
validationResult.errorMessage = errorInfo;
204+
205+
return Promise.resolve(validationResult);
206+
}
207+
);
208+
209+
formValidationBase
210+
.validateField(viewModel, 'fullname.firstName', value)
211+
.then((fieldValidationResult: FieldValidationResult) => {
212+
213+
// Assert
214+
expect(fieldValidationResult.key).to.be.equal('fullname.firstName');
215+
expect(fieldValidationResult.type).to.equal('REQUIRED');
216+
expect(fieldValidationResult.succeeded).to.be.false;
217+
expect(fieldValidationResult.errorMessage).to.equal('Mandatory field');
218+
done();
219+
});
220+
});
136221
});

lib/src/spec/validationsDispatcher.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ describe('ValidationsDispatcher', () => {
399399
});
400400
});
401401

402-
it('Spec #11 => should return empty FieldValidationResult and calls to validation functions ' +
402+
it('Spec #12 => should return empty FieldValidationResult and calls to validation functions ' +
403403
'When passing vm equals undefined, value equals undefined and validationsPerField equals array with one item ' +
404404
'equals validation function resolving a fieldValidationResult equals ""', (done) => {
405405
//Arrange
@@ -433,7 +433,7 @@ describe('ValidationsDispatcher', () => {
433433
});
434434
});
435435

436-
it('Spec #12 => should return undefined FieldValidationResult and calls to first validation function ' +
436+
it('Spec #13 => should return undefined FieldValidationResult and calls to first validation function ' +
437437
'When passing vm equals undefined, value equals undefined and validationsPerField equals array with two items ' +
438438
'first equals validation function resolving a fieldValidationResult equals undefined' +
439439
'second equals successful validation function', (done) => {
@@ -484,7 +484,7 @@ describe('ValidationsDispatcher', () => {
484484
});
485485
});
486486

487-
it('Spec #13 => should return undefined FieldValidationResult and calls to first validation function ' +
487+
it('Spec #14 => should return undefined FieldValidationResult and calls to first validation function ' +
488488
'When passing vm equals undefined, value equals undefined and validationsPerField equals array with two items ' +
489489
'first equals validation function resolving a fieldValidationResult equals undefined' +
490490
'second equals failed validation function', (done) => {
@@ -534,7 +534,7 @@ describe('ValidationsDispatcher', () => {
534534
});
535535
});
536536

537-
it('Spec #14 => should return failed and key equals "test1" FieldValidationResult and calls to first validation function ' +
537+
it('Spec #15 => should return failed and key equals "test1" FieldValidationResult and calls to first validation function ' +
538538
'When passing vm equals undefined, value equals undefined and validationsPerField equals array with two items ' +
539539
'first equals failed validation function' +
540540
'second equals validation function resolving a fieldValidationResult equals undefined', (done) => {
@@ -586,7 +586,7 @@ describe('ValidationsDispatcher', () => {
586586
});
587587
});
588588

589-
it('Spec #15 => should return undefined FieldValidationResult and calls to first and second validation functions ' +
589+
it('Spec #16 => should return undefined FieldValidationResult and calls to first and second validation functions ' +
590590
'When passing vm equals undefined, value equals undefined and validationsPerField equals array with two items ' +
591591
'first equals successful validation function' +
592592
'second equals validation function resolving a fieldValidationResult equals undefined', (done) => {
@@ -636,7 +636,7 @@ describe('ValidationsDispatcher', () => {
636636
});
637637
});
638638

639-
it('should pass customParams to its proper validationFunction', (done) => {
639+
it('Spec #17 => should pass customParams to its proper validationFunction', (done) => {
640640
//Arrange
641641
const vm = undefined;
642642
const value = undefined;

0 commit comments

Comments
 (0)