This is the directive I'm testing:
'use strict';
angular.module('vsnap.emailInput', [
'services.emailCollection'
])
.directive('vsEmailInput', function() {
return {
restrict: 'A',
replace: true,
templateUrl: 'common/directives/emailInput/emailInput.html',
scope: {
EmailCollection: '=emails',
ErrorMessage: '=message',
listLimit: '=limit'
},
controller: [
'$element',
'$document',
'$scope',
function(
$element,
$document,
$scope) {
$scope.newEmail = '';
$scope.setInputFocus = function() {
document.getElementById('emailInput').focus();
};
$scope.keyDown = function(event) {
var keyCode = event.keyCode;
if ((keyCode === 188) || (keyCode === 9)) {
event.preventDefault();
$scope.addEmail($scope.newEmail);
$scope.newEmail = '';
}
};
}
]
};
}
);
Called from:
<input type="text" ng-model="newEmail" placeholder="Enter an email" ng-change="checkSuggestions()" ng-keydown="keyDown($event)" id="emailInput">
This works well, and the problem I am facing is trying to trigger this action when testing with Karma and Jasmine.
This is what I have in the test:
describe('email input directive', function() {
var elm, scope, httpBackend, apiUrl, ctrl;
beforeEach(module('vsnap', 'templates'));
beforeEach(inject(function($rootScope, $compile, $httpBackend,
_apiUrl_, $controller) {
httpBackend = $httpBackend;
apiUrl =_apiUrl_;
elm = angular.element(
'<div vs-email-input emails="EmailCollection" ' +
'message="emailError" limit="3"></div>');
$scope = $rootScope.$new();
$rootScope.newEmail = '';
ctrl = $controller('DefaultCtrl', {$scope:$scope});
$compile(elm)($scope);
$scope.$digest();
$rootScope.$apply();
}
));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should check if email is valid', function() {
$("#emailInput").focus();
var e = jQuery.Event("keydown");
e.which = 77;
$("#emailInput").val(String.fromCharCode(e.which));
$("#emailInput").trigger(e);
});
});
After many attempts ... I got the message "must check if the email is valid" from here . This does not seem to work, but the value of $ ('input') does not change.
Console.log of $ ('# emailInput'). val () returns undefined.
Ultimately, what I'm trying to do is enter 3 characters in this text box, and then wait for the API call when it dials the keyDown () function.
Stackoverflow, -, , , .