I have a form with the following input:
<input type="email" name="email" ng-model="register.form.email" ng-pattern="emailRegex">
I want to test that the form is invalid if the user types an invalid email:
it('...', function() {
form.email.$setViewValue('invalid');
expect(form.email.$valid).toBeFalsy(); // OK
form.email.$setViewValue('invalid@');
expect(form.email.$valid).toBeFalsy(); // OK
form.email.$setViewValue('invalid@host');
expect(form.email.$valid).toBeFalsy(); // Not OK
});
The last expectatijon fails because the input is valid, although the email regex expects a domain. It feels as if the pattern is not used in my test because when I change the type to text, it already fails at the first expectation. How can I make sure the pattern is picked up in the tests?
For completeness, this is the regex (not written by me):
^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[_A-Za-z0-9]+[_A-Za-z0-9-]*[_A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$
Changing the type to text
didn't work.
I'm using jasmine 2.4.1 and angular 1.4.2. I'm using ngHtml2JsPreprocessor as explained here to initialize the form in my tests.
Edit:
This is how to form is initialized:
beforeEach(inject(function($rootScope, $templateCache, $compile) {
var $scope = $rootScope.$new();
vm = $controller('RegisterController', {$scope: $scope});
var templateHtml = $templateCache.get("register.html");
var template = angular.element("<div>" + templateHtml + "</div>");
$compile(template)($scope);
var form = $scope.registerForm;
$scope.$apply();
scope = $scope;
}));