-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdirective-inline-without-test-element.js
64 lines (53 loc) · 1.92 KB
/
directive-inline-without-test-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function specialChars() {
var directive = {
restrict: 'A',
require: 'ngModel',
link: function(scope, elem, attr, ngModel) {
var specialCharsRegex = new RegExp('[!@#$%^&*()_+]+');
ngModel.$parsers.unshift(function(viewValue) {
ngModel.$setValidity('specialChars', !specialCharsRegex.test(viewValue));
return viewValue;
});
}
};
return directive;
}
angular
.module('directiveInlineWithoutTE', [])
.directive('specialChars', specialChars);
describe('Directive: specialChars', function() {
var $compile, element, $rootScope, $scope;
beforeEach(module('directiveInlineWithoutTE'));
$compile = {};
$rootScope = {};
element = {};
$scope = {};
beforeEach(function() {
inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$scope = $rootScope;
$scope.someValue = '';
element = angular.element('<input ng-model="someValue" special-chars>');
$compile(element)($scope);
angular.element(document.body).append(element);
$scope.$apply();
});
});
it('should compile', function() {
expect(element).toBeTruthy();
});
it('should not add ng-invalid-special-chars class when empty', function() {
expect(element.hasClass('ng-invalid-special-chars')).toBeFalsy();
});
it('should add ng-valid-special-chars class when value doesn\'t contain special chars', function() {
angular.element(element[0]).val('asd').triggerHandler('input');
expect(element.hasClass('ng-valid-special-chars')).toBeTruthy();
expect(element.hasClass('ng-invalid-special-chars')).toBeFalsy();
});
it('should add ng-invalid-special-chars class when value contains special chars', function() {
angular.element(element[0]).val('@#').triggerHandler('input');
expect(element.hasClass('ng-invalid-special-chars')).toBeTruthy();
expect(element.hasClass('ng-valid-special-chars')).toBeFalsy();
});
});