salix/front/core/lib/specs/validator.spec.js

185 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

2019-10-11 15:38:04 +00:00
import {validate} from '../validator.js';
describe('Validator', () => {
let $translate;
beforeEach(ngModule('vnCore'));
2019-10-11 15:38:04 +00:00
beforeEach(inject(_$translate_ => {
$translate = _$translate_;
}));
describe('presence', () => {
let conf = {validation: 'presence'};
it('should not validate the value as it should be defined', () => {
expect(() => {
validate($translate, '', conf);
}).toThrowError();
});
it('should validate the value as it should be defined', () => {
expect(() => {
validate($translate, 'aDefinedValue', conf);
}).not.toThrowError();
});
});
describe('absence', () => {
let conf = {validation: 'absence'};
it('should not validate the value as it should be undefined', () => {
expect(() => {
validate($translate, 'aDefinedValue', conf);
}).toThrowError();
});
it('should validate the value as it should be undefined', () => {
expect(() => {
validate($translate, '', conf);
}).not.toThrowError();
});
});
describe('length', () => {
it('should not validate the value as it should have an specific valid length', () => {
let conf = {
validation: 'length',
is: 25
};
expect(() => {
validate($translate, 'invalidSpecificLengthString', conf);
}).toThrowError();
});
it('should validate the value as it should have an specific valid length', () => {
let conf = {
validation: 'length',
is: 25
};
expect(() => {
validate($translate, 'validSpecificLengthString', conf);
}).not.toThrowError();
});
it('should not validate the value as it should have a min length', () => {
let conf = {
validation: 'length',
min: 10
};
expect(() => {
validate($translate, 'shortName', conf);
}).toThrowError();
});
it('should validate the value as it should have a min length', () => {
let conf = {
validation: 'length',
min: 10
};
expect(() => {
validate($translate, 'veryLongName', conf);
}).not.toThrowError();
});
it('should not validate the value as it should be smaller than the maximum', () => {
let conf = {
validation: 'length',
max: 20
};
expect(() => {
validate($translate, 'chainThatExceedsMaxLength', conf);
}).toThrowError();
});
it('should validate the value as it should be smaller than the maximum', () => {
let conf = {
validation: 'length',
max: 20
};
expect(() => {
validate($translate, 'shortString', conf);
}).not.toThrowError();
});
});
describe('numericality', () => {
let conf = {validation: 'numericality'};
it('should not validate the value as it should be an integer', () => {
expect(() => {
validate($translate, 'notANumber', conf);
}).toThrowError();
});
it('should validate the value as it should be an integer', () => {
expect(() => {
validate($translate, '123456789', conf);
}).not.toThrowError();
});
});
describe('inclusion', () => {
let conf = {
validation: 'inclusion',
in: ['firstValue', 'seekValue', 'lastValue']
};
it('should not validate the value as it should be in array', () => {
expect(() => {
validate($translate, 'notIncludedValue', conf);
}).toThrowError();
});
it('should validate the value as it should be in array', () => {
expect(() => {
validate($translate, 'seekValue', conf);
}).not.toThrowError();
});
});
describe('exclusion', () => {
let conf = {
validation: 'exclusion',
in: ['firstValue', 'seekValue', 'lastValue']
};
it('should not validate the value as it should not be in array', () => {
expect(() => {
validate($translate, 'seekValue', conf);
}).toThrowError();
});
it('should validate the value as it should not be in array', () => {
expect(() => {
validate($translate, 'notIncludedValue', conf);
}).not.toThrowError();
});
});
describe('format', () => {
let conf = {
validation: 'format',
with: /[a-z-]+@[a-z-]+\.[a-z]+/
};
it('should not validate the value as it should match regexp', () => {
expect(() => {
validate($translate, 'wrongValue', conf);
}).toThrowError();
});
it('should validate the value as it should match regexp', () => {
expect(() => {
validate($translate, 'valid-value@domain.com', conf);
}).not.toThrowError();
});
});
});