describe('Directive validation', () => { let scope; let element; let compile; beforeEach(ngModule('vnCore')); compile = (_element, validations, value) => { inject(($compile, $rootScope, $window) => { $window.validations = validations; scope = $rootScope.$new(); scope.user = {name: value}; element = angular.element(_element); $compile(element)(scope); scope.$digest(); }); }; it(`should throw an error if the vnValidation doesn't have the right syntax`, () => { let html = ``; expect(() => { compile(html, {}); }).toThrow(new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`)); }); it('should throw an error if the window.validations aint defined', () => { let html = ``; expect(() => { compile(html, {}); }).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`)); }); describe('Validator presence()', () => { it('should not validate the user name as it is an empty string', () => { let html = `
`; let validations = {User: {validations: {name: [{validation: 'presence'}]}}}; compile(html, validations, 'Spiderman'); scope.user.name = ''; scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); }); describe('Validator absence()', () => { it('should not validate the entity as it should be an empty string', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'absence'}]}}}; compile(html, validations, 'Spiderman'); scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); it('should validate the entity as it is an empty string', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'absence'}]}}}; compile(html, validations, ''); scope.$digest(); expect(element[0].classList).toContain('ng-valid'); expect(element[0].classList).not.toContain('ng-invalid'); }); }); describe('Validator length()', () => { it('should not validate the user name as it should have min length of 15', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}}; compile(html, validations, 'fifteen!'); scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); it('should validate the user name as it has length of 15', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}}; compile(html, validations, 'fifteen length!'); scope.$digest(); expect(element[0].classList).toContain('ng-valid'); expect(element[0].classList).not.toContain('ng-invalid'); }); it('should not validate the user name as it should have min length of 10', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}}; compile(html, validations, 'shortname'); scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); it('should validate the user name as its length is greater then the minimum', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}}; compile(html, validations, 'verylongname'); scope.$digest(); expect(element[0].classList).toContain('ng-valid'); expect(element[0].classList).not.toContain('ng-invalid'); }); it('should not validate the user name as its length is greater then the maximum', () => { let html = ``; let validations = {User: {validations: {name: [{validation: 'length', max: 10}]}}}; compile(html, validations, 'toolongname'); scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); }); describe('Validator numericality()', () => { it('should not validate the phone number as it should a integer', () => { let html = ``; let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}}; compile(html, validations, 'spiderman'); scope.user.phone = 'this is not a phone number!'; scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); it('should validate the phone number as it an integer', () => { let html = ``; let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}}; compile(html, validations, 'spiderman'); scope.user.phone = '555555555'; scope.$digest(); expect(element[0].classList).toContain('ng-valid'); expect(element[0].classList).not.toContain('ng-invalid'); }); }); describe('Validator inclusion()', () => { it('should not validate the phone number as it is not an integer', () => { let html = ``; let validations = {User: {validations: {phone: [{validation: 'inclusion'}]}}}; compile(html, validations, 'spiderman'); scope.user.phone = 'this is not a phone number!'; scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); }); describe('Validator exclusion()', () => { it('should validate the phone number as it is an integer', () => { let html = ``; let validations = {User: {validations: {phone: [{validation: 'exclusion'}]}}}; compile(html, validations, 'spiderman'); scope.user.phone = '555555555'; scope.$digest(); expect(element[0].classList).toContain('ng-valid'); expect(element[0].classList).not.toContain('ng-invalid'); }); }); describe('Validator format()', () => { it('should not validate the email number as it doesnt contain @', () => { let html = ``; let validations = {User: {validations: {email: [{validation: 'format', with: '@'}]}}}; compile(html, validations, 'spiderman'); scope.user.email = 'userverdnatura.es'; scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); expect(element[0].classList).not.toContain('ng-valid'); }); }); });