angularjs - how can i test custom directive in jasmine -


i'm trying write test case custom directives jasmine. currently, have directive setup shown here: code in fiddler http://jsfiddle.net/ahmpv/33/

app.directive('focusme', function ($timeout) {  return {     scope: { trigger: '=focusme' },     link: function (scope, element) {         scope.$watch('trigger', function (value) {             if (value === true) {                 element[0].focus();              }         });     } }; 

});

my jasmine code in fiddler tried different ways test $watch function unable achieve , please

you set $watch on scope.trigger, passed in focus-me.

you can use spyon element[0].focus , create test focus-me="true" , focus-me="false":

describe('directive: focusme', function() {     var element,         scope;      beforeeach(module('commonbusinfo'));     beforeeach(inject(function($rootscope) {         scope = $rootscope.$new();     }));      it("should focus if focus-me true", inject(function($compile) {         element = angular.element('<input type="text" focus-me="true" value="chaitu">');         element = $compile(element)(scope);         spyon(element[0], 'focus');          scope.$digest();          expect(element).tobedefined();         expect(element[0].focus).tohavebeencalled();     }));      it("should not focus if focus-me falsy", inject(function($compile) {         element = angular.element('<input type="text" focus-me="" value="chaitu">');         element = $compile(element)(scope);         spyon(element[0], 'focus');          scope.$digest();          expect(element).tobedefined();         expect(element[0].focus).not.tohavebeencalled();     })); }); 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

node.js - Express and Redis - If session exists for this user, don't allow access -

excel - I can't get the attachement of the email PHP -