javascript - AngularJS directive link function does not run in Jasmine test -
in jsfiddle have directive.
(function() { angular.module('myapp', []) .directive('appfoo', appfoo); function appfoo() { console.log('directive factory runs'); return { controller: appfoocontroller, link: link, replace: true, restrict: 'e', scope: { parentprop: '=' } }; function appfoocontroller($scope) { console.log('controller runs'); $scope.render({ some: 'data' }); } function link($scope, $element) { console.log('link function runs'); $scope.render = function(data) { console.log(shared.$scope.parentprop, $element[0], data); }; } } }());
and jasmine spec;
describe('myapp::appfoo', function() { var shared; beforeeach(function() { shared = {}; shared.markup = '<app-foo parent-prop="someprop"></app-foo>'; inject(function($compile, $rootscope) { shared.$compile = $compile; shared.$parentscope = $rootscope.$new(true); shared.$rootscope = $rootscope; }); shared.createdirective = function() { shared.$element = angular.element(shared.markup); shared.$compile(shared.$element)(shared.$parentscope); shared.$parentscope.$digest(); shared.el = shared.$element[0]; shared.$childscope = shared.$element.scope(); }; }); describe('when compiled', function() { describe('when parameters provided', function() { beforeeach(function() { shared.$parentscope.someprop = { a: 'a', b: 'b' }; shared.createdirective(); }); it('should have render method', function() { expect(typeof shared.$childscope.render).toequal('function'); }); }); }); });
the job of link function add render method scope. works in application, in jasmine tests never runs.
i've looked on @ various other questions listed below, no luck.
any appreciated.
[1]
- angularjs directive link function not working
- angularjs directive link function not executing
- replace element in angularjs directive linking function
- scope.$watch in directive link function not getting called
- angularjs directive link function not being called
- anglarjs directive link function not called compilefunction
- link function not called
- angularjs link function not called
- angularjs directive link function not called in jasmine test
- angular directive link function not called during pagination
- angular directive's link function not being called
- angularjs link function not called due attribute name normalization
- angularjs: why directive's link function not being called?
as pointed out @aliasm2k, issue here controller invoked before link function.
because controller calling $scope.render before link function has created it, call stack abandoned , link function not run.
Comments
Post a Comment