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 -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -