angularjs - Testing KeyPress on inputbox in an Angular directive is undefined -
angularjs - Testing KeyPress on inputbox in an Angular directive is undefined -
this directive testing:
'use strict'; angular.module('vsnap.emailinput', [ 'services.emailcollection' ]) .directive('vsemailinput', function() { homecoming { 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 = ''; // sets focus on email input $scope.setinputfocus = function() { document.getelementbyid('emailinput').focus(); }; // watches key inputs , edits emails accordingly $scope.keydown = function(event) { var keycode = event.keycode; // comma & tab if ((keycode === 188) || (keycode === 9)) { event.preventdefault(); $scope.addemail($scope.newemail); $scope.newemail = ''; } }; } ] }; } );
which called from:
<input type="text" ng-model="newemail" placeholder="enter email" ng-change="checksuggestions()" ng-keydown="keydown($event)" id="emailinput">
this works well, , problem having trying trigger action while testing karma , jasmine.
this have in 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_; // create element load our directive 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 valid', function() { $("#emailinput").focus(); var e = jquery.event("keydown"); e.which = 77; // # key code value $("#emailinput").val(string.fromcharcode(e.which)); $("#emailinput").trigger(e); }); });
after many attempts... got 'should check if email valid' thought here. doesn't seem fail, value of $('input') not changing.
console.log of $('#emailinput').val() comes undefined.
ultimately, trying type in 3 characters text box , expect api phone call when kits keydown() function.
this first question on stackoverflow, sorry may have missed.
angularjs testing keypress karma-runner directive
Comments
Post a Comment